#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int read(int, char **, int);
void test_open_file_reading() {
  int fd = open("/etc/resolv.conf", O_RDONLY);
  if (fd == -1) {
    puts("Error opening file for read: BROKEN");
  }
  char buf[10];
  if (read(fd, &buf, 10) != 10) {
    puts("Did not read 10 bytes");
  }
  if (close(fd) == -1) {
    puts("Error closing");
  }
}


x86 assembly (clang, -O0):
<test_open_file_reading>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1176 <test_open_file_reading+0x36>
lea    0xea0(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0xa,%edx
call   1050 <read@plt>
cmp    $0xa,%eax
je     119c <test_open_file_reading+0x5c>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11b9 <test_open_file_reading+0x79>
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_open_file_reading>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    116d <test_open_file_reading+0x2d>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
lea    0x6(%rsp),%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1050 <read@plt>
cmp    $0xa,%eax
je     118f <test_open_file_reading+0x4f>
lea    0xeab(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     11a1 <test_open_file_reading+0x61>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xea3(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@O_RDONLY = global i32 0
@.str.1 = [36 x i8] c"Error opening file for read: BROKEN\00"
@.str.2 = [22 x i8] c"Did not read 10 bytes\00"
@.str.3 = [14 x i8] c"Error closing\00"
define void @test_open_file_reading() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* %1
%11 = bitcast [10 x i8]* %2 to i8**
%12 = call i32 @read(i32 %10, i8** %11, i32 10)
%13 = icmp ne i32 %12, 10
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i32, i32* %1
%18 = call i32 @close(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@O_RDONLY = global i32 0
@.str.1 = [36 x i8] c"Error opening file for read: BROKEN\00"
@.str.2 = [22 x i8] c"Did not read 10 bytes\00"
@.str.3 = [14 x i8] c"Error closing\00"
define void @test_open_file_reading() {
%1 = alloca [10 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%9 = bitcast [10 x i8]* %1 to i8**
%10 = call i32 @read(i32 %3, i8** %9, i32 10)
%11 = icmp eq i32 %10, 10
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = tail call i32 @close(i32 %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
ret void
}