#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 WEXITSTATUS(int);
scalar_t__ WIFCONTINUED(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int wait(int *);
int main(void) {
  int status;
  pid_t pid = 1;
  pid = fork();
  printf("pid %d\n", pid);
  if (pid == 0) {
    for (int i = 0; i < 10; i++) {
      printf("in child \n");
    }
    return 1;
  } else if (pid > 0) {
    printf("in parent\n");
  } else {
    perror("fork error\n");
  }
  pid = wait(&status);
  if (pid == -1)
    perror("wait");
  printf("pid=%d\n", pid);
  if (WIFSTOPPED(status))
    printf("Stopped by signal=%d\n", WSTOPSIG(status));
  if (WIFCONTINUED(status))
    printf("Continued\n");
  if (WIFEXITED(status))
    printf("Normal termination with exit status=%d\n", WEXITSTATUS(status));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1204 <main+0x74>
movl   $0x0,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jge    11f8 <main+0x68>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11d2 <main+0x42>
movl   $0x1,-0x4(%rbp)
jmp    12e0 <main+0x150>
cmpl   $0x0,-0xc(%rbp)
jle    1221 <main+0x91>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122d <main+0x9d>
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
jmp    1232 <main+0xa2>
lea    -0x8(%rbp),%rdi
call   10a0 <wait@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1254 <main+0xc4>
lea    0xddb(%rip),%rdi
call   1070 <perror@plt>
mov    -0xc(%rbp),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     128f <main+0xff>
mov    -0x8(%rbp),%edi
call   1090 <WSTOPSIG@plt>
mov    %eax,%esi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     12af <main+0x11f>
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12d9 <main+0x149>
mov    -0x8(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
je     1269 <main+0xc9>
jle    12d1 <main+0x131>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12e2 <main+0x142>
lea    0xe25(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <WIFSTOPPED@plt>
test   %rax,%rax
je     1224 <main+0x84>
mov    0xc(%rsp),%edi
call   10a0 <WSTOPSIG@plt>
lea    0xe06(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1050 <WIFCONTINUED@plt>
test   %rax,%rax
je     123e <main+0x9e>
lea    0xe26(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
je     12c7 <main+0x127>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xddb(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12c9 <main+0x129>
lea    0xe03(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    12c9 <main+0x129>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd30(%rip),%rdi
call   1080 <perror@plt>
jmp    11d8 <main+0x38>
lea    0xd2b(%rip),%rdi
call   1080 <perror@plt>
jmp    11ed <main+0x4d>

LLVM IR (-O0):
@.str = [8 x i8] c"pid %d\0A\00"
@.str.1 = [11 x i8] c"in child \0A\00"
@.str.2 = [11 x i8] c"in parent\0A\00"
@.str.3 = [12 x i8] c"fork error\0A\00"
@.str.4 = [5 x i8] c"wait\00"
@.str.5 = [8 x i8] c"pid=%d\0A\00"
@.str.6 = [22 x i8] c"Stopped by signal=%d\0A\00"
@.str.7 = [11 x i8] c"Continued\0A\00"
@.str.8 = [40 x i8] c"Normal termination with exit status=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %3
%5 = call i32 (...) @fork()
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %0
store i32 0, i32* %4
br label %11
11: ; preds = %16, %10
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 10
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14
%17 = load i32, i32* %4
%18 = add nsw i32 %17, 1
store i32 %18, i32* %4
br label %11
19: ; preds = %11
store i32 1, i32* %1
br label %59
20: ; preds = %0
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %23
br label %28
28: ; preds = %27
%29 = call i32 @wait(i32* %2)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = load i32, i32* %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %2
%38 = call i64 @WIFSTOPPED(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* %2
%42 = call i32 @WSTOPSIG(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %40, %34
%45 = load i32, i32* %2
%46 = call i64 @WIFCONTINUED(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
%51 = load i32, i32* %2
%52 = call i64 @WIFEXITED(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32, i32* %2
%56 = call i32 @WEXITSTATUS(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i32 %56)
br label %58
58: ; preds = %54, %50
store i32 0, i32* %1
br label %59
59: ; preds = %58, %19
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
@.str = [8 x i8] c"pid %d\0A\00"
@.str.3 = [12 x i8] c"fork error\0A\00"
@.str.4 = [5 x i8] c"wait\00"
@.str.5 = [8 x i8] c"pid=%d\0A\00"
@.str.6 = [22 x i8] c"Stopped by signal=%d\0A\00"
@.str.8 = [40 x i8] c"Normal termination with exit status=%d\0A\00"
@str = [10 x i8] c"Continued\00"
@str.9 = [10 x i8] c"in parent\00"
@str.10 = [10 x i8] c"in child \00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %3, 0
br i1 %5, label %6, label %17
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.10, i64 0, i64 0))
br label %51
17: ; preds = %0
%18 = icmp sgt i32 %3, 0
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.9, i64 0, i64 0))
br label %23
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %19, %21
%24 = call i32 @wait(i32* %1)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %24)
%30 = load i32, i32* %1
%31 = call i64 @WIFSTOPPED(i32 %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = load i32, i32* %1
%35 = call i32 @WSTOPSIG(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %33, %28
%38 = load i32, i32* %1
%39 = call i64 @WIFCONTINUED(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %37
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = load i32, i32* %1
%45 = call i64 @WIFEXITED(i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %43
%48 = load i32, i32* %1
%49 = call i32 @WEXITSTATUS(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.8, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %6, %43, %47
%52 = phi i32 [ 0, %47 ], [ 0, %43 ], [ 1, %6 ]
ret i32 %52
}