#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 getpid();
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  pid_t result;
  result = fork();
  while (1) {
    result = fork();
    if (-1 == result) {
      printf("Fork error\n");
      break;
    } else if (result == 0) {
      printf("Child process id is %d\n", getpid());
    } else {
      printf("Father process id is %d\n", getpid());
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1174 <main+0x44>
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11bb <main+0x8b>
cmpl   $0x0,-0x8(%rbp)
jne    119a <main+0x6a>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b1 <main+0x81>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b6 <main+0x86>
jmp    1149 <main+0x19>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1060 <fork@plt>
lea    0xead(%rip),%r14
lea    0xebe(%rip),%r15
jmp    1173 <main+0x33>
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <fork@plt>
mov    %r14,%rbx
test   %eax,%eax
je     1160 <main+0x20>
cmp    $0xffffffff,%eax
je     118b <main+0x4b>
mov    %r15,%rbx
jmp    1160 <main+0x20>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str.1 = [24 x i8] c"Child process id is %d\0A\00"
@.str.2 = [25 x i8] c"Father process id is %d\0A\00"
@str = [11 x i8] c"Fork error\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
br label %2
2: ; preds = %7, %0
%3 = tail call i32 (...) @fork()
switch i32 %3, label %6 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
ret i32 -1
6: ; preds = %2
br label %7
7: ; preds = %2, %6
%8 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), %6 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), %2 ]
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %8, i32 %9)
br label %2
}