#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*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pid, pid1;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "Fork Failed");
    return 1;
  } else if (pid == 0) {
    pid1 = getpid();
    printf("child: pid = %d\n", pid);
    printf("child: pid1 = %d\n", pid1);
  } else {
    pid1 = getpid();
    wait(NULL);
    printf("parent: pid = %d\n", pid);
    printf("parent: pid1 = %d\n", pid1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1196 <main+0x46>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1211 <main+0xc1>
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <main+0x81>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
jmp    1205 <main+0xb5>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
mov    -0x10(%rbp),%rsi
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
jmp    120a <main+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
js     1195 <main+0x45>
mov    %rax,%rbx
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     11b2 <main+0x62>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xeab(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xeae(%rip),%rdi
jmp    11ca <main+0x7a>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%r15d
jmp    11d2 <main+0x82>
lea    0xe53(%rip),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
call   1040 <printf@plt>
lea    0xe53(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.1 = [17 x i8] c"child: pid = %d\0A\00"
@.str.2 = [18 x i8] c"child: pid1 = %d\0A\00"
@.str.3 = [18 x i8] c"parent: pid = %d\0A\00"
@.str.4 = [19 x i8] c"parent: pid1 = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %28
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = call i64 (...) @getpid()
store i64 %14, i64* %3
%15 = load i64, i64* %2
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = load i64, i64* %3
%18 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %17)
br label %26
19: ; preds = %10
%20 = call i64 (...) @getpid()
store i64 %20, i64* %3
%21 = call i32 @wait(i32* null)
%22 = load i64, i64* %2
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %22)
%24 = load i64, i64* %3
%25 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %24)
br label %26
26: ; preds = %19, %13
br label %27
27: ; preds = %26
store i32 0, i32* %1
br label %28
28: ; preds = %27, %7
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Fork Failed\00"
@.str.1 = [17 x i8] c"child: pid = %d\0A\00"
@.str.2 = [18 x i8] c"child: pid1 = %d\0A\00"
@.str.3 = [18 x i8] c"parent: pid = %d\0A\00"
@.str.4 = [19 x i8] c"parent: pid1 = %d\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %16
6: ; preds = %0
%7 = icmp eq i64 %1, 0
%8 = tail call i64 (...) @getpid()
br i1 %7, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 0)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 %8)
br label %16
12: ; preds = %6
%13 = tail call i32 @wait(i32* null)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %8)
br label %16
16: ; preds = %12, %9, %3
%17 = phi i32 [ 1, %3 ], [ 0, %9 ], [ 0, %12 ]
ret i32 %17
}