#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 pid_t;

/* Variables and functions */
int fork();
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main() {
  pid_t pid = fork();
  if (pid == -1) {
    perror("faled");
  } else if (pid == 0) {
    printf("child process pid:%d\n", getpid());
  } else if (pid > 0) {
    printf("parent process pid:%d\n", getpid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1174 <main+0x34>
lea    0xe96(%rip),%rdi
call   1050 <perror@plt>
jmp    11c1 <main+0x81>
cmpl   $0x0,-0x8(%rbp)
jne    1198 <main+0x58>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe78(%rip),%rdi
call   1040 <printf@plt>
jmp    11bc <main+0x7c>
cmpl   $0x0,-0x8(%rbp)
jle    11b7 <main+0x77>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
jmp    11bc <main+0x7c>
jmp    11c1 <main+0x81>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %eax,%eax
je     1165 <main+0x25>
cmp    $0xffffffff,%eax
je     117e <main+0x3e>
test   %eax,%eax
jle    117a <main+0x3a>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeb9(%rip),%rdi
jmp    1173 <main+0x33>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe93(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
jmp    117a <main+0x3a>

LLVM IR (-O0):
@.str = [6 x i8] c"faled\00"
@.str.1 = [22 x i8] c"child process pid:%d\0A\00"
@.str.2 = [23 x i8] c"parent process pid:%d\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
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %22
8: ; preds = %0
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = call i32 (...) @getpid()
%13 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %21
14: ; preds = %8
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %14
br label %21
21: ; preds = %20, %11
br label %22
22: ; preds = %21, %6
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"faled\00"
@.str.1 = [22 x i8] c"child process pid:%d\0A\00"
@.str.2 = [23 x i8] c"parent process pid:%d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %7 [
i32 -1, label %2
i32 0, label %4
]
2: ; preds = %0
%3 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %12
4: ; preds = %0
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %12
7: ; preds = %0
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %4, %9, %7, %2
ret i32 0
}