#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(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char, char, char);
void readWriteBytes(void) {
  unsigned char bytes[5] = {'a', '*', 0x0F, 10, 255};
  FILE *fp = fopen("fileio-binary.temp", "wb");
  if (fp == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  for (int i = 0; i < 5; i++) {
    fwrite(&bytes[i], 1, 1, fp);
    printf("Wrote '%d' to file which is character '%c' and hex 0x%x\n",
           bytes[i], bytes[i], bytes[i]);
  }
  fclose(fp);
  fp = fopen("fileio-binary.temp", "r");
  if (fp == NULL) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }
  char c = '?';
  for (int i = 0; i < 5; i++) {
    fread(&c, 1, 1, fp);
    printf("Read '%d' from file which is character '%c' and hex 0x%x\n", c, c,
           c);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<readWriteBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0xe82(%rip),%eax
mov    %eax,-0x5(%rbp)
mov    0xe7d(%rip),%al
mov    %al,-0x1(%rbp)
lea    0xe74(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c6 <readWriteBytes+0x56>
lea    0xe68(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    122b <readWriteBytes+0xbb>
movslq -0x14(%rbp),%rax
lea    -0x5(%rbp),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
movslq -0x14(%rbp),%rax
mov    -0x5(%rbp,%rax,1),%dl
movslq -0x14(%rbp),%rax
mov    -0x5(%rbp,%rax,1),%cl
movslq -0x14(%rbp),%rax
lea    0xe14(%rip),%rdi
movsbl %dl,%esi
movsbl %cl,%edx
movsbl -0x5(%rbp,%rax,1),%ecx
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cd <readWriteBytes+0x5d>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdca(%rip),%rdi
lea    0xe18(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1270 <readWriteBytes+0x100>
lea    0xdbe(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movb   $0x3f,-0x15(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x5,-0x1c(%rbp)
jge    12c3 <readWriteBytes+0x153>
mov    -0x10(%rbp),%rcx
lea    -0x15(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x15(%rbp),%cl
mov    -0x15(%rbp),%al
lea    0xdb6(%rip),%rdi
movsbl %cl,%esi
movsbl %al,%edx
movsbl -0x15(%rbp),%ecx
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    127b <readWriteBytes+0x10b>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readWriteBytes>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe80(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     135a <readWriteBytes+0x1ea>
mov    %rax,%rbx
mov    $0x61,%edi
mov    %rax,%rsi
call   1060 <fputc@plt>
lea    0xe70(%rip),%r14
mov    %r14,%rdi
mov    $0x61,%esi
mov    $0x61,%edx
mov    $0x61,%ecx
call   1050 <printf@plt>
mov    $0x2a,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
mov    $0x2a,%esi
mov    $0x2a,%edx
mov    $0x2a,%ecx
call   1050 <printf@plt>
mov    $0xf,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
mov    $0xf,%esi
mov    $0xf,%edx
mov    $0xf,%ecx
call   1050 <printf@plt>
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
mov    $0xa,%esi
mov    $0xa,%edx
mov    $0xa,%ecx
call   1050 <printf@plt>
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
mov    $0xffffffff,%esi
mov    $0xffffffff,%edx
mov    $0xffffffff,%ecx
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xd9e(%rip),%rdi
lea    0xdec(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     135a <readWriteBytes+0x1ea>
mov    %rax,%rbx
movb   $0x3f,0xf(%rsp)
lea    0xf(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
movsbl 0xf(%rsp),%ecx
lea    0xdb2(%rip),%r15
mov    %r15,%rdi
mov    %ecx,%esi
mov    %ecx,%edx
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
movsbl 0xf(%rsp),%ecx
mov    %r15,%rdi
mov    %ecx,%esi
mov    %ecx,%edx
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
movsbl 0xf(%rsp),%ecx
mov    %r15,%rdi
mov    %ecx,%esi
mov    %ecx,%edx
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
movsbl 0xf(%rsp),%ecx
mov    %r15,%rdi
mov    %ecx,%esi
mov    %ecx,%edx
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
movsbl 0xf(%rsp),%ecx
mov    %r15,%rdi
mov    %ecx,%esi
mov    %ecx,%edx
call   1050 <printf@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
lea    0xcb5(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.readWriteBytes.bytes = [5 x i8] c"a*\0F\0A\FF"
@.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 = [57 x i8] c"Wrote '%d' to file which is character '%c' and hex 0x%x\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [58 x i8] c"Read '%d' from file which is character '%c' and hex 0x%x\0A\00"
define void @readWriteBytes() {
%1 = alloca [5 x i8]
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = bitcast [5 x i8]* %1 to i8*
%7 = 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* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
store i32 0, i32* %3
br label %15
15: ; preds = %37, %14
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 5
br i1 %17, label %18, label %40
18: ; preds = %15
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 %20
%22 = load i32*, i32** %2
%23 = call i32 @fwrite(i8* %21, i32 1, i32 1, i32* %22)
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = load i32, i32* %3
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 %33
%35 = load i8, i8* %34
%36 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8 signext %27, i8 signext %31, i8 signext %35)
br label %37
37: ; preds = %18
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
br label %15
40: ; preds = %15
%41 = load i32*, i32** %2
%42 = call i32 @fclose(i32* %41)
%43 = 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* %43, i32** %2
%44 = load i32*, i32** %2
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40
store i8 63, i8* %4
store i32 0, i32* %5
br label %51
51: ; preds = %61, %50
%52 = load i32, i32* %5
%53 = icmp slt i32 %52, 5
br i1 %53, label %54, label %64
54: ; preds = %51
%55 = load i32*, i32** %2
%56 = call i32 @fread(i8* %4, i32 1, i32 1, i32* %55)
%57 = load i8, i8* %4
%58 = load i8, i8* %4
%59 = load i8, i8* %4
%60 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8 signext %57, i8 signext %58, i8 signext %59)
br label %61
61: ; preds = %54
%62 = load i32, i32* %5
%63 = add nsw i32 %62, 1
store i32 %63, i32* %5
br label %51
64: ; preds = %51
%65 = load i32*, i32** %2
%66 = call i32 @fclose(i32* %65)
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 = [57 x i8] c"Wrote '%d' to file which is character '%c' and hex 0x%x\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [58 x i8] c"Read '%d' from file which is character '%c' and hex 0x%x\0A\00"
define void @readWriteBytes() {
%1 = alloca i8
%2 = 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))
%3 = icmp eq i32* %2, null
br i1 %3, label %18, label %4
4: ; preds = %0
%5 = tail call i32 @fputc(i32 97, i32* %2)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8 signext 97, i8 signext 97, i8 signext 97)
%7 = tail call i32 @fputc(i32 42, i32* %2)
%8 = tail call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8 signext 42, i8 signext 42, i8 signext 42)
%9 = tail call i32 @fputc(i32 15, i32* %2)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8 signext 15, i8 signext 15, i8 signext 15)
%11 = tail call i32 @fputc(i32 10, i32* %2)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8 signext 10, i8 signext 10, i8 signext 10)
%13 = tail call i32 @fputc(i32 -1, i32* %2)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.3, i64 0, i64 0), i8 signext -1, i8 signext -1, i8 signext -1)
%15 = tail call i32 @fclose(i32* %2)
%16 = 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))
%17 = icmp eq i32* %16, null
br i1 %17, label %22, label %26
18: ; preds = %0
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %4
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %4
store i8 63, i8* %1
%27 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %16)
%28 = load i8, i8* %1
%29 = tail call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8 signext %28, i8 signext %28, i8 signext %28)
%30 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %16)
%31 = load i8, i8* %1
%32 = tail call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8 signext %31, i8 signext %31, i8 signext %31)
%33 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %16)
%34 = load i8, i8* %1
%35 = tail call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8 signext %34, i8 signext %34, i8 signext %34)
%36 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %16)
%37 = load i8, i8* %1
%38 = tail call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8 signext %37, i8 signext %37, i8 signext %37)
%39 = call i32 @fread(i8* %1, i32 1, i32 1, i32* %16)
%40 = load i8, i8* %1
%41 = tail call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0), i8 signext %40, i8 signext %40, i8 signext %40)
%42 = tail call i32 @fclose(i32* %16)
ret void
}