#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 char uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ *fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int read_dumpfile(uint8_t *buf, size_t buflen, const char *filename) {
  FILE *fp;
  char i;
  int pos = 0;
  if (NULL == (fp = fopen(filename, "rb")))
    return 1;
  while (NULL != fread(&i, sizeof(char), 1, fp) && buflen >= pos) {
    buf[pos++] = i;
    printf("%02X", i);
  }
  printf("\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<read_dumpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1189 <read_dumpfile+0x49>
movl   $0x1,-0x4(%rbp)
jmp    1223 <read_dumpfile+0xe3>
jmp    118e <read_dumpfile+0x4e>
mov    -0x28(%rbp),%rcx
lea    -0x29(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %rax,%rdx
xor    %eax,%eax
xor    %ecx,%ecx
cmp    %rdx,%rcx
mov    %al,-0x31(%rbp)
je     11c6 <read_dumpfile+0x86>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
cmp    %rcx,%rax
setae  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11d6 <read_dumpfile+0x96>
jmp    1205 <read_dumpfile+0xc5>
mov    -0x29(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x30(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x29(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    118e <read_dumpfile+0x4e>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_dumpfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe98(%rip),%rsi
mov    %rdx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ff <read_dumpfile+0xaf>
mov    %rax,%rbx
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11e9 <read_dumpfile+0x99>
lea    0xe64(%rip),%r12
lea    0x7(%rsp),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
movsbl 0x7(%rsp),%esi
mov    %sil,(%r15,%rbp,1)
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11e9 <read_dumpfile+0x99>
lea    0x1(%rbp),%rax
cmp    %r14,%rbp
mov    %rax,%rbp
jb     11b0 <read_dumpfile+0x60>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    1204 <read_dumpfile+0xb4>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @read_dumpfile(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8
%10 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 0, i32* %10
%11 = load i8*, i8** %7
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = icmp eq i32* null, %12
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 1, i32* %4
br label %41
15: ; preds = %3
br label %16
16: ; preds = %27, %15
%17 = load i32*, i32** %8
%18 = call i32* @fread(i8* %9, i32 1, i32 1, i32* %17)
%19 = icmp ne i32* null, %18
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i64, i64* %6
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = icmp uge i64 %21, %23
br label %25
25: ; preds = %20, %16
%26 = phi i1 [ false, %16 ], [ %24, %20 ]
br i1 %26, label %27, label %37
27: ; preds = %25
%28 = load i8, i8* %9
%29 = load i8*, i8** %5
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
store i32 %31, i32* %10
%32 = sext i32 %30 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 %28, i8* %33
%34 = load i8, i8* %9
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %35)
br label %16
37: ; preds = %25
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32*, i32** %8
%40 = call i32 @fclose(i32* %39)
store i32 0, i32* %4
br label %41
41: ; preds = %37, %14
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c"%02X\00"
define i32 @read_dumpfile(i8* nocapture writeonly %0, i64 %1, i8* nocapture %2) {
%4 = alloca i8
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %24, label %7
7: ; preds = %3
%8 = call i32* @fread(i8* %4, i32 1, i32 1, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %21, label %10
10: ; preds = %7, %10
%11 = phi i64 [ %13, %10 ], [ 0, %7 ]
%12 = load i8, i8* %4
%13 = add nuw i64 %11, 1
%14 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %12, i8* %14
%15 = sext i8 %12 to i32
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32* @fread(i8* %4, i32 1, i32 1, i32* %5)
%18 = icmp ne i32* %17, null
%19 = icmp ult i64 %11, %1
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %10, label %21
21: ; preds = %10, %7
%22 = tail call i32 @putchar(i32 10)
%23 = tail call i32 @fclose(i32* %5)
br label %24
24: ; preds = %3, %21
%25 = phi i32 [ 0, %21 ], [ 1, %3 ]
ret i32 %25
}