#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
char *buildQueryUrl(char *);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ execlp(char *, char *, char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  if ((pid = fork()) < 0) {
    err_sys("fork error");
  } else if (pid == 0) {
    if (argc == 1) {
      execlp("./print-pid", "print-pid", (char *)NULL);
    } else if (argc == 2) {
      char *url = buildQueryUrl(argv[1]);
      execlp("google-chrome", "google-chrome", "--new-window", url,
             (char *)NULL);
    }
    err_sys("exec failed");
    exit(EXIT_FAILURE);
  }
  printf("Created child with pid %d\n", pid);
  printf("Parent waiting for child to finish...\n");
  if (waitpid(pid, NULL, 0) != pid) {
    err_sys("waitpid error");
  }
  printf("Parent exiting\n");
  exit(EXIT_SUCCESS);
}


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   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11ac <main+0x3c>
lea    0xe5e(%rip),%rdi
call   1030 <err_sys@plt>
jmp    123f <main+0xcf>
cmpq   $0x0,-0x18(%rbp)
jne    123a <main+0xca>
cmpl   $0x1,-0x8(%rbp)
jne    11df <main+0x6f>
lea    0xe43(%rip),%rdi
lea    0xe3e(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    1220 <main+0xb0>
cmpl   $0x2,-0x8(%rbp)
jne    121b <main+0xab>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <buildQueryUrl@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
lea    0xe12(%rip),%rsi
lea    0xe19(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
mov    $0x0,%al
call   1070 <execlp@plt>
jmp    1220 <main+0xb0>
lea    0xe0b(%rip),%rdi
call   1030 <err_sys@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    123f <main+0xcf>
mov    -0x18(%rbp),%rsi
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    -0x18(%rbp),%rax
je     1284 <main+0x114>
lea    0xe01(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11a6 <main+0x26>
lea    0xe61(%rip),%rdi
call   1040 <err_sys@plt>
jmp    11cf <main+0x4f>
jne    11cf <main+0x4f>
cmp    $0x2,%ebp
je     1223 <main+0xa3>
cmp    $0x1,%ebp
jne    124a <main+0xca>
lea    0xe4e(%rip),%rdi
lea    0xe49(%rip),%rsi
xor    %edx,%edx
xor    %eax,%eax
call   1080 <execlp@plt>
jmp    124a <main+0xca>
lea    0xe68(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %rbx,%rax
je     1209 <main+0x89>
lea    0xe55(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x8(%r14),%rdi
call   10a0 <buildQueryUrl@plt>
lea    0xde4(%rip),%rdi
lea    0xdeb(%rip),%rdx
mov    %rdi,%rsi
mov    %rax,%rcx
xor    %r8d,%r8d
xor    %eax,%eax
call   1080 <execlp@plt>
lea    0xde1(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [12 x i8] c"./print-pid\00"
@.str.2 = [10 x i8] c"print-pid\00"
@.str.3 = [14 x i8] c"google-chrome\00"
@.str.4 = [13 x i8] c"--new-window\00"
@.str.5 = [12 x i8] c"exec failed\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [27 x i8] c"Created child with pid %d\0A\00"
@.str.7 = [39 x i8] c"Parent waiting for child to finish...\0A\00"
@.str.8 = [14 x i8] c"waitpid error\00"
@.str.9 = [16 x i8] c"Parent exiting\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %36
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %35
15: ; preds = %12
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %31
20: ; preds = %15
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 2
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i8* @buildQueryUrl(i8* %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %28, i8* null)
br label %30
30: ; preds = %23, %20
br label %31
31: ; preds = %30, %18
%32 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %12
br label %36
36: ; preds = %35, %10
%37 = load i64, i64* %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i64 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%40 = load i64, i64* %6
%41 = call i64 @waitpid(i64 %40, i32* null, i32 0)
%42 = load i64, i64* %6
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
br label %46
46: ; preds = %44, %36
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0))
%48 = load i32, i32* @EXIT_SUCCESS
%49 = call i32 @exit(i32 %48)
unreachable
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [12 x i8] c"./print-pid\00"
@.str.2 = [10 x i8] c"print-pid\00"
@.str.3 = [14 x i8] c"google-chrome\00"
@.str.4 = [13 x i8] c"--new-window\00"
@.str.5 = [12 x i8] c"exec failed\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [27 x i8] c"Created child with pid %d\0A\00"
@.str.8 = [14 x i8] c"waitpid error\00"
@EXIT_SUCCESS = global i32 0
@str = [38 x i8] c"Parent waiting for child to finish...\00"
@str.10 = [15 x i8] c"Parent exiting\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %21
7: ; preds = %2
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %21
9: ; preds = %7
switch i32 %0, label %17 [
i32 1, label %10
i32 2, label %12
]
10: ; preds = %9
%11 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* null)
br label %17
12: ; preds = %9
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i8* @buildQueryUrl(i8* %14)
%16 = tail call i32 (i8*, i8*, i8*, ...) @execlp(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %15, i8* null)
br label %17
17: ; preds = %9, %12, %10
%18 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %7, %5
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i64 %3)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
%24 = tail call i64 @waitpid(i64 %3, i32* null, i32 0)
%25 = icmp eq i64 %24, %3
br i1 %25, label %28, label %26
26: ; preds = %21
%27 = tail call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%30 = load i32, i32* @EXIT_SUCCESS
%31 = tail call i32 @exit(i32 %30)
unreachable
}