#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int process_child(char *);
int process_parent(scalar_t__);
int second_child_run();
int main(int argc, char *argv[]) {
  pid_t child = fork();
  if (argc > 1) {
    return second_child_run();
  }
  if (child < 0) {
    perror("fork failed");
    return 1;
  }
  if (child == 0) {
    return process_child(argv[0]);
  }
  return process_parent(child);
}


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   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    118a <main+0x3a>
mov    $0x0,%al
call   1030 <second_child_run@plt>
mov    %eax,-0x4(%rbp)
jmp    11d8 <main+0x88>
cmpq   $0x0,-0x18(%rbp)
jge    11ad <main+0x5d>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d8 <main+0x88>
cmpq   $0x0,-0x18(%rbp)
jne    11cc <main+0x7c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <process_child@plt>
mov    %eax,-0x4(%rbp)
jmp    11d8 <main+0x88>
mov    -0x18(%rbp),%rdi
call   1040 <process_parent@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0x2,%ebp
jl     1171 <main+0x21>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <second_child_run@plt>
test   %rax,%rax
js     1194 <main+0x44>
je     1186 <main+0x36>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <process_parent@plt>
mov    (%rbx),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <process_child@plt>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"fork failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (...) @second_child_run()
store i32 %11, i32* %3
br label %28
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %28
17: ; preds = %12
%18 = load i64, i64* %6
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @process_child(i8* %23)
store i32 %24, i32* %3
br label %28
25: ; preds = %17
%26 = load i64, i64* %6
%27 = call i32 @process_parent(i64 %26)
store i32 %27, i32* %3
br label %28
28: ; preds = %25, %20, %15, %10
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @second_child_run()
br label %18
7: ; preds = %2
%8 = icmp slt i64 %3, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %18
11: ; preds = %7
%12 = icmp eq i64 %3, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i8*, i8** %1
%15 = tail call i32 @process_child(i8* %14)
br label %18
16: ; preds = %11
%17 = tail call i32 @process_parent(i64 %3)
br label %18
18: ; preds = %16, %13, %9, %5
%19 = phi i32 [ %6, %5 ], [ 1, %9 ], [ %15, %13 ], [ %17, %16 ]
ret i32 %19
}