#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
unsigned int fread(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror_exit(char const *);
int /*<<< orphan*/ printf(char *, ...);
unsigned char *xmalloc(unsigned int);
void read_file(const char *filename, unsigned char **buf, unsigned *size) {
  FILE *f;
  unsigned rdsize;
  unsigned bufsize;
  f = fopen(filename, "r");
  if (!f)
    perror_exit(filename);
  fseek(f, 0L, SEEK_END);
  bufsize = rdsize = ftell(f);
  if (*size > rdsize) {
    bufsize = *size;
  }
  if (bufsize & 1)
    bufsize += 1;
  fseek(f, 0L, SEEK_SET);
  *buf = xmalloc(bufsize);
  if (rdsize != fread(*buf, 1, rdsize, f)) {
    printf("%s: couldn't read whole file\n", filename);
    exit(1);
  }
  fclose(f);
  printf("%s: %d bytes", filename, rdsize);
  if (rdsize != bufsize) {
    printf(" (%d bytes buffer)", bufsize);
  }
  printf("\n");
  *size = rdsize;
  if (*size & 1)
    *size += 1;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cc <read_file+0x3c>
mov    -0x8(%rbp),%rdi
call   10a0 <perror_exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x24(%rbp),%eax
jbe    1209 <read_file+0x79>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1221 <read_file+0x91>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%edi
call   1060 <xmalloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
je     1290 <read_file+0x100>
mov    -0x8(%rbp),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     12cb <read_file+0x13b>
mov    -0x28(%rbp),%esi
lea    0xd69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd4c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12ff <read_file+0x16f>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r15
lea    0xe45(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11d0 <read_file+0x30>
mov    %r15,%rdi
call   10b0 <perror_exit@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
mov    (%r14),%eax
cmp    %ebp,%eax
cmovbe %ebp,%eax
mov    %eax,%ebx
and    $0x1,%ebx
add    %eax,%ebx
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %ebx,%edi
call   1070 <xmalloc@plt>
mov    %rax,0x0(%r13)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    %eax,%ebp
jne    1282 <read_file+0xe2>
mov    %r12,%rdi
call   1050 <fclose@plt>
lea    0xde1(%rip),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %ebx,%ebp
je     125f <read_file+0xbf>
lea    0xdd7(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebp,%eax
and    $0x1,%eax
add    %ebp,%eax
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd79(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [30 x i8] c"%s: couldn't read whole file\0A\00"
@.str.2 = [13 x i8] c"%s: %d bytes\00"
@.str.3 = [19 x i8] c" (%d bytes buffer)\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @read_file(i8* %0, i8** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 @perror_exit(i8* %15)
br label %17
17: ; preds = %14, %3
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
%21 = load i32*, i32** %7
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %8
store i32 %22, i32* %9
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = load i32, i32* %8
%26 = icmp ugt i32 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = load i32*, i32** %6
%29 = load i32, i32* %28
store i32 %29, i32* %9
br label %30
30: ; preds = %27, %17
%31 = load i32, i32* %9
%32 = and i32 %31, 1
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* %9
%36 = add i32 %35, 1
store i32 %36, i32* %9
br label %37
37: ; preds = %34, %30
%38 = load i32*, i32** %7
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @fseek(i32* %38, i64 0, i32 %39)
%41 = load i32, i32* %9
%42 = call i8* @xmalloc(i32 %41)
%43 = load i8**, i8*** %5
store i8* %42, i8** %43
%44 = load i32, i32* %8
%45 = load i8**, i8*** %5
%46 = load i8*, i8** %45
%47 = load i32, i32* %8
%48 = load i32*, i32** %7
%49 = call i32 @fread(i8* %46, i32 1, i32 %47, i32* %48)
%50 = icmp ne i32 %44, %49
br i1 %50, label %51, label %55
51: ; preds = %37
%52 = load i8*, i8** %4
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %37
%56 = load i32*, i32** %7
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %4
%59 = load i32, i32* %8
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %58, i32 %59)
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = icmp ne i32 %61, %62
br i1 %63, label %64, label %67
64: ; preds = %55
%65 = load i32, i32* %9
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %64, %55
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* %8
%70 = load i32*, i32** %6
store i32 %69, i32* %70
%71 = load i32*, i32** %6
%72 = load i32, i32* %71
%73 = and i32 %72, 1
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %67
%76 = load i32*, i32** %6
%77 = load i32, i32* %76
%78 = add i32 %77, 1
store i32 %78, i32* %76
br label %79
79: ; preds = %75, %67
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [30 x i8] c"%s: couldn't read whole file\0A\00"
@.str.2 = [13 x i8] c"%s: %d bytes\00"
@.str.3 = [19 x i8] c" (%d bytes buffer)\00"
define void @read_file(i8* %0, i8** nocapture writeonly %1, i32* nocapture %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_exit(i8* %0)
br label %8
8: ; preds = %6, %3
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %4, i64 0, i32 %9)
%11 = tail call i32 @ftell(i32* %4)
%12 = load i32, i32* %2
%13 = icmp ugt i32 %12, %11
%14 = select i1 %13, i32 %12, i32 %11
%15 = and i32 %14, 1
%16 = add i32 %15, %14
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @fseek(i32* %4, i64 0, i32 %17)
%19 = tail call i8* @xmalloc(i32 %16)
store i8* %19, i8** %1
%20 = tail call i32 @fread(i8* %19, i32 1, i32 %11, i32* %4)
%21 = icmp eq i32 %11, %20
br i1 %21, label %25, label %22
22: ; preds = %8
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %8
%26 = tail call i32 @fclose(i32* %4)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %11)
%28 = icmp eq i32 %11, %16
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %31
31: ; preds = %29, %25
%32 = tail call i32 @putchar(i32 10)
%33 = and i32 %11, 1
%34 = add i32 %33, %11
store i32 %34, i32* %2
ret void
}