#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int open_output(int argc, char **argv) {
  if (argc == 4) {
    int outputFd =
        open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (outputFd == -1) {
      perror(argv[3]);
      close(outputFd);
      exit(EXIT_FAILURE);
    } else
      return outputFd;
  } else {
    int outputFd =
        open("fconc.out", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (outputFd == -1) {
      perror("fconc.out");
      exit(EXIT_FAILURE);
    } else
      return outputFd;
  }
}


x86 assembly (clang, -O0):
<open_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11ce <open_output+0x8e>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e87(%rip),%rax
or     (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
mov    0x2e3d(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c3 <open_output+0x83>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <open_output+0xf4>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
mov    0x2ddf(%rip),%rax
or     (%rax),%edx
lea    0xdfe(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    122e <open_output+0xee>
lea    0xde5(%rip),%rdi
call   1050 <perror@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_output>:
push   %rbx
cmp    $0x4,%edi
jne    1199 <open_output+0x59>
mov    %rsi,%rbx
mov    0x18(%rsi),%rdi
mov    0x2e5c(%rip),%rax
mov    0x2e9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e8a(%rip),%rax
or     (%rax),%esi
mov    0x2e51(%rip),%rax
mov    0x2e62(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1040 <open@plt>
cmp    $0xffffffff,%eax
jne    11d7 <open_output+0x97>
mov    0x18(%rbx),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1030 <close@plt>
jmp    11e5 <open_output+0xa5>
mov    0x2e10(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe33(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     11d9 <open_output+0x99>
pop    %rbx
ret
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [10 x i8] c"fconc.out\00"
define i32 @open_output(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %4, label %23
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 3
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %6, i32 %11, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %38
17: ; preds = %4
%18 = load i8*, i8** %5
%19 = tail call i32 @perror(i8* %18)
%20 = tail call i32 @close(i32 -1)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%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 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %30, %29
%32 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %28, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %23
%35 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = tail call i32 @exit(i32 %36)
unreachable
38: ; preds = %23, %4
%39 = phi i32 [ %15, %4 ], [ %32, %23 ]
ret i32 %39
}