#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 getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait();
int main() {
  pid_t p;
  if ((p = fork()) < 0) {
    printf("fork error\n");
    return 0;
  } else if (p == 0) {
    printf("i am child process\n");
    printf("my parent process id is:%d\n", getppid());
    printf("my process id is:%d\n", getpid());
  } else {
    wait();
    printf("i am parent process\n");
    printf("my child process id is:%d\n", p);
    printf("my own id is: %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   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    118e <main+0x3e>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1224 <main+0xd4>
cmpq   $0x0,-0x10(%rbp)
jne    11da <main+0x8a>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1218 <main+0xc8>
mov    $0x0,%al
call   1060 <wait@plt>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    121d <main+0xcd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11a6 <main+0x46>
je     11b4 <main+0x54>
mov    %rax,%rbx
xor    %eax,%eax
call   1070 <wait@plt>
lea    0xede(%rip),%rdi
call   1030 <puts@plt>
lea    0xea5(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xea8(%rip),%rdi
jmp    11e5 <main+0x85>
lea    0xed8(%rip),%rdi
call   1030 <puts@plt>
jmp    11ee <main+0x8e>
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
lea    0xe32(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@.str.2 = [28 x i8] c"my parent process id is:%d\0A\00"
@.str.3 = [21 x i8] c"my process id is:%d\0A\00"
@.str.5 = [27 x i8] c"my child process id is:%d\0A\00"
@.str.6 = [18 x i8] c"my own id is: %d\0A\00"
@str = [20 x i8] c"i am parent process\00"
@str.7 = [19 x i8] c"i am child process\00"
@str.8 = [11 x i8] c"fork error\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
br label %19
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %13
7: ; preds = %5
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
%9 = tail call i32 (...) @getppid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %11)
br label %19
13: ; preds = %5
%14 = tail call i32 (...) @wait()
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i64 %1)
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %13, %7, %3
ret i32 0
}