#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 */
unsigned int checksum32(unsigned char *, int);
int checksum_page(int, int, unsigned int *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ write_page(int, unsigned char *, int);
int write_page_with_verify(int f, unsigned char *data, int page) {
  unsigned int csumhere, csumthere;
  csumhere = checksum32(data, 0x8000);
  write_page(f, data, page);
  if (checksum_page(f, page, &csumthere) != 1) {
    printf("ERROR: Checksum command failed at page %i\n", page);
    return 0;
  }
  if (csumhere != csumthere) {
    printf("ERROR: Checksum mismatch after write: page=%i buffer=%08x, "
           "device=%08x\n",
           page, csumhere, csumthere);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<write_page_with_verify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x8000,%esi
call   1050 <checksum32@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write_page@plt>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1060 <checksum_page@plt>
cmp    $0x1,%eax
je     11a7 <write_page_with_verify+0x67>
mov    -0x14(%rbp),%esi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dd <write_page_with_verify+0x9d>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11d6 <write_page_with_verify+0x96>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dd <write_page_with_verify+0x9d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_page_with_verify>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%ebx
mov    %rsi,%rdi
mov    $0x8000,%esi
call   1050 <checksum32@plt>
mov    %eax,%r14d
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1040 <write_page@plt>
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
mov    %r15d,%esi
call   1060 <checksum_page@plt>
cmp    $0x1,%eax
jne    11a6 <write_page_with_verify+0x66>
mov    0x4(%rsp),%ecx
mov    $0x1,%ebp
cmp    %ecx,%r14d
je     11b9 <write_page_with_verify+0x79>
lea    0xe96(%rip),%rdi
xor    %ebp,%ebp
mov    %r15d,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    11b9 <write_page_with_verify+0x79>
lea    0xe53(%rip),%rdi
xor    %ebp,%ebp
mov    %r15d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"ERROR: Checksum command failed at page %i\0A\00"
@.str.1 = [72 x i8] c"ERROR: Checksum mismatch after write: page=%i buffer=%08x, device=%08x\0A\00"
define i32 @write_page_with_verify(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = call i32 @checksum32(i8* %10, i32 32768)
store i32 %11, i32* %8
%12 = load i32, i32* %5
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = call i32 @write_page(i32 %12, i8* %13, i32 %14)
%16 = load i32, i32* %5
%17 = load i32, i32* %7
%18 = call i32 @checksum_page(i32 %16, i32 %17, i32* %9)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = load i32, i32* %7
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 0, i32* %4
br label %33
23: ; preds = %3
%24 = load i32, i32* %8
%25 = load i32, i32* %9
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = load i32, i32* %9
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29, i32 %30)
store i32 0, i32* %4
br label %33
32: ; preds = %23
store i32 1, i32* %4
br label %33
33: ; preds = %32, %27, %20
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [43 x i8] c"ERROR: Checksum command failed at page %i\0A\00"
@.str.1 = [72 x i8] c"ERROR: Checksum mismatch after write: page=%i buffer=%08x, device=%08x\0A\00"
define i32 @write_page_with_verify(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 @checksum32(i8* %1, i32 32768)
%7 = tail call i32 @write_page(i32 %0, i8* %1, i32 %2)
%8 = call i32 @checksum_page(i32 %0, i32 %2, i32* %4)
%9 = icmp eq i32 %8, 1
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %17
12: ; preds = %3
%13 = load i32, i32* %4
%14 = icmp eq i32 %6, %13
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %6, i32 %13)
br label %17
17: ; preds = %12, %15, %10
%18 = phi i32 [ 0, %10 ], [ 0, %15 ], [ 1, %12 ]
ret i32 %18
}