#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_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int main() {
  int fd;
  errno = 0;
  fd = open("./test.txt", O_RDWR);
  if (0 == errno)
    printf("Sucessful\n");
  else {
    int err = errno;
    printf("Fail : the reason is : %s\n", strerror(err));
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%eax
jne    1198 <main+0x58>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11bd <main+0x7d>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <strerror@plt>
mov    %rax,%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rbx
movl   $0x0,(%rbx)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
test   %edi,%edi
je     1195 <main+0x45>
call   1070 <strerror@plt>
lea    0xe82(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11a1 <main+0x51>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [11 x i8] c"./test.txt\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"Sucessful\0A\00"
@.str.2 = [27 x i8] c"Fail : the reason is : %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* @errno
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* @errno
%7 = icmp eq i32 0, %6
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %15
10: ; preds = %0
%11 = load i32, i32* @errno
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %10, %8
%16 = load i32, i32* %2
%17 = call i32 @close(i32 %16)
ret i32 0
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [11 x i8] c"./test.txt\00"
@O_RDWR = global i32 0
@.str.2 = [27 x i8] c"Fail : the reason is : %s\0A\00"
@str = [10 x i8] c"Sucessful\00"
define i32 @main() {
store i32 0, i32* @errno
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32, i32* @errno
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %10
7: ; preds = %0
%8 = tail call i8* @strerror(i32 %3)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %7, %5
%11 = tail call i32 @close(i32 %2)
ret i32 0
}