#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_APPEND;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ closeFd(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  int fd;
  fd = open("contract.txt", O_RDONLY);
  if (fd == -1) {
    printf("error opening file\n");
    exit(EXIT_FAILURE);
  }
  closeFd(fd);
  fd = open("contract1.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("error opening file\n");
    exit(EXIT_FAILURE);
  }
  closeFd(fd);
  fd =
      open("w.log", O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    printf("error opening file\n");
    exit(EXIT_FAILURE);
  }
  closeFd(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1194 <main+0x54>
lea    0xe8c(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <closeFd@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
mov    0x2e01(%rip),%rax
or     (%rax),%edx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11fe <main+0xbe>
lea    0xe22(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <closeFd@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
mov    0x2dd1(%rip),%rax
or     (%rax),%esi
mov    0x2d68(%rip),%rax
or     (%rax),%esi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    0x2d8e(%rip),%rax
or     (%rax),%edx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1271 <main+0x131>
lea    0xdaf(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <closeFd@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11eb <main+0xab>
mov    %eax,%edi
call   1050 <closeFd@plt>
mov    0x2e62(%rip),%rax
mov    0x2e7b(%rip),%rbx
mov    (%rbx),%esi
or     (%rax),%esi
mov    0x2e68(%rip),%r15
or     (%r15),%esi
mov    0x2e1e(%rip),%r14
mov    0x2e2f(%rip),%r12
mov    (%r12),%edx
or     (%r14),%edx
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11eb <main+0xab>
mov    %eax,%edi
call   1050 <closeFd@plt>
mov    0x2dd7(%rip),%rax
mov    (%rbx),%esi
or     (%rax),%esi
or     (%r15),%esi
mov    0x2dc1(%rip),%rax
or     (%rax),%esi
mov    (%r12),%edx
or     (%r14),%edx
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    1205 <main+0xc5>
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    %eax,%edi
call   1050 <closeFd@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"contract.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"error opening file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [14 x i8] c"contract1.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [6 x i8] c"w.log\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* @O_RDONLY
%8 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @closeFd(i32 %16)
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRUSR
%24 = load i32, i32* @S_IWUSR
%25 = or i32 %23, %24
%26 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %15
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %15
%34 = load i32, i32* %6
%35 = call i32 @closeFd(i32 %34)
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = load i32, i32* @O_APPEND
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IRUSR
%44 = load i32, i32* @S_IWUSR
%45 = or i32 %43, %44
%46 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %42, i32 %45)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %33
%50 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %33
%54 = load i32, i32* %6
%55 = call i32 @closeFd(i32 %54)
%56 = load i32, i32* @EXIT_SUCCESS
%57 = call i32 @exit(i32 %56)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"contract.txt\00"
@O_RDONLY = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [14 x i8] c"contract1.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [6 x i8] c"w.log\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@EXIT_SUCCESS = global i32 0
@str.5 = [19 x i8] c"error opening file\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i32 @closeFd(i32 %4)
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %18, %17
%20 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %10
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %10
%27 = tail call i32 @closeFd(i32 %20)
%28 = load i32, i32* @O_WRONLY
%29 = load i32, i32* @O_CREAT
%30 = or i32 %29, %28
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = load i32, i32* @O_APPEND
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %36, %35
%38 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %44
40: ; preds = %26
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.5, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = tail call i32 @exit(i32 %42)
unreachable
44: ; preds = %26
%45 = tail call i32 @closeFd(i32 %38)
%46 = load i32, i32* @EXIT_SUCCESS
%47 = tail call i32 @exit(i32 %46)
unreachable
}