#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void readWriteInts(void) {
  FILE *fp = fopen("fileio-binary.temp", "wb");
  if (fp == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  int i = (int)getpid();
  fwrite(&i, sizeof(int), 1, fp);
  printf("Wrote %d to file\n", i);
  fclose(fp);
  fp = fopen("fileio-binary.temp", "r");
  if (fp == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  int newI;
  fread(&newI, sizeof(int), 1, fp);
  printf("Read %d from file\n", newI);
  fclose(fp);
}


x86 assembly (clang, -O0):
<readWriteInts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe71(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11c4 <readWriteInts+0x44>
lea    0xe65(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0xc(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0xc(%rbp),%esi
lea    0xe2d(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xdfc(%rip),%rdi
lea    0xe23(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1239 <readWriteInts+0xb9>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rcx
lea    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%esi
lea    0xdd6(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readWriteInts>:
push   %rbx
sub    $0x10,%rsp
lea    0xe74(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     122d <readWriteInts+0xad>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    0x8(%rsp),%esi
lea    0xe4b(%rip),%rdi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe1b(%rip),%rdi
lea    0xe42(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     122d <readWriteInts+0xad>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    0xc(%rsp),%esi
lea    0xe15(%rip),%rdi
call   1060 <printf@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1050 <fclose@plt>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"fileio-binary.temp\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [18 x i8] c"Wrote %d to file\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [19 x i8] c"Read %d from file\0A\00"
define void @readWriteInts() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = call i64 (...) @getpid()
%13 = trunc i64 %12 to i32
store i32 %13, i32* %2
%14 = load i32*, i32** %1
%15 = call i32 @fwrite(i32* %2, i32 4, i32 1, i32* %14)
%16 = load i32, i32* %2
%17 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = load i32*, i32** %1
%19 = call i32 @fclose(i32* %18)
%20 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %20, i32** %1
%21 = load i32*, i32** %1
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %11
%24 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %11
%28 = load i32*, i32** %1
%29 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %28)
%30 = load i32, i32* %3
%31 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = load i32*, i32** %1
%33 = call i32 @fclose(i32* %32)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"fileio-binary.temp\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [6 x i8] c"fopen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [18 x i8] c"Wrote %d to file\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [19 x i8] c"Read %d from file\0A\00"
define void @readWriteInts() {
%1 = alloca i32
%2 = alloca i32
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = bitcast i32* %1 to i8*
%11 = tail call i64 (...) @getpid()
%12 = trunc i64 %11 to i32
store i32 %12, i32* %1
%13 = call i32 @fwrite(i32* %1, i32 4, i32 1, i32* %3)
%14 = load i32, i32* %1
%15 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i32 @fclose(i32* %3)
%17 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %9
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %9
%24 = bitcast i32* %2 to i8*
%25 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %17)
%26 = load i32, i32* %2
%27 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = tail call i32 @fclose(i32* %17)
ret void
}