#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*/ O_RDONLY;
int /*<<< orphan*/ WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ close(int);
int creat(char *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int pid, fd, st;
  if ((pid = fork()) == 0) {
    if ((fd = creat(argv[3], 0666)) < 0) {
      printf("creat failed\n");
      exit(0);
    }
    dup2(fd, 2);
    close(fd);
    execlp(argv[1], argv[1], NULL);
    printf("exec failed\n");
  } else if (pid < 0) {
    printf("fork failed\n");
    exit(0);
  }
  wait(&st);
  if (WIFEXITED(st) && !WEXITSTATUS(st)) {
    if ((pid = fork()) == 0) {
      if ((fd = open(argv[3], O_RDONLY)) < 0) {
        printf("open failed\n");
        exit(0);
      }
      dup2(fd, 0);
      close(fd);
      execlp(argv[2], argv[2], NULL);
      printf("exec failed\n");
    } else if (pid < 0) {
      printf("fork failed\n");
      exit(0);
    }
  }
  wait(&st);
  return 0;
}


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    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1249 <main+0x99>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    120a <main+0x5a>
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <execlp@plt>
lea    0xdcf(%rip),%rdi
call   1040 <printf@plt>
jmp    126b <main+0xbb>
cmpl   $0x0,-0x14(%rbp)
jge    1266 <main+0xb6>
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    126b <main+0xbb>
lea    -0x1c(%rbp),%rdi
call   10c0 <wait@plt>
mov    -0x1c(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1342 <main+0x192>
mov    -0x1c(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
jne    1342 <main+0x192>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    131b <main+0x16b>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12df <main+0x12f>
lea    0xd55(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <execlp@plt>
lea    0xcfd(%rip),%rdi
call   1040 <printf@plt>
jmp    133d <main+0x18d>
cmpl   $0x0,-0x14(%rbp)
jge    1338 <main+0x188>
lea    0xcef(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    133d <main+0x18d>
jmp    1342 <main+0x192>
lea    -0x1c(%rbp),%rdi
call   10c0 <wait@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     11cf <main+0x1f>
jns    1214 <main+0x64>
lea    0xe42(%rip),%rdi
jmp    12b0 <main+0x100>
mov    0x18(%rbx),%rdi
mov    $0x1b6,%esi
call   1080 <creat@plt>
test   %eax,%eax
js     12a0 <main+0xf0>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x8(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10b0 <execlp@plt>
lea    0xe09(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     128d <main+0xdd>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
jne    128d <main+0xdd>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     124b <main+0x9b>
jns    128d <main+0xdd>
jmp    11c3 <main+0x13>
mov    0x18(%rbx),%rdi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     12a9 <main+0xf9>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x10(%rbx),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10b0 <execlp@plt>
lea    0xd90(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd7d(%rip),%rdi
jmp    12b0 <main+0x100>
lea    0xd50(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"creat failed\0A\00"
@.str.1 = [13 x i8] c"exec failed\0A\00"
@.str.2 = [13 x i8] c"fork failed\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [13 x i8] c"open failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 3
%14 = load i8*, i8** %13
%15 = call i32 @creat(i8* %14, i32 438)
store i32 %15, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %11
%21 = load i32, i32* %7
%22 = call i32 @dup2(i32 %21, i32 2)
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @execlp(i8* %27, i8* %30, i32* null)
%32 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %40
33: ; preds = %2
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %33
br label %40
40: ; preds = %39, %20
%41 = call i32 @wait(i32* %8)
%42 = load i32, i32* %8
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %83
45: ; preds = %40
%46 = load i32, i32* %8
%47 = call i32 @WEXITSTATUS(i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %83, label %49
49: ; preds = %45
%50 = call i32 (...) @fork()
store i32 %50, i32* %6
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %75
52: ; preds = %49
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 3
%55 = load i8*, i8** %54
%56 = load i32, i32* @O_RDONLY
%57 = call i32 @open(i8* %55, i32 %56)
store i32 %57, i32* %7
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %52
%60 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %52
%63 = load i32, i32* %7
%64 = call i32 @dup2(i32 %63, i32 0)
%65 = load i32, i32* %7
%66 = call i32 @close(i32 %65)
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 2
%69 = load i8*, i8** %68
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 2
%72 = load i8*, i8** %71
%73 = call i32 @execlp(i8* %69, i8* %72, i32* null)
%74 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %82
75: ; preds = %49
%76 = load i32, i32* %6
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%80 = call i32 @exit(i32 0)
unreachable
81: ; preds = %75
br label %82
82: ; preds = %81, %62
br label %83
83: ; preds = %82, %45, %40
%84 = call i32 @wait(i32* %8)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@str.5 = [12 x i8] c"open failed\00"
@str.6 = [12 x i8] c"fork failed\00"
@str.7 = [12 x i8] c"exec failed\00"
@str.8 = [13 x i8] c"creat failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %22
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 3
%9 = load i8*, i8** %8
%10 = tail call i32 @creat(i8* %9, i32 438)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %7
%16 = tail call i32 @dup2(i32 %10, i32 2)
%17 = tail call i32 @close(i32 %10)
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @execlp(i8* %19, i8* %19, i32* null)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %27
22: ; preds = %2
%23 = icmp slt i32 %5, 0
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %22, %15
%28 = call i32 @wait(i32* %3)
%29 = load i32, i32* %3
%30 = call i64 @WIFEXITED(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %60, label %32
32: ; preds = %27
%33 = load i32, i32* %3
%34 = call i32 @WEXITSTATUS(i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %60
36: ; preds = %32
%37 = call i32 (...) @fork()
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %55
39: ; preds = %36
%40 = getelementptr inbounds i8*, i8** %1, i64 3
%41 = load i8*, i8** %40
%42 = load i32, i32* @O_RDONLY
%43 = call i32 @open(i8* %41, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.5, i64 0, i64 0))
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %39
%49 = call i32 @dup2(i32 %43, i32 0)
%50 = call i32 @close(i32 %43)
%51 = getelementptr inbounds i8*, i8** %1, i64 2
%52 = load i8*, i8** %51
%53 = call i32 @execlp(i8* %52, i8* %52, i32* null)
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %60
55: ; preds = %36
%56 = icmp slt i32 %37, 0
br i1 %56, label %57, label %60
57: ; preds = %55
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.6, i64 0, i64 0))
%59 = call i32 @exit(i32 0)
unreachable
60: ; preds = %48, %55, %32, %27
%61 = call i32 @wait(i32* %3)
ret i32 0
}