#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int sendcmd(int, int, int, int, int /*<<< orphan*/);
int write_data(int, unsigned char *, int);
int write_page(int f, unsigned char *data, int p) {
  ssize_t wrote_bytes;
  if (sendcmd(f, 3, (p & 0xFF00) | (((p & 0xFF) - 2) & 0xFF), 0x8000, 0) != 1) {
    printf("ERROR: Write command sending failed.");
    return 0;
  }
  wrote_bytes = write_data(f, data, 0x8000);
  if (wrote_bytes != 0x8000) {
    printf("ERROR: Write for page %02x returned %i.\n", p, (int)wrote_bytes);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<write_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%edx
and    $0xff00,%edx
mov    -0x14(%rbp),%eax
and    $0xff,%eax
sub    $0x2,%eax
and    $0xff,%eax
or     %eax,%edx
mov    $0x3,%esi
mov    $0x8000,%ecx
xor    %r8d,%r8d
call   1050 <sendcmd@plt>
cmp    $0x1,%eax
je     1195 <write_page+0x65>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dd <write_page+0xad>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x8000,%edx
call   1040 <write_data@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x8000,-0x18(%rbp)
je     11d6 <write_page+0xa6>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dd <write_page+0xad>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_page>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%eax
and    $0xff00,%eax
lea    0xfe(%rbx),%ecx
movzbl %cl,%edx
or     %eax,%edx
xor    %r15d,%r15d
mov    $0x3,%esi
mov    $0x8000,%ecx
xor    %r8d,%r8d
call   1050 <sendcmd@plt>
cmp    $0x1,%eax
jne    119d <write_page+0x6d>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x8000,%edx
call   1040 <write_data@plt>
mov    $0x1,%r15d
cmp    $0x8000,%eax
je     11ab <write_page+0x7b>
lea    0xe98(%rip),%rdi
xor    %r15d,%r15d
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11ab <write_page+0x7b>
lea    0xe5c(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"ERROR: Write command sending failed.\00"
@.str.1 = [41 x i8] c"ERROR: Write for page %02x returned %i.\0A\00"
define i32 @write_page(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* %7
%11 = and i32 %10, 65280
%12 = load i32, i32* %7
%13 = and i32 %12, 255
%14 = sub nsw i32 %13, 2
%15 = and i32 %14, 255
%16 = or i32 %11, %15
%17 = call i32 @sendcmd(i32 %9, i32 3, i32 %16, i32 32768, i32 0)
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %32
21: ; preds = %3
%22 = load i32, i32* %5
%23 = load i8*, i8** %6
%24 = call i32 @write_data(i32 %22, i8* %23, i32 32768)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 32768
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
store i32 0, i32* %4
br label %32
31: ; preds = %21
store i32 1, i32* %4
br label %32
32: ; preds = %31, %27, %19
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [37 x i8] c"ERROR: Write command sending failed.\00"
@.str.1 = [41 x i8] c"ERROR: Write for page %02x returned %i.\0A\00"
define i32 @write_page(i32 %0, i8* %1, i32 %2) {
%4 = and i32 %2, 65280
%5 = add i32 %2, 254
%6 = and i32 %5, 255
%7 = or i32 %6, %4
%8 = tail call i32 @sendcmd(i32 %0, i32 3, i32 %7, i32 32768, i32 0)
%9 = icmp eq i32 %8, 1
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %3
%13 = tail call i32 @write_data(i32 %0, i8* %1, i32 32768)
%14 = icmp eq i32 %13, 32768
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %13)
br label %17
17: ; preds = %12, %15, %10
%18 = phi i32 [ 0, %10 ], [ 0, %15 ], [ 1, %12 ]
ret i32 %18
}