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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ sscanf(char *, char *, int /*<<< orphan*/ *);
int read_file(char *filename, uint8_t *buffer, int maxlen) {
  int size, read;
  FILE *handler = fopen(filename, "r");
  if (handler) {
    fseek(handler, 0, SEEK_END);
    size = ftell(handler);
    rewind(handler);
    if (size / 2 > maxlen) {
      printf("Buffer too small\n");
    }
    char *hex = (char *)malloc(size);
    read = fread(hex, 1, size, handler);
    fclose(handler);
    if (size != read) {
      printf("Failed to read file\n");
      free(hex);
      hex = NULL;
      return 0;
    }
    int i;
    for (i = 0; i < maxlen; i++) {
      sscanf(hex + 2 * i, "%2hhx", &buffer[i]);
    }
    return read;
  } else {
    printf("No such file\n");
    return 0;
  }
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12d3 <read_file+0x133>
mov    -0x30(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    -0x1c(%rbp),%eax
jle    121b <read_file+0x7b>
lea    0xdec(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     127d <read_file+0xdd>
lea    0xdb9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    12e6 <read_file+0x146>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12c8 <read_file+0x128>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rdx
movslq -0x3c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xd74(%rip),%rsi
call   10a0 <sscanf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1284 <read_file+0xe4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e6 <read_file+0x146>
lea    0xd55(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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    %edx,%r14d
mov    %rsi,%r13
lea    0xe48(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     126e <read_file+0xce>
mov    %rax,%r12
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1070 <rewind@plt>
mov    %r15d,%eax
shr    $0x1f,%eax
add    %r15d,%eax
sar    %eax
cmp    %r14d,%eax
jle    120b <read_file+0x6b>
lea    0xe23(%rip),%rdi
call   1040 <puts@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1060 <fclose@plt>
cmp    %ebx,%r15d
jne    127c <read_file+0xdc>
test   %r14d,%r14d
jle    1293 <read_file+0xf3>
mov    %r14d,%ebx
lea    0xdbb(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   10a0 <sscanf@plt>
add    $0x2,%rbp
add    $0x4,%r13
add    $0xffffffffffffffff,%rbx
jne    1250 <read_file+0xb0>
jmp    1293 <read_file+0xf3>
lea    0xd93(%rip),%rdi
call   1040 <puts@plt>
jmp    1290 <read_file+0xf0>
lea    0xd92(%rip),%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.1 = [18 x i8] c"Buffer too small\0A\00"
@.str.2 = [21 x i8] c"Failed to read file\0A\00"
@.str.3 = [6 x i8] c"%2hhx\00"
@.str.4 = [14 x i8] c"No such file\0A\00"
define i32 @read_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %69
17: ; preds = %3
%18 = load i32*, i32** %10
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %10
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %8
%23 = load i32*, i32** %10
%24 = call i32 @rewind(i32* %23)
%25 = load i32, i32* %8
%26 = sdiv i32 %25, 2
%27 = load i32, i32* %7
%28 = icmp sgt i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %17
%32 = load i32, i32* %8
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %11
%35 = load i8*, i8** %11
%36 = load i32, i32* %8
%37 = load i32*, i32** %10
%38 = call i32 @fread(i8* %35, i32 1, i32 %36, i32* %37)
store i32 %38, i32* %9
%39 = load i32*, i32** %10
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %8
%42 = load i32, i32* %9
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %31
%45 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%46 = load i8*, i8** %11
%47 = call i32 @free(i8* %46)
store i8* null, i8** %11
store i32 0, i32* %4
br label %71
48: ; preds = %31
store i32 0, i32* %12
br label %49
49: ; preds = %64, %48
%50 = load i32, i32* %12
%51 = load i32, i32* %7
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %67
53: ; preds = %49
%54 = load i8*, i8** %11
%55 = load i32, i32* %12
%56 = mul nsw i32 2, %55
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
%59 = load i32*, i32** %6
%60 = load i32, i32* %12
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %59, i64 %61
%63 = call i32 @sscanf(i8* %58, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %62)
br label %64
64: ; preds = %53
%65 = load i32, i32* %12
%66 = add nsw i32 %65, 1
store i32 %66, i32* %12
br label %49
67: ; preds = %49
%68 = load i32, i32* %9
store i32 %68, i32* %4
br label %71
69: ; preds = %3
%70 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %4
br label %71
71: ; preds = %69, %67, %44
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.3 = [6 x i8] c"%2hhx\00"
@str = [13 x i8] c"No such file\00"
@str.5 = [20 x i8] c"Failed to read file\00"
@str.6 = [17 x i8] c"Buffer too small\00"
define i32 @read_file(i8* nocapture %0, i32* %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 %36, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %4)
%10 = tail call i32 @rewind(i32* %4)
%11 = sdiv i32 %9, 2
%12 = icmp sgt i32 %11, %2
br i1 %12, label %13, label %15
13: ; preds = %6
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
br label %15
15: ; preds = %13, %6
%16 = tail call i64 @malloc(i32 %9)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @fread(i8* %17, i32 1, i32 %9, i32* %4)
%19 = tail call i32 @fclose(i32* %4)
%20 = icmp eq i32 %9, %18
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = icmp sgt i32 %2, 0
br i1 %22, label %23, label %38
23: ; preds = %21
%24 = zext i32 %2 to i64
br label %28
25: ; preds = %15
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
%27 = tail call i32 @free(i8* %17)
br label %38
28: ; preds = %23, %28
%29 = phi i64 [ 0, %23 ], [ %34, %28 ]
%30 = shl nuw nsw i64 %29, 1
%31 = getelementptr inbounds i8, i8* %17, i64 %30
%32 = getelementptr inbounds i32, i32* %1, i64 %29
%33 = tail call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %32)
%34 = add nuw nsw i64 %29, 1
%35 = icmp eq i64 %34, %24
br i1 %35, label %38, label %28
36: ; preds = %3
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %28, %21, %25, %36
%39 = phi i32 [ 0, %36 ], [ 0, %25 ], [ %9, %21 ], [ %9, %28 ]
ret i32 %39
}