#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 */
typedef int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ do_instruction(int, int *, int *);
int /*<<< orphan*/ print_err(char *);
int /*<<< orphan*/ usleep(int);
int write_program_page(int spidev, unsigned int addr) {
  uint8_t tx[4], rx[4];
  int i;
  tx[0] = 0x4c;
  tx[1] = addr >> 9;
  tx[2] = addr >> 1;
  tx[3] = 0;
  if (!do_instruction(spidev, tx, rx))
    return 0;
  tx[0] = 0xf0;
  tx[1] = tx[2] = tx[3] = 0;
  for (i = 0; i < 100; i++) {
    usleep(1000);
    if (!do_instruction(spidev, tx, rx))
      return 0;
    if (!(rx[3] & 1))
      return 1;
  }
  print_err("Time out waiting for program memory page write to complete");
  return 0;
}


x86 assembly (clang, -O0):
<write_program_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x4c,-0x20(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x9,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <do_instruction@plt>
cmp    $0x0,%eax
jne    1183 <write_program_page+0x53>
movl   $0x0,-0x4(%rbp)
jmp    1220 <write_program_page+0xf0>
movl   $0xf0,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x64,-0x34(%rbp)
jge    120d <write_program_page+0xdd>
mov    $0x3e8,%edi
call   1050 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <do_instruction@plt>
cmp    $0x0,%eax
jne    11df <write_program_page+0xaf>
movl   $0x0,-0x4(%rbp)
jmp    1220 <write_program_page+0xf0>
mov    -0x24(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11fa <write_program_page+0xca>
movl   $0x1,-0x4(%rbp)
jmp    1220 <write_program_page+0xf0>
jmp    11ff <write_program_page+0xcf>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a6 <write_program_page+0x76>
lea    0xdec(%rip),%rdi
call   1030 <print_err@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_program_page>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
movl   $0x4c,(%rsp)
mov    %esi,%eax
shr    $0x9,%eax
mov    %eax,0x4(%rsp)
shr    %esi
mov    %esi,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
call   1040 <do_instruction@plt>
xor    %r14d,%r14d
test   %eax,%eax
je     11cb <write_program_page+0x9b>
movaps 0xe89(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    $0x64,%ebp
mov    %rsp,%r15
lea    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1050 <usleep@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1040 <do_instruction@plt>
test   %eax,%eax
je     11cb <write_program_page+0x9b>
testb  $0x1,0x1c(%rsp)
je     11c5 <write_program_page+0x95>
add    $0xffffffff,%ebp
jne    1190 <write_program_page+0x60>
lea    0xe52(%rip),%rdi
call   1030 <print_err@plt>
jmp    11cb <write_program_page+0x9b>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [59 x i8] c"Time out waiting for program memory page write to complete\00"
define i32 @write_program_page(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x i32]
%7 = alloca [4 x i32]
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
store i32 76, i32* %9
%10 = load i32, i32* %5
%11 = lshr i32 %10, 9
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 1
store i32 %11, i32* %12
%13 = load i32, i32* %5
%14 = lshr i32 %13, 1
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 2
store i32 %14, i32* %15
%16 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 3
store i32 0, i32* %16
%17 = load i32, i32* %4
%18 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 0
%20 = call i32 @do_instruction(i32 %17, i32* %18, i32* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %2
store i32 0, i32* %3
br label %51
23: ; preds = %2
%24 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
store i32 240, i32* %24
%25 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 3
store i32 0, i32* %25
%26 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 2
store i32 0, i32* %26
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 1
store i32 0, i32* %27
store i32 0, i32* %8
br label %28
28: ; preds = %46, %23
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 100
br i1 %30, label %31, label %49
31: ; preds = %28
%32 = call i32 @usleep(i32 1000)
%33 = load i32, i32* %4
%34 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%35 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 0
%36 = call i32 @do_instruction(i32 %33, i32* %34, i32* %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %31
store i32 0, i32* %3
br label %51
39: ; preds = %31
%40 = getelementptr inbounds [4 x i32], [4 x i32]* %7, i64 0, i64 3
%41 = load i32, i32* %40
%42 = and i32 %41, 1
%43 = icmp ne i32 %42, 0
br i1 %43, label %45, label %44
44: ; preds = %39
store i32 1, i32* %3
br label %51
45: ; preds = %39
br label %46
46: ; preds = %45
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %28
49: ; preds = %28
%50 = call i32 @print_err(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %51
51: ; preds = %49, %44, %38, %22
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [59 x i8] c"Time out waiting for program memory page write to complete\00"
define i32 @write_program_page(i32 %0, i32 %1) {
%3 = alloca <4 x i32>
%4 = alloca [4 x i32]
%5 = bitcast <4 x i32>* %3 to i8*
%6 = bitcast [4 x i32]* %4 to i8*
%7 = getelementptr inbounds <4 x i32>, <4 x i32>* %3, i64 0, i64 0
store i32 76, i32* %7
%8 = lshr i32 %1, 9
%9 = getelementptr inbounds <4 x i32>, <4 x i32>* %3, i64 0, i64 1
store i32 %8, i32* %9
%10 = lshr i32 %1, 1
%11 = getelementptr inbounds <4 x i32>, <4 x i32>* %3, i64 0, i64 2
store i32 %10, i32* %11
%12 = getelementptr inbounds <4 x i32>, <4 x i32>* %3, i64 0, i64 3
store i32 0, i32* %12
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%14 = call i32 @do_instruction(i32 %0, i32* %7, i32* %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %32, label %16
16: ; preds = %2
store <4 x i32> <i32 240, i32 0, i32 0, i32 0>, <4 x i32>* %3
%17 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
br label %21
18: ; preds = %26
%19 = add nuw nsw i32 %22, 1
%20 = icmp eq i32 %19, 100
br i1 %20, label %30, label %21
21: ; preds = %16, %18
%22 = phi i32 [ 0, %16 ], [ %19, %18 ]
%23 = call i32 @usleep(i32 1000)
%24 = call i32 @do_instruction(i32 %0, i32* %7, i32* %13)
%25 = icmp eq i32 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %21
%27 = load i32, i32* %17
%28 = and i32 %27, 1
%29 = icmp eq i32 %28, 0
br i1 %29, label %32, label %18
30: ; preds = %18
%31 = call i32 @print_err(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %26, %21, %2, %30
%33 = phi i32 [ 0, %30 ], [ 0, %2 ], [ 1, %26 ], [ 0, %21 ]
ret i32 %33
}