#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 EACCES;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int errno;
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int main(void) {
  int fd = open("/this/does/not/exist", O_RDONLY, 0644);
  if (fd == -1) {
    printf("errno=%d\n", errno);
    if (errno == EACCES) {
      perror("EACCES error in open");
      printf("EACCES error in open: %s\n", strerror(errno));
    } else {
      perror("Other error in open");
      printf("Other error in open: %s\n", strerror(errno));
    }
  }
  perror("Perror1");
  fd = open("/dev/null", O_WRONLY, 0644);
  if (fd > 0) {
    printf("open() was successful.\n");
    perror("Perror2");
  }
  errno = 1;
  perror("Perror3");
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1205 <main+0xc5>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%eax
jne    11d5 <main+0x95>
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1200 <main+0xc0>
lea    0xe72(%rip),%rdi
call   1050 <perror@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1205 <main+0xc5>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
lea    0xe62(%rip),%rdi
mov    $0x1a4,%edx
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1252 <main+0x112>
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d77(%rip),%rax
movl   $0x1,(%rax)
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    11d5 <main+0x85>
mov    0x2e56(%rip),%r14
mov    (%r14),%esi
lea    0xe91(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%eax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%eax
lea    0xe81(%rip),%rax
lea    0xea9(%rip),%rdi
cmove  %rax,%rdi
lea    0xe84(%rip),%rax
lea    0xeab(%rip),%rbx
cmove  %rax,%rbx
call   1060 <perror@plt>
mov    (%r14),%edi
call   1070 <strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe9f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    0xe92(%rip),%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
test   %eax,%eax
jle    1217 <main+0xc7>
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db2(%rip),%rax
movl   $0x1,(%rax)
lea    0xe6a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/this/does/not/exist\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"errno=%d\0A\00"
@errno = global i32 0
@EACCES = global i32 0
@.str.2 = [21 x i8] c"EACCES error in open\00"
@.str.3 = [26 x i8] c"EACCES error in open: %s\0A\00"
@.str.4 = [20 x i8] c"Other error in open\00"
@.str.5 = [25 x i8] c"Other error in open: %s\0A\00"
@.str.6 = [8 x i8] c"Perror1\00"
@.str.7 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.8 = [24 x i8] c"open() was successful.\0A\00"
@.str.9 = [8 x i8] c"Perror2\00"
@.str.10 = [8 x i8] c"Perror3\00"
@EXIT_SUCCESS = 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 ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %3, i32 420)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %24
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @errno
%11 = load i32, i32* @EACCES
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %18
13: ; preds = %7
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %16)
br label %23
18: ; preds = %7
%19 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %18, %13
br label %24
24: ; preds = %23, %0
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%26 = load i32, i32* @O_WRONLY
%27 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %26, i32 420)
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %33
33: ; preds = %30, %24
store i32 1, i32* @errno
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
%35 = load i32, i32* @EXIT_SUCCESS
ret i32 %35
}

LLVM IR (-O2):
@.str = [21 x i8] c"/this/does/not/exist\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"errno=%d\0A\00"
@errno = global i32 0
@EACCES = global i32 0
@.str.2 = [21 x i8] c"EACCES error in open\00"
@.str.3 = [26 x i8] c"EACCES error in open: %s\0A\00"
@.str.4 = [20 x i8] c"Other error in open\00"
@.str.5 = [25 x i8] c"Other error in open: %s\0A\00"
@.str.6 = [8 x i8] c"Perror1\00"
@.str.7 = [10 x i8] c"/dev/null\00"
@O_WRONLY = global i32 0
@.str.9 = [8 x i8] c"Perror2\00"
@.str.10 = [8 x i8] c"Perror3\00"
@EXIT_SUCCESS = global i32 0
@str = [23 x i8] c"open() was successful.\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %1, i32 420)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %16
4: ; preds = %0
%5 = load i32, i32* @errno
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @errno
%8 = load i32, i32* @EACCES
%9 = icmp eq i32 %7, %8
%10 = select i1 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0)
%11 = select i1 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0)
%12 = tail call i32 @perror(i8* %10)
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %11, i8* %14)
br label %16
16: ; preds = %4, %0
%17 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%18 = load i32, i32* @O_WRONLY
%19 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %18, i32 420)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %24
24: ; preds = %21, %16
store i32 1, i32* @errno
%25 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
%26 = load i32, i32* @EXIT_SUCCESS
ret i32 %26
}