#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 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();
    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   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1186 <main+0x46>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
lea    0xe8b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f8 <main+0xb8>
cmpq   $0x0,-0x10(%rbp)
jne    11c1 <main+0x81>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
jmp    11ec <main+0xac>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe56(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1040 <printf@plt>
jmp    11f1 <main+0xb1>
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   1060 <fork@plt>
test   %rax,%rax
js     117e <main+0x3e>
mov    %rax,%rbx
xor    %r15d,%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     119b <main+0x5b>
lea    0xec0(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xec3(%rip),%rdi
jmp    11b3 <main+0x73>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%r15d
jmp    11bb <main+0x7b>
lea    0xe6a(%rip),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
call   1040 <printf@plt>
lea    0xe69(%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 = [16 x i8] c"child:pid = %d\0A\00"
@.str.2 = [17 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 %27
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 ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = load i64, i64* %3
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %17)
br label %25
19: ; preds = %10
%20 = call i64 (...) @getpid()
store i64 %20, i64* %3
%21 = load i64, i64* %2
%22 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %21)
%23 = load i64, i64* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %23)
br label %25
25: ; preds = %19, %13
br label %26
26: ; preds = %25
store i32 0, i32* %1
br label %27
27: ; preds = %26, %7
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Fork failed\00"
@.str.1 = [16 x i8] c"child:pid = %d\0A\00"
@.str.2 = [17 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 %15
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 ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i64 0)
%11 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 %8)
br label %15
12: ; preds = %6
%13 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i64 %8)
br label %15
15: ; preds = %12, %9, %3
%16 = phi i32 [ 1, %3 ], [ 0, %9 ], [ 0, %12 ]
ret i32 %16
}