#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ expectCHKSUM(int);
int /*<<< orphan*/ expectETX(int);
int /*<<< orphan*/ expectPacketType(int, int);
int /*<<< orphan*/ expectSTX(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sendETX(int);
int /*<<< orphan*/ sendSTX(int);
int /*<<< orphan*/ sendchecksum(int);
int /*<<< orphan*/ senddata(int, long);
void eraseflash(int fd, long int startaddr, int rows) {
  printf("SENDing Erase Flash: ");
  sendSTX(fd);
  sendSTX(fd);
  senddata(fd, 0x03);
  senddata(fd, rows);
  senddata(fd, startaddr & 0x0000FF);
  senddata(fd, (startaddr >> 8) & 0x0000FF);
  senddata(fd, (startaddr >> 16) & 0x0000FF);
  sendchecksum(fd);
  sendETX(fd);
  printf("RECEIVING: ");
  expectSTX(fd);
  expectSTX(fd);
  expectPacketType(fd, 0x03);
  expectCHKSUM(fd);
  if (!expectETX(fd)) {
    printf("[ERROR: ACK for erase flash incomplete]\n");
    close(fd);
    exit(-1);
  }
  printf("[OK => Erase Flash Program Memory]\n");
}


x86 assembly (clang, -O0):
<eraseflash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    0xe37(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <sendSTX@plt>
mov    -0x4(%rbp),%edi
call   1050 <sendSTX@plt>
mov    -0x4(%rbp),%edi
mov    $0x3,%esi
call   1040 <senddata@plt>
mov    -0x4(%rbp),%edi
movslq -0x14(%rbp),%rsi
call   1040 <senddata@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
and    $0xff,%rsi
call   1040 <senddata@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
sar    $0x8,%rsi
and    $0xff,%rsi
call   1040 <senddata@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
sar    $0x10,%rsi
and    $0xff,%rsi
call   1040 <senddata@plt>
mov    -0x4(%rbp),%edi
call   1030 <sendchecksum@plt>
mov    -0x4(%rbp),%edi
call   10b0 <sendETX@plt>
lea    0xdc7(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   10d0 <expectSTX@plt>
mov    -0x4(%rbp),%edi
call   10d0 <expectSTX@plt>
mov    -0x4(%rbp),%edi
mov    $0x3,%esi
call   1090 <expectPacketType@plt>
mov    -0x4(%rbp),%edi
call   10a0 <expectCHKSUM@plt>
mov    -0x4(%rbp),%edi
call   1080 <expectETX@plt>
cmp    $0x0,%eax
jne    12a8 <eraseflash+0xf8>
lea    0xd91(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xd9c(%rip),%rdi
call   1060 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eraseflash>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0xe2d(%rip),%rdi
call   1070 <printf@plt>
mov    %ebx,%edi
call   1060 <sendSTX@plt>
mov    %ebx,%edi
call   1060 <sendSTX@plt>
mov    $0x3,%esi
mov    %ebx,%edi
call   1040 <senddata@plt>
movslq %r14d,%rsi
mov    %ebx,%edi
call   1040 <senddata@plt>
movzbl %bpl,%esi
mov    %ebx,%edi
call   1040 <senddata@plt>
mov    %rbp,%rax
movzbl %ah,%esi
mov    %ebx,%edi
call   1040 <senddata@plt>
shr    $0x10,%ebp
movzbl %bpl,%esi
mov    %ebx,%edi
call   1040 <senddata@plt>
mov    %ebx,%edi
call   1030 <sendchecksum@plt>
mov    %ebx,%edi
call   10c0 <sendETX@plt>
lea    0xddf(%rip),%rdi
call   1070 <printf@plt>
mov    %ebx,%edi
call   10e0 <expectSTX@plt>
mov    %ebx,%edi
call   10e0 <expectSTX@plt>
mov    %ebx,%edi
mov    $0x3,%esi
call   10a0 <expectPacketType@plt>
mov    %ebx,%edi
call   10b0 <expectCHKSUM@plt>
mov    %ebx,%edi
call   1090 <expectETX@plt>
test   %eax,%eax
je     1278 <eraseflash+0xb8>
lea    0xddb(%rip),%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <puts@plt>
lea    0xda3(%rip),%rdi
call   1050 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"SENDing Erase Flash: \00"
@.str.1 = [12 x i8] c"RECEIVING: \00"
@.str.2 = [41 x i8] c"[ERROR: ACK for erase flash incomplete]\0A\00"
@.str.3 = [36 x i8] c"[OK => Erase Flash Program Memory]\0A\00"
define void @eraseflash(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%7 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %4
%9 = call i32 @sendSTX(i32 %8)
%10 = load i32, i32* %4
%11 = call i32 @sendSTX(i32 %10)
%12 = load i32, i32* %4
%13 = call i32 @senddata(i32 %12, i64 3)
%14 = load i32, i32* %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = call i32 @senddata(i32 %14, i64 %16)
%18 = load i32, i32* %4
%19 = load i64, i64* %5
%20 = and i64 %19, 255
%21 = call i32 @senddata(i32 %18, i64 %20)
%22 = load i32, i32* %4
%23 = load i64, i64* %5
%24 = ashr i64 %23, 8
%25 = and i64 %24, 255
%26 = call i32 @senddata(i32 %22, i64 %25)
%27 = load i32, i32* %4
%28 = load i64, i64* %5
%29 = ashr i64 %28, 16
%30 = and i64 %29, 255
%31 = call i32 @senddata(i32 %27, i64 %30)
%32 = load i32, i32* %4
%33 = call i32 @sendchecksum(i32 %32)
%34 = load i32, i32* %4
%35 = call i32 @sendETX(i32 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* %4
%38 = call i32 @expectSTX(i32 %37)
%39 = load i32, i32* %4
%40 = call i32 @expectSTX(i32 %39)
%41 = load i32, i32* %4
%42 = call i32 @expectPacketType(i32 %41, i32 3)
%43 = load i32, i32* %4
%44 = call i32 @expectCHKSUM(i32 %43)
%45 = load i32, i32* %4
%46 = call i32 @expectETX(i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %3
%49 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %4
%51 = call i32 @close(i32 %50)
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %3
%54 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"SENDing Erase Flash: \00"
@.str.1 = [12 x i8] c"RECEIVING: \00"
@str = [40 x i8] c"[ERROR: ACK for erase flash incomplete]\00"
@str.4 = [35 x i8] c"[OK => Erase Flash Program Memory]\00"
define void @eraseflash(i32 %0, i64 %1, i32 %2) {
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @sendSTX(i32 %0)
%6 = tail call i32 @sendSTX(i32 %0)
%7 = tail call i32 @senddata(i32 %0, i64 3)
%8 = sext i32 %2 to i64
%9 = tail call i32 @senddata(i32 %0, i64 %8)
%10 = and i64 %1, 255
%11 = tail call i32 @senddata(i32 %0, i64 %10)
%12 = lshr i64 %1, 8
%13 = and i64 %12, 255
%14 = tail call i32 @senddata(i32 %0, i64 %13)
%15 = lshr i64 %1, 16
%16 = and i64 %15, 255
%17 = tail call i32 @senddata(i32 %0, i64 %16)
%18 = tail call i32 @sendchecksum(i32 %0)
%19 = tail call i32 @sendETX(i32 %0)
%20 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @expectSTX(i32 %0)
%22 = tail call i32 @expectSTX(i32 %0)
%23 = tail call i32 @expectPacketType(i32 %0, i32 3)
%24 = tail call i32 @expectCHKSUM(i32 %0)
%25 = tail call i32 @expectETX(i32 %0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %3
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%29 = tail call i32 @close(i32 %0)
%30 = tail call i32 @exit(i32 -1)
unreachable
31: ; preds = %3
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.4, i64 0, i64 0))
ret void
}