#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(void) {
  int fd;
  fd = open("no_such_file", O_RDONLY);
  if (fd == -1)
    perror("fail to open");
  else
    close(fd);
  fd = open("denied", O_WRONLY);
  if (fd == -1)
    perror("fail to open");
  else
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1172 <main+0x42>
lea    0xea5(%rip),%rdi
call   1050 <perror@plt>
jmp    117a <main+0x4a>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ad <main+0x7d>
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
jmp    11b5 <main+0x85>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
lea    0xebf(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1177 <main+0x47>
mov    %eax,%edi
call   1030 <close@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1185 <main+0x55>
mov    %eax,%edi
call   1030 <close@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe8f(%rip),%rdi
call   1050 <perror@plt>
jmp    1152 <main+0x22>
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
jmp    1173 <main+0x43>

LLVM IR (-O0):
@.str = [13 x i8] c"no_such_file\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@.str.2 = [7 x i8] c"denied\00"
@O_WRONLY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %12
9: ; preds = %0
%10 = load i32, i32* %2
%11 = call i32 @close(i32 %10)
br label %12
12: ; preds = %9, %7
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %22
19: ; preds = %12
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
br label %22
22: ; preds = %19, %17
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"no_such_file\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"fail to open\00"
@.str.2 = [7 x i8] c"denied\00"
@O_WRONLY = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %8
6: ; preds = %0
%7 = tail call i32 @close(i32 %2)
br label %8
8: ; preds = %6, %4
%9 = load i32, i32* @O_WRONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %16
14: ; preds = %8
%15 = tail call i32 @close(i32 %10)
br label %16
16: ; preds = %14, %12
ret i32 0
}