#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char, ...);
int read(int, char *, int);
void read_file(char *fname) {
  int fd;
  int ret;
  char *rbuf;
  fd = open(fname, O_RDONLY);
  if (fd == -1) {
    perror("open file  fail ");
    exit(-1);
  }
  rbuf = (char *)malloc(1024);
  ret = read(fd, rbuf, 1024);
  if (ret < 0) {
    perror("read file fail ");
    close(fd);
    exit(-2);
  }
  printf("read %d bytes from file %s\n", ret, fname);
  for (int i = 0; i < ret; i++) {
    printf("%x ", rbuf[i]);
  }
  ret = close(fd);
  if (ret) {
    perror("close fail ");
    exit(-2);
  }
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11b1 <read_file+0x41>
lea    0xe5e(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11fb <read_file+0x8b>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rdx
lea    0xe18(%rip),%rdi
movsbl %al,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    124e <read_file+0xde>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xe08(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    121a <read_file+0xaa>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1279 <read_file+0x109>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     120d <read_file+0x9d>
mov    %eax,%r14d
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1223 <read_file+0xb3>
mov    %eax,%ebp
movsbl %bpl,%esi
lea    0xe62(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
test   %ebp,%ebp
je     11f8 <read_file+0x88>
mov    %ebp,%ebp
lea    0xe67(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r12,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11e0 <read_file+0x70>
mov    %r14d,%edi
call   1040 <close@plt>
test   %eax,%eax
jne    1241 <read_file+0xd1>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdec(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [17 x i8] c"open file fail \00"
@.str.1 = [16 x i8] c"read file fail \00"
@.str.2 = [28 x i8] c"read %d bytes from file %s\0A\00"
@.str.3 = [4 x i8] c"%x \00"
@.str.4 = [12 x i8] c"close fail \00"
define void @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %1
%16 = call i64 @malloc(i32 1024)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %5
%18 = load i32, i32* %3
%19 = load i8*, i8** %5
%20 = call i32 @read(i32 %18, i8* %19, i32 1024)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %15
%24 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
%27 = call i32 @exit(i32 -2)
unreachable
28: ; preds = %15
%29 = load i32, i32* %4
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %2
%32 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8 signext %30, i8* %31)
store i32 0, i32* %6
br label %33
33: ; preds = %44, %28
%34 = load i32, i32* %6
%35 = load i32, i32* %4
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %47
37: ; preds = %33
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %42)
br label %44
44: ; preds = %37
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
br label %33
47: ; preds = %33
%48 = load i32, i32* %3
%49 = call i32 @close(i32 %48)
store i32 %49, i32* %4
%50 = load i32, i32* %4
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @exit(i32 -2)
unreachable
55: ; preds = %47
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [17 x i8] c"open file fail \00"
@.str.1 = [16 x i8] c"read file fail \00"
@.str.2 = [28 x i8] c"read %d bytes from file %s\0A\00"
@.str.3 = [4 x i8] c"%x \00"
@.str.4 = [12 x i8] c"close fail \00"
define void @read_file(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %1
%9 = tail call i64 @malloc(i32 1024)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i32 @read(i32 %3, i8* %10, i32 1024)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @close(i32 %3)
%16 = tail call i32 @exit(i32 -2)
unreachable
17: ; preds = %8
%18 = trunc i32 %11 to i8
%19 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8 signext %18, i8* %0)
%20 = icmp eq i32 %11, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = zext i32 %11 to i64
br label %26
23: ; preds = %26, %17
%24 = tail call i32 @close(i32 %3)
%25 = icmp eq i32 %24, 0
br i1 %25, label %36, label %33
26: ; preds = %21, %26
%27 = phi i64 [ 0, %21 ], [ %31, %26 ]
%28 = getelementptr inbounds i8, i8* %10, i64 %27
%29 = load i8, i8* %28
%30 = tail call i32 (i8*, i8, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 signext %29)
%31 = add nuw nsw i64 %27, 1
%32 = icmp eq i64 %31, %22
br i1 %32, label %23, label %26
33: ; preds = %23
%34 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%35 = tail call i32 @exit(i32 -2)
unreachable
36: ; preds = %23
ret void
}