#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main(void) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("Error in fork");
    return 0;
  }
  while (1) {
    if (pid == 0) {
      printf("I am the child process, ID = %i\n", getpid());
      sleep(1);
    } else {
      printf("I am the parent process, ID = %i\n", getppid());
      sleep(2);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1199 <main+0x39>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret
jmp    119e <main+0x3e>
cmpq   $0x0,-0x10(%rbp)
jne    11cd <main+0x6d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    11ec <main+0x8c>
mov    $0x0,%al
call   1060 <getppid@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    119e <main+0x3e>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11cb <main+0x6b>
mov    %rax,%rbx
lea    0xe93(%rip),%r14
lea    0xead(%rip),%r15
jmp    11a9 <main+0x49>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1030 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
test   %rbx,%rbx
je     1190 <main+0x30>
call   1060 <getppid@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
jmp    11a9 <main+0x49>
lea    0xe2e(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Error in fork\00"
@.str.1 = [33 x i8] c"I am the child process, ID = %i\0A\00"
@.str.2 = [34 x i8] c"I am the parent process, ID = %i\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 = load i64, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
ret i32 0
8: ; preds = %0
br label %9
9: ; preds = %8, %20
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = call i32 (...) @getpid()
%14 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = call i32 @sleep(i32 1)
br label %20
16: ; preds = %9
%17 = call i32 (...) @getppid()
%18 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = call i32 @sleep(i32 2)
br label %20
20: ; preds = %16, %12
br label %9
}

LLVM IR (-O2):
@.str = [14 x i8] c"Error in fork\00"
@.str.1 = [33 x i8] c"I am the child process, ID = %i\0A\00"
@.str.2 = [34 x i8] c"I am the parent process, ID = %i\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %5, label %3
3: ; preds = %0
%4 = icmp eq i64 %1, 0
br label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
ret i32 0
7: ; preds = %3, %14
br i1 %4, label %8, label %11
8: ; preds = %7
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %14
11: ; preds = %7
%12 = tail call i32 (...) @getppid()
%13 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11, %8
%15 = phi i32 [ 2, %11 ], [ 1, %8 ]
%16 = tail call i32 @sleep(i32 %15)
br label %7
}