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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, unsigned long long);
int mem_page_write(FILE *f, uint64_t addr, uint8_t *page) {
  if (addr >= 0xe0000000) {
    addr += 0x20000000;
  }
  if (fseek(f, addr, SEEK_SET)) {
    printf("Failed to seek to address 0x%016llx in output file\n",
           (unsigned long long)addr);
    return -1;
  }
  if (fwrite(page, 4096, 1, f) != 1) {
    printf("Failed to write page at address 0x%016llx in output "
           "file\n",
           (unsigned long long)addr);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mem_page_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
cmpl   $0xe0000000,-0x14(%rbp)
jb     115b <mem_page_write+0x2b>
mov    -0x14(%rbp),%eax
add    $0x20000000,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1040 <fseek@plt>
cmp    $0x0,%rax
je     1196 <mem_page_write+0x66>
movslq -0x14(%rbp),%rsi
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <mem_page_write+0xa9>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1000,%esi
mov    $0x1,%edx
call   1050 <fwrite@plt>
cmp    $0x1,%eax
je     11d2 <mem_page_write+0xa2>
movslq -0x14(%rbp),%rsi
lea    0xe73(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <mem_page_write+0xa9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_page_write>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rdi,%r14
lea    0x20000000(%rsi),%ebx
cmp    $0xe0000000,%esi
cmovb  %esi,%ebx
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%esi
call   1040 <fseek@plt>
test   %rax,%rax
je     1168 <mem_page_write+0x38>
lea    0xe9a(%rip),%rdi
jmp    118d <mem_page_write+0x5d>
mov    %r15,%rdi
mov    $0x1000,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
je     119a <mem_page_write+0x6a>
lea    0xea7(%rip),%rdi
movslq %ebx,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [52 x i8] c"Failed to seek to address 0x%016llx in output file\0A\00"
@.str.1 = [58 x i8] c"Failed to write page at address 0x%016llx in output file\0A\00"
define i32 @mem_page_write(i32* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%8 = load i32, i32* %6
%9 = icmp uge i32 %8, -536870912
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 536870912
store i32 %12, i32* %6
br label %13
13: ; preds = %10, %3
%14 = load i32*, i32** %5
%15 = load i32, i32* %6
%16 = load i32, i32* @SEEK_SET
%17 = call i64 @fseek(i32* %14, i32 %15, i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i64 %21)
store i32 -1, i32* %4
br label %33
23: ; preds = %13
%24 = load i32*, i32** %7
%25 = load i32*, i32** %5
%26 = call i32 @fwrite(i32* %24, i32 4096, i32 1, i32* %25)
%27 = icmp ne i32 %26, 1
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 %30)
store i32 -1, i32* %4
br label %33
32: ; preds = %23
store i32 0, i32* %4
br label %33
33: ; preds = %32, %28, %19
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [52 x i8] c"Failed to seek to address 0x%016llx in output file\0A\00"
@.str.1 = [58 x i8] c"Failed to write page at address 0x%016llx in output file\0A\00"
define i32 @mem_page_write(i32* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = icmp ugt i32 %1, -536870913
%5 = add nsw i32 %1, 536870912
%6 = select i1 %4, i32 %5, i32 %1
%7 = load i32, i32* @SEEK_SET
%8 = tail call i64 @fseek(i32* %0, i32 %6, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = tail call i32 @fwrite(i32* %2, i32 4096, i32 1, i32* %0)
%12 = icmp eq i32 %11, 1
br i1 %12, label %17, label %13
13: ; preds = %10, %3
%14 = phi i8* [ getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), %10 ]
%15 = sext i32 %6 to i64
%16 = tail call i32 @printf(i8* %14, i64 %15)
br label %17
17: ; preds = %13, %10
%18 = phi i32 [ 0, %10 ], [ -1, %13 ]
ret i32 %18
}