#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 int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ syserr(char *);
int wait(int /*<<< orphan*/);
int main() {
  pid_t pid;
  switch (pid = fork()) {
  case -1:
    syserr("Error in fork\n");
    break;
  case 0:
    printf("I am a child and my pid is %d\n", getpid());
    execlp("ps", "ps", NULL);
    syserr("Error in execlp\n");
    break;
  default:
    printf("I am a parent and my pid is %d\n", getpid());
    if (wait(0) == -1)
      syserr("Error in wait\n");
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     119c <main+0x3c>
jmp    118c <main+0x2c>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     11ad <main+0x4d>
jmp    11e6 <main+0x86>
lea    0xe5d(%rip),%rdi
call   1050 <syserr@plt>
jmp    121e <main+0xbe>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
lea    0xe65(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1060 <execlp@plt>
lea    0xe55(%rip),%rdi
call   1050 <syserr@plt>
jmp    121e <main+0xbe>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
jne    1217 <main+0xb7>
lea    0xe50(%rip),%rdi
call   1050 <syserr@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11a0 <main+0x40>
test   %eax,%eax
jne    11a9 <main+0x49>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe90(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
lea    0xea1(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1060 <execlp@plt>
lea    0xe93(%rip),%rdi
jmp    11d1 <main+0x71>
lea    0xe59(%rip),%rdi
jmp    11d1 <main+0x71>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <wait@plt>
cmp    $0xffffffff,%eax
jne    11d6 <main+0x76>
lea    0xe91(%rip),%rdi
call   1050 <syserr@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Error in fork\0A\00"
@.str.1 = [31 x i8] c"I am a child and my pid is %d\0A\00"
@.str.2 = [3 x i8] c"ps\00"
@.str.3 = [17 x i8] c"Error in execlp\0A\00"
@.str.4 = [32 x i8] c"I am a parent and my pid is %d\0A\00"
@.str.5 = [15 x i8] c"Error in wait\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @fork()
store i32 %3, i32* %2
switch i32 %3, label %11 [
i32 -1, label %4
i32 0, label %6
]
4: ; preds = %0
%5 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %19
6: ; preds = %0
%7 = call i32 (...) @getpid()
%8 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
%10 = call i32 @syserr(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %19
11: ; preds = %0
%12 = call i32 (...) @getpid()
%13 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%14 = call i32 @wait(i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @syserr(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
store i32 0, i32* %1
br label %19
19: ; preds = %18, %6, %4
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
@.str = [15 x i8] c"Error in fork\0A\00"
@.str.1 = [31 x i8] c"I am a child and my pid is %d\0A\00"
@.str.2 = [3 x i8] c"ps\00"
@.str.3 = [17 x i8] c"Error in execlp\0A\00"
@.str.4 = [32 x i8] c"I am a parent and my pid is %d\0A\00"
@.str.5 = [15 x i8] c"Error in wait\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %6 [
i32 -1, label %11
i32 0, label %2
]
2: ; preds = %0
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = tail call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %11
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %7)
%9 = tail call i32 @wait(i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %6, %0, %2
%12 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), %6 ]
%13 = tail call i32 @syserr(i8* %12)
br label %14
14: ; preds = %11, %6
ret i32 0
}