#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 /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int main() {
  int count = 0;
  pid_t pid = fork();
  if (pid == -1) {
    perror("faled");
  } else if (pid == 0) {
    count++;
    printf("child process pid:%d\n", count);
  } else if (pid > 0) {
    count++;
    printf("parent process pid:%d\n", count);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    116b <main+0x3b>
lea    0xe9f(%rip),%rdi
call   1040 <perror@plt>
jmp    11be <main+0x8e>
cmpl   $0x0,-0xc(%rbp)
jne    1192 <main+0x62>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
jmp    11b9 <main+0x89>
cmpl   $0x0,-0xc(%rbp)
jle    11b4 <main+0x84>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
jmp    11b9 <main+0x89>
jmp    11be <main+0x8e>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     114e <main+0x1e>
cmp    $0xffffffff,%eax
je     1163 <main+0x33>
test   %eax,%eax
jle    115f <main+0x2f>
lea    0xed0(%rip),%rdi
jmp    1155 <main+0x25>
lea    0xeb1(%rip),%rdi
mov    $0x1,%esi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe96(%rip),%rdi
call   1040 <perror@plt>
jmp    115f <main+0x2f>

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
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%4 = call i32 (...) @fork()
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %27
9: ; preds = %0
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %26
17: ; preds = %9
%18 = load i32, i32* %3
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* %2
%22 = add nsw i32 %21, 1
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %20, %17
br label %26
26: ; preds = %25, %12
br label %27
27: ; preds = %26, %7
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 %6 [
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 %10
4: ; preds = %0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %10
6: ; preds = %0
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %10
10: ; preds = %4, %8, %6, %2
ret i32 0
}