#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 /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  pid_t pid;
  int idx = -1;
  for (int i = 0; i < 4; i++) {
    pid = fork();
    if (pid < 0) {
      printf("fork failed\n");
    } else if (pid == 0) {
      printf("this is sub process, pid %d\n", getpid());
    } else {
      printf(
          "this is parent process, parent process is %d, sub process is %d\n",
          getpid(), pid);
    }
  }
  printf("hello pid: %d\n", getpid());
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    11ea <main+0xaa>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1190 <main+0x50>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d7 <main+0x97>
cmpq   $0x0,-0x10(%rbp)
jne    11b7 <main+0x77>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d2 <main+0x92>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d7 <main+0x97>
jmp    11dc <main+0x9c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115d <main+0x1d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jns    116b <main+0x1b>
lea    0xf09(%rip),%rdi
call   1030 <puts@plt>
jmp    119f <main+0x4f>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
jne    118c <main+0x3c>
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    119f <main+0x4f>
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jns    11b9 <main+0x69>
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
jmp    11ed <main+0x9d>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
jne    11da <main+0x8a>
lea    0xe31(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11ed <main+0x9d>
lea    0xe3c(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jns    1207 <main+0xb7>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
jmp    123b <main+0xeb>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
jne    1228 <main+0xd8>
lea    0xde3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    123b <main+0xeb>
lea    0xdee(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jns    1255 <main+0x105>
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
jmp    1289 <main+0x139>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
jne    1276 <main+0x126>
lea    0xd95(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1289 <main+0x139>
lea    0xda0(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdc7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"fork failed\0A\00"
@.str.1 = [29 x i8] c"this is sub process, pid %d\0A\00"
@.str.2 = [65 x i8] c"this is parent process, parent process is %d, sub process is %d\0A\00"
@.str.3 = [15 x i8] c"hello pid: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 -1, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %26, %0
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 4
br i1 %7, label %8, label %29
8: ; preds = %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %25
14: ; preds = %8
%15 = load i64, i64* %2
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %24
20: ; preds = %14
%21 = call i32 (...) @getpid()
%22 = load i64, i64* %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %21, i64 %22)
br label %24
24: ; preds = %20, %17
br label %25
25: ; preds = %24, %12
br label %26
26: ; preds = %25
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %5
29: ; preds = %5
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [29 x i8] c"this is sub process, pid %d\0A\00"
@.str.2 = [65 x i8] c"this is parent process, parent process is %d, sub process is %d\0A\00"
@.str.3 = [15 x i8] c"hello pid: %d\0A\00"
@str = [12 x i8] c"fork failed\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 ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %12
5: ; preds = %0
%6 = icmp eq i64 %1, 0
%7 = tail call i32 (...) @getpid()
br i1 %6, label %8, label %10
8: ; preds = %5
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %12
10: ; preds = %5
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %7, i64 %1)
br label %12
12: ; preds = %3, %10, %8
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %22, label %15
15: ; preds = %12
%16 = icmp eq i64 %13, 0
%17 = tail call i32 (...) @getpid()
br i1 %16, label %20, label %18
18: ; preds = %15
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %17, i64 %13)
br label %24
20: ; preds = %15
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %24
22: ; preds = %12
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %22, %20, %18
%25 = tail call i64 (...) @fork()
%26 = icmp slt i64 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %24
%28 = icmp eq i64 %25, 0
%29 = tail call i32 (...) @getpid()
br i1 %28, label %32, label %30
30: ; preds = %27
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %29, i64 %25)
br label %36
32: ; preds = %27
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %36
34: ; preds = %24
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %34, %32, %30
%37 = tail call i64 (...) @fork()
%38 = icmp slt i64 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %36
%40 = icmp eq i64 %37, 0
%41 = tail call i32 (...) @getpid()
br i1 %40, label %44, label %42
42: ; preds = %39
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %41, i64 %37)
br label %48
44: ; preds = %39
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %41)
br label %48
46: ; preds = %36
%47 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %48
48: ; preds = %46, %44, %42
%49 = tail call i32 (...) @getpid()
%50 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = tail call i32 @exit(i32 0)
unreachable
}