#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 getpid();
int /*<<< orphan*/ in_child(int);
int /*<<< orphan*/ in_parent(scalar_t__);
int /*<<< orphan*/ printf(char *, int);
int main(int argc, char *argv[]) {
  printf("process start [%d]\n", getpid());
  int mpid = getpid();
  pid_t pid = fork();
  if (pid < 0) {
    printf("fork faild in process [%d]\n", getpid());
    return -1;
  } else if (0 == pid) {
    in_child(mpid);
  } else {
    in_parent(pid);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11bc <main+0x6c>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <main+0x9a>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11d5 <main+0x85>
mov    -0x14(%rbp),%edi
call   1050 <in_child@plt>
jmp    11de <main+0x8e>
mov    -0x20(%rbp),%rdi
call   1070 <in_parent@plt>
jmp    11e3 <main+0x93>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xea1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
js     1187 <main+0x37>
je     11a3 <main+0x53>
mov    %rax,%rdi
call   1070 <in_parent@plt>
jmp    11aa <main+0x5a>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret
mov    %ebx,%edi
call   1050 <in_child@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [20 x i8] c"process start [%d]\0A\00"
@.str.1 = [28 x i8] c"fork faild in process [%d]\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @getpid()
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %17
11: ; preds = %2
%12 = icmp eq i64 %6, 0
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @in_child(i32 %5)
br label %17
15: ; preds = %11
%16 = tail call i32 @in_parent(i64 %6)
br label %17
17: ; preds = %15, %13, %8
%18 = phi i32 [ -1, %8 ], [ 0, %13 ], [ 0, %15 ]
ret i32 %18
}