#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_IRWXU;
int /*<<< orphan*/ close(int);
int dup(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int executeFileOutCommand(char *commandName, char *argv[], char *filename) {
  int def = dup(1);
  int file =
      open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
  if (file < 0)
    return 1;
  if (dup2(file, 1) < 0)
    return 1;
  int pid;
  if (pid = fork() == 0) {
    close(file);
    close(def);
    execve(commandName, argv, 0);
    return 0;
  }
  dup2(def, 1);
  close(file);
  close(def);
  wait(0);
  close(file);
  return 0;
}


x86 assembly (clang, -O0):
<executeFileOutCommand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x1,%edi
call   1040 <dup@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    0x2df7(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11e9 <executeFileOutCommand+0x79>
movl   $0x1,-0x4(%rbp)
jmp    1290 <executeFileOutCommand+0x120>
mov    -0x28(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    120c <executeFileOutCommand+0x9c>
movl   $0x1,-0x4(%rbp)
jmp    1290 <executeFileOutCommand+0x120>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
sete   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x2c(%rbp)
test   $0x1,%al
jne    1232 <executeFileOutCommand+0xc2>
jmp    125d <executeFileOutCommand+0xed>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1060 <execve@plt>
movl   $0x0,-0x4(%rbp)
jmp    1290 <executeFileOutCommand+0x120>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <wait@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<executeFileOutCommand>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x1,%ebx
mov    $0x1,%edi
call   1040 <dup@plt>
mov    0x2e19(%rip),%rcx
mov    0x2e5a(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2e47(%rip),%rcx
or     (%rcx),%esi
mov    0x2e26(%rip),%rcx
mov    0x2e0f(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %eax,%r12d
mov    0x2df9(%rip),%rax
or     (%rax),%edx
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1245 <executeFileOutCommand+0xd5>
mov    %eax,%ebp
mov    $0x1,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
test   %rax,%rax
js     1245 <executeFileOutCommand+0xd5>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1227 <executeFileOutCommand+0xb7>
mov    %r12d,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1080 <wait@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1245 <executeFileOutCommand+0xd5>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r12d,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <execve@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @executeFileOutCommand(i8* %0, i8** %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
%11 = call i32 @dup(i32 1)
store i32 %11, i32* %8
%12 = load i8*, i8** %7
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRWXU
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IROTH
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %12, i32 %17, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %3
store i32 1, i32* %4
br label %54
27: ; preds = %3
%28 = load i32, i32* %9
%29 = call i64 @dup2(i32 %28, i32 1)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 1, i32* %4
br label %54
32: ; preds = %27
%33 = call i64 (...) @fork()
%34 = icmp eq i64 %33, 0
%35 = zext i1 %34 to i32
store i32 %35, i32* %10
br i1 %34, label %36, label %44
36: ; preds = %32
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %8
%40 = call i32 @close(i32 %39)
%41 = load i8*, i8** %5
%42 = load i8**, i8*** %6
%43 = call i32 @execve(i8* %41, i8** %42, i32 0)
store i32 0, i32* %4
br label %54
44: ; preds = %32
%45 = load i32, i32* %8
%46 = call i64 @dup2(i32 %45, i32 1)
%47 = load i32, i32* %9
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = call i32 @wait(i32 0)
%52 = load i32, i32* %9
%53 = call i32 @close(i32 %52)
store i32 0, i32* %4
br label %54
54: ; preds = %44, %36, %31, %26
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
define i32 @executeFileOutCommand(i8* %0, i8** %1, i8* nocapture %2) {
%4 = tail call i32 @dup(i32 1)
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRWXU
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %2, i32 %9, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %33, label %17
17: ; preds = %3
%18 = tail call i64 @dup2(i32 %15, i32 1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %17
%21 = tail call i64 (...) @fork()
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = tail call i32 @close(i32 %15)
%25 = tail call i32 @close(i32 %4)
%26 = tail call i32 @execve(i8* %0, i8** %1, i32 0)
br label %33
27: ; preds = %20
%28 = tail call i64 @dup2(i32 %4, i32 1)
%29 = tail call i32 @close(i32 %15)
%30 = tail call i32 @close(i32 %4)
%31 = tail call i32 @wait(i32 0)
%32 = tail call i32 @close(i32 %15)
br label %33
33: ; preds = %23, %27, %17, %3
%34 = phi i32 [ 1, %3 ], [ 1, %17 ], [ 0, %27 ], [ 0, %23 ]
ret i32 %34
}