#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*/ MyErr(int /*<<< orphan*/);
int /*<<< orphan*/ MyErr2(char *, int);
int /*<<< orphan*/ MyErr3(char *, int, int /*<<< orphan*/);
int O_CREAT;
int O_EXCL;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int, int);
int main() {
  int fd;
  if (-1 == (fd = open("test.txt", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR))) {
    MyErr(errno);
    MyErr2("open", 36);
    MyErr3("open", 37, errno);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    0x2e61(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e5f(%rip),%rax
or     (%rax),%edx
lea    0xe76(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11da <main+0x8a>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1030 <MyErr@plt>
lea    0xe53(%rip),%rdi
mov    $0x24,%esi
call   1070 <MyErr2@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
lea    0xe39(%rip),%rdi
mov    $0x25,%esi
call   1050 <MyErr3@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e9c(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e52(%rip),%rax
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe80(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bf <main+0x6f>
mov    0x2e35(%rip),%r15
mov    (%r15),%edi
call   1030 <MyErr@plt>
lea    0xe67(%rip),%r14
mov    %r14,%rdi
mov    $0x24,%esi
call   1070 <MyErr2@plt>
mov    (%r15),%edx
mov    %r14,%rdi
mov    $0x25,%esi
call   1050 <MyErr3@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_EXCL
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
store i32 %9, i32* %2
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %17
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = call i32 @MyErr(i32 %12)
%14 = call i32 @MyErr2(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 36)
%15 = load i32, i32* @errno
%16 = call i32 @MyErr3(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 37, i32 %15)
br label %17
17: ; preds = %11, %0
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_EXCL
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i32 @MyErr(i32 %10)
%12 = tail call i32 @MyErr2(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 36)
%13 = load i32, i32* @errno
%14 = tail call i32 @MyErr3(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 37, i32 %13)
br label %15
15: ; preds = %9, %0
%16 = tail call i32 @close(i32 %7)
ret i32 0
}