#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 ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int main() {
  int fd;
  fd = open("NON_EXISTENT_FILE", O_RDONLY);
  if (fd < 0) {
    int errsv = errno;
    printf("\nError number is %d", errsv);
    if (errsv == ENOENT) {
      printf("\nFile does not exist. Check if the file is there.");
    }
    char *errmsg = strerror(errsv);
    printf("\nError is: %s\n", errmsg);
    exit(1);
  } else {
    printf("\nSuccessfully opened the destination file. Closing it.\n");
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e6 <main+0x96>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%eax
jne    11be <main+0x6e>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1070 <strerror@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1193 <main+0x33>
mov    %eax,%ebx
lea    0xee4(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e3e(%rip),%rax
mov    (%rax),%ebx
lea    0xe6f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e15(%rip),%rax
cmp    (%rax),%ebx
jne    11c5 <main+0x65>
lea    0xe68(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe85(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"NON_EXISTENT_FILE\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [20 x i8] c"\0AError number is %d\00"
@ENOENT = global i32 0
@.str.2 = [50 x i8] c"\0AFile does not exist. Check if the file is there.\00"
@.str.3 = [15 x i8] c"\0AError is: %s\0A\00"
@.str.4 = [56 x i8] c"\0ASuccessfully opened the destination file. Closing it.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %0
%10 = load i32, i32* @errno
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %3
%14 = load i32, i32* @ENOENT
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %9
%19 = load i32, i32* %3
%20 = call i8* @strerror(i32 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %24
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"NON_EXISTENT_FILE\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [20 x i8] c"\0AError number is %d\00"
@ENOENT = global i32 0
@.str.2 = [50 x i8] c"\0AFile does not exist. Check if the file is there.\00"
@.str.3 = [15 x i8] c"\0AError is: %s\0A\00"
@str = [55 x i8] c"\0ASuccessfully opened the destination file. Closing it.\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %15
4: ; preds = %0
%5 = load i32, i32* @errno
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @ENOENT
%8 = icmp eq i32 %5, %7
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %4
%12 = tail call i8* @strerror(i32 %5)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @close(i32 %2)
ret i32 0
}