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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned long *, int);
void readFile(char *fname) {
  unsigned long word;
  ssize_t nr;
  int fd;
  fd = open(fname, O_RDONLY);
  if (fd == -1) {
    printf("Error opening file\n");
    return;
  }
  nr = read(fd, &word, sizeof(unsigned long));
  if (nr == -1) {
    printf("Error reading file\n");
  } else {
    printf("Some bytes %zu\n", nr);
  }
  if (close(fd) == -1) {
    printf("Error closing file\n");
  }
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    117e <readFile+0x3e>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11df <readFile+0x9f>
mov    -0x18(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11af <readFile+0x6f>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11c0 <readFile+0x80>
mov    -0x14(%rbp),%esi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11df <readFile+0x9f>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1192 <readFile+0x42>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     119b <readFile+0x4b>
lea    0xe79(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a7 <readFile+0x57>
lea    0xe9d(%rip),%rdi
jmp    11c0 <readFile+0x70>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     11b9 <readFile+0x69>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe50(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [20 x i8] c"Error opening file\0A\00"
@.str.1 = [20 x i8] c"Error reading file\0A\00"
@.str.2 = [16 x i8] c"Some bytes %zu\0A\00"
@.str.3 = [20 x i8] c"Error closing file\0A\00"
define void @readFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %29
13: ; preds = %1
%14 = load i32, i32* %5
%15 = call i32 @read(i32 %14, i64* %3, i32 8)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %23
20: ; preds = %13
%21 = load i32, i32* %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %18
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %11, %27, %23
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.2 = [16 x i8] c"Some bytes %zu\0A\00"
@str = [19 x i8] c"Error closing file\00"
@str.4 = [19 x i8] c"Error reading file\00"
@str.5 = [19 x i8] c"Error opening file\00"
define void @readFile(i8* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %17, label %7
7: ; preds = %1
%8 = call i32 @read(i32 %5, i64* %2, i32 8)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
br label %14
12: ; preds = %7
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %8)
br label %14
14: ; preds = %12, %10
%15 = tail call i32 @close(i32 %5)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14, %1
%18 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0), %1 ], [ getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0), %14 ]
%19 = tail call i32 @puts(i8* dereferenceable(1) %18)
br label %20
20: ; preds = %17, %14
ret void
}