#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(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ test_fork_exec(char *);
int /*<<< orphan*/ test_spawn(char *);
int main(int argc, char **argv) {
  int argp = 1;
  int spawn_fork = 0;
  if ((argc > 2) || (argc < 1)) {
    fprintf(stderr, "Usage:%s [%s|%s]\n", argv[0], "s", "f");
    exit(1);
  } else if (argc == 1) {
    spawn_fork = 0;
  } else {
    if (!strcmp("s", argv[argp])) {
      spawn_fork = 0;
    } else if (!strcmp("f", argv[argp])) {
      spawn_fork = 1;
    } else {
      fprintf(stderr, "Usage:%s [%s|%s]\n", argv[0], "s", "f");
      exit(1);
    }
  }
  if (spawn_fork) {
    printf("Testing fork/exec\n");
    fflush(NULL);
    test_fork_exec("demo_process1");
    test_fork_exec("demo_process2");
  } else {
    printf("Testing spawn\n");
    fflush(NULL);
    test_spawn("demo_process1");
    test_spawn("demo_process2");
  }
  while (1)
    ;
  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)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
jg     11a8 <main+0x38>
cmpl   $0x1,-0x8(%rbp)
jge    11dc <main+0x6c>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe41(%rip),%rsi
lea    0xe4c(%rip),%rcx
lea    0xe47(%rip),%r8
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jne    11f2 <main+0x82>
movl   $0x0,-0x18(%rbp)
jmp    128a <main+0x11a>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe0d(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    121f <main+0xaf>
movl   $0x0,-0x18(%rbp)
jmp    1285 <main+0x115>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xde2(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    124c <main+0xdc>
movl   $0x1,-0x18(%rbp)
jmp    1280 <main+0x110>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd9d(%rip),%rsi
lea    0xda8(%rip),%rcx
lea    0xda3(%rip),%r8
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1285 <main+0x115>
jmp    128a <main+0x11a>
jmp    128f <main+0x11f>
cmpl   $0x0,-0x18(%rbp)
je     12cb <main+0x15b>
lea    0xd76(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
lea    0xd74(%rip),%rdi
call   1090 <test_fork_exec@plt>
lea    0xd76(%rip),%rdi
call   1090 <test_fork_exec@plt>
jmp    12f8 <main+0x188>
lea    0xd73(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <fflush@plt>
lea    0xd42(%rip),%rdi
call   1070 <test_spawn@plt>
lea    0xd44(%rip),%rdi
call   1070 <test_spawn@plt>
jmp    12fd <main+0x18d>
jmp    12fd <main+0x18d>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    -0x3(%rdi),%eax
cmp    $0xfffffffd,%eax
jbe    1212 <main+0xa2>
cmp    $0x1,%edi
je     11df <main+0x6f>
mov    0x8(%r14),%rbx
lea    0xe7f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11df <main+0x6f>
lea    0xe6e(%rip),%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
jne    1212 <main+0xa2>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
lea    0xe4a(%rip),%rdi
call   1090 <test_fork_exec@plt>
lea    0xe4c(%rip),%rdi
call   1090 <test_fork_exec@plt>
jmp    1210 <main+0xa0>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
lea    0xe1d(%rip),%rdi
call   1070 <test_spawn@plt>
lea    0xe1f(%rip),%rdi
call   1070 <test_spawn@plt>
nopw   0x0(%rax,%rax,1)
jmp    1210 <main+0xa0>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xddb(%rip),%rsi
lea    0xde6(%rip),%rcx
lea    0xde1(%rip),%r8
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Usage:%s [%s|%s]\0A\00"
@.str.1 = [2 x i8] c"s\00"
@.str.2 = [2 x i8] c"f\00"
@.str.3 = [19 x i8] c"Testing fork/exec\0A\00"
@.str.4 = [14 x i8] c"demo_process1\00"
@.str.5 = [14 x i8] c"demo_process2\00"
@.str.6 = [15 x i8] c"Testing spawn\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %20
13: ; preds = %10, %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %10
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 0, i32* %7
br label %51
24: ; preds = %20
%25 = load i8**, i8*** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
%30 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %24
store i32 0, i32* %7
br label %50
33: ; preds = %24
%34 = load i8**, i8*** %5
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8*, i8** %34, i64 %36
%38 = load i8*, i8** %37
%39 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %33
store i32 1, i32* %7
br label %49
42: ; preds = %33
%43 = load i32, i32* @stderr
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 0
%46 = load i8*, i8** %45
%47 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
br label %50
50: ; preds = %49, %32
br label %51
51: ; preds = %50, %23
br label %52
52: ; preds = %51
%53 = load i32, i32* %7
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @fflush(i32* null)
%58 = call i32 @test_fork_exec(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @test_fork_exec(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %65
60: ; preds = %52
%61 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%62 = call i32 @fflush(i32* null)
%63 = call i32 @test_spawn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%64 = call i32 @test_spawn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %60, %55
br label %66
66: ; preds = %65, %66
br label %66
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Usage:%s [%s|%s]\0A\00"
@.str.1 = [2 x i8] c"s\00"
@.str.2 = [2 x i8] c"f\00"
@.str.4 = [14 x i8] c"demo_process1\00"
@.str.5 = [14 x i8] c"demo_process2\00"
@str = [14 x i8] c"Testing spawn\00"
@str.7 = [18 x i8] c"Testing fork/exec\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -3
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = icmp eq i32 %0, 1
br i1 %11, label %30, label %12
12: ; preds = %10
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* dereferenceable(1) %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %12
%18 = tail call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* dereferenceable(1) %14)
%19 = icmp eq i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %1
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
%27 = tail call i32 @fflush(i32* null)
%28 = tail call i32 @test_fork_exec(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%29 = tail call i32 @test_fork_exec(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %35
30: ; preds = %12, %10
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%32 = tail call i32 @fflush(i32* null)
%33 = tail call i32 @test_spawn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%34 = tail call i32 @test_spawn(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %30, %25
br label %36
36: ; preds = %35, %36
br label %36
}