#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fwrite(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char const *);
int write_page2_to_file(const char *path, void *pg, int size) {
  FILE *f;
  f = fopen(path, "w");
  if (!f) {
    perror(path);
    return -1;
  }
  if (fwrite(pg, size, 1, f) != 1) {
    perror(path);
    fclose(f);
    return -2;
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<write_page2_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1187 <write_page2_to_file+0x47>
mov    -0x10(%rbp),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <write_page2_to_file+0x93>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
je     11c3 <write_page2_to_file+0x83>
mov    -0x10(%rbp),%rdi
call   1050 <perror@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d3 <write_page2_to_file+0x93>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_page2_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0xea9(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1191 <write_page2_to_file+0x51>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
cmp    $0x1,%eax
jne    11a0 <write_page2_to_file+0x60>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
jmp    1186 <write_page2_to_file+0x46>
mov    %r14,%rdi
call   1050 <perror@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0xfffffffe,%eax
jmp    1186 <write_page2_to_file+0x46>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
define i32 @write_page2_to_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @perror(i8* %14)
store i32 -1, i32* %4
br label %30
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = load i32*, i32** %8
%20 = call i32 @fwrite(i8* %17, i32 %18, i32 1, i32* %19)
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i8*, i8** %5
%24 = call i32 @perror(i8* %23)
%25 = load i32*, i32** %8
%26 = call i32 @fclose(i32* %25)
store i32 -2, i32* %4
br label %30
27: ; preds = %16
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
store i32 0, i32* %4
br label %30
30: ; preds = %27, %22, %13
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
define i32 @write_page2_to_file(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* %0)
br label %16
8: ; preds = %3
%9 = tail call i32 @fwrite(i8* %1, i32 %2, i32 1, i32* %4)
%10 = icmp eq i32 %9, 1
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 @perror(i8* %0)
%13 = tail call i32 @fclose(i32* %4)
br label %16
14: ; preds = %8
%15 = tail call i32 @fclose(i32* %4)
br label %16
16: ; preds = %14, %11, %6
%17 = phi i32 [ -2, %11 ], [ 0, %14 ], [ -1, %6 ]
ret i32 %17
}