#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  int fd;
  char *filename1 = "/tmp/whatever1.txt";
  unlink(filename1);
  fd = open(filename1, O_WRONLY | O_CREAT | O_TRUNC,
            S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
  if (fd == -1) {
    perror("open");
  }
  char *filename2 = "/tmp/whatever3.txt";
  unlink(filename2);
  fd = open(filename2, O_WRONLY | O_CREAT | O_TRUNC, 0664);
  if (fd == -1) {
    perror("open");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeb3(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e8a(%rip),%rax
or     (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
mov    0x2e46(%rip),%rax
or     (%rax),%edx
mov    0x2e25(%rip),%rax
or     (%rax),%edx
mov    0x2e14(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c4 <main+0x94>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
lea    0xe4d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x28(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
or     (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
mov    $0x1b4,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    121a <main+0xea>
lea    0xdfe(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xec1(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e52(%rip),%r14
mov    0x2ea3(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
mov    0x2e8e(%rip),%r12
or     (%r12),%esi
mov    0x2e63(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e59(%rip),%rax
or     (%rax),%edx
mov    0x2e38(%rip),%rax
or     (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11d9 <main+0xa9>
lea    0xe71(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    (%r15),%esi
or     (%r14),%esi
or     (%r12),%esi
mov    %rbx,%rdi
mov    $0x1b4,%edx
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11e7 <main+0xb7>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe33(%rip),%rdi
call   1050 <perror@plt>
jmp    11a0 <main+0x70>
lea    0xe25(%rip),%rdi
call   1050 <perror@plt>
jmp    11cb <main+0x9b>

LLVM IR (-O0):
@.str = [19 x i8] c"/tmp/whatever1.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [19 x i8] c"/tmp/whatever3.txt\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %7
%9 = load i8*, i8** %7
%10 = call i32 @unlink(i8* %9)
%11 = load i8*, i8** %7
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IWUSR
%18 = load i32, i32* @S_IRUSR
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IWGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IROTH
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %11, i32 %16, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %2
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %2
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %8
%32 = load i8*, i8** %8
%33 = call i32 @unlink(i8* %32)
%34 = load i8*, i8** %8
%35 = load i32, i32* @O_WRONLY
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %34, i32 %39, i32 436)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %45
45: ; preds = %43, %31
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"/tmp/whatever1.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [19 x i8] c"/tmp/whatever3.txt\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IWUSR
%10 = load i32, i32* @S_IRUSR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IWGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %2
%23 = tail call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %26, %27
%29 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 436)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %22
ret i32 0
}