#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 */
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  pid_t a;
  a = fork();
  printf("hello\n");
  if (a < 0) {
    perror("fork");
    exit(1);
  } else if (a == 0) {
    printf("child: PID=%d, PPID=%d\n", getpid(), getppid());
  } else if (a > 0) {
    printf("parent: PID=%d, PPID=%d\n", getpid(), getppid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
jge    11a9 <main+0x49>
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11dd <main+0x7d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1211 <main+0xb1>
cmpq   $0x0,-0x10(%rbp)
jle    120c <main+0xac>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1211 <main+0xb1>
jmp    1216 <main+0xb6>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
test   %rbx,%rbx
js     11c7 <main+0x57>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
test   %rbx,%rbx
lea    0xe5d(%rip),%rcx
lea    0xe6e(%rip),%rdi
cmove  %rcx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [24 x i8] c"child: PID=%d, PPID=%d\0A\00"
@.str.3 = [25 x i8] c"parent: PID=%d, PPID=%d\0A\00"
@str = [6 x i8] c"hello\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%3 = icmp slt i64 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
%8 = icmp eq i64 %1, 0
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (...) @getppid()
%11 = select i1 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %11, i32 %9, i32 %10)
ret i32 0
}