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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_IRUSR;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
void read_file(const char *file_name) {
  int fd = open(file_name, O_RDONLY, S_IRUSR);
  if (fd == -1) {
    printf("failed to open the file %s", file_name);
    return;
  }
  char buf[64];
  ssize_t bytes_read = 0;
  while ((bytes_read = read(fd, buf, 64)) != 0) {
    printf("%s", buf);
  }
  printf("\nnow close the file\n");
  close(fd);
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    118b <read_file+0x4b>
mov    -0x8(%rbp),%rsi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11dc <read_file+0x9c>
movl   $0x0,-0x54(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     11c6 <read_file+0x86>
lea    -0x50(%rbp),%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1192 <read_file+0x52>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%r14
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11dc <read_file+0x8c>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11c0 <read_file+0x70>
lea    0xe85(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11a0 <read_file+0x50>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str = [27 x i8] c"failed to open the file %s\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [21 x i8] c"\0Anow close the file\0A\00"
define void @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @S_IRUSR
%9 = call i32 @open(i8* %6, i32 %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 = load i8*, i8** %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %28
15: ; preds = %1
store i32 0, i32* %5
br label %16
16: ; preds = %21, %15
%17 = load i32, i32* %3
%18 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 64)
store i32 %19, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %16
24: ; preds = %16
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %3
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %24, %12
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str = [27 x i8] c"failed to open the file %s\00"
@.str.1 = [3 x i8] c"%s\00"
@str = [20 x i8] c"\0Anow close the file\00"
define void @read_file(i8* %0) {
%2 = alloca [64 x i8]
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @S_IRUSR
%5 = tail call i32 @open(i8* %0, i32 %3, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
9: ; preds = %1
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%11 = call i32 @read(i32 %5, i8* %10, i32 64)
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %9, %13
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%15 = call i32 @read(i32 %5, i8* %10, i32 64)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %13
17: ; preds = %13, %9
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%19 = call i32 @close(i32 %5)
br label %20
20: ; preds = %17, %7
ret void
}