Skip to content

Incorrect rewrite of a simple program #1008

@anuragsiy7504

Description

@anuragsiy7504

Hello,

I am trying to optimise the following program:

#include <stdio.h>
#include <cstdlib>
#include <stddef.h>
#include <stdint.h>


int count(int var) {
  int res;
  for(res = 0; res < var; res++);
  return res;
}

int main() {
  int val = 100000;
  int res = count(val);

  printf("%d\n", res);
  return 0;
}

The Function extracted by stoke is:

  .text
  .globl _Z5counti
  .type _Z5counti, @function

#! file-offset 0x566
#! rip-offset  0x400566
#! capacity    26 bytes

# Text              #  Line  RIP       Bytes  Opcode
._Z5counti:         #        0x400566  0      OPC=<label>
  movl %edi, %eax   #  1     0x400566  2      OPC=movl_r32_r32
  testl %edi, %edi  #  2     0x400568  2      OPC=testl_r32_r32
  jle .L_40057a     #  3     0x40056a  2      OPC=jle_label
  movl $0x0, %edx   #  4     0x40056c  5      OPC=movl_r32_imm32
.L_400571:          #        0x400571  0      OPC=<label>
  addl $0x1, %edx   #  5     0x400571  3      OPC=addl_r32_imm8
  cmpl %eax, %edx   #  6     0x400574  2      OPC=cmpl_r32_r32
  jne .L_400571     #  7     0x400576  2      OPC=jne_label
  nop               #  8     0x400578  1      OPC=nop
  retq              #  9     0x400579  1      OPC=retq
.L_40057a:          #        0x40057a  0      OPC=<label>
  movl $0x0, %eax   #  10    0x40057a  5      OPC=movl_r32_imm32
  retq              #  11    0x40057f  1      OPC=retq

.size _Z5counti, .-_Z5counti

The rewrite discovered by stoke is:

  .text
  .globl _Z5counti
  .type _Z5counti, @function

#! file-offset 0x566
#! rip-offset  0x400566
#! capacity    26 bytes

# Text                    #  Line  RIP       Bytes  Opcode
._Z5counti:               #        0x400566  0      OPC=<label>
  lzcntl %edi, %edx       #  1     0x400566  4      OPC=lzcntl_r32_r32
  bzhil %edx, %edi, %eax  #  2     0x40056a  5      OPC=bzhil_r32_r32_r32
  retq                    #  3     0x40056f  1      OPC=retq

.size _Z5counti, .-_Z5counti

The stoke debug verify command :
stoke debug verify --def_in "{ %edi }" --live_out "{ %eax }" --target bins/_Z5counti.s --rewrite result.s --abi_check --strategy bounded --bound 64

Returns : Equivalent: yes

But on incorporating the rewrite into the original program produces incorrect results.
Is my understanding correct here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions