#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ resign();
int /*<<< orphan*/ status();
int main() {
  int pid = fork();
  if (pid == 0) {
    printf("Child: pid = %d\n", getpid());
    int i;
    for (i = 0; i < 10; i++) {
      status();
      resign();
    }
  } else {
    printf("Parent: pid = %d\n", getpid());
    int i;
    for (i = 0; i < 3; i++) {
      status();
      resign();
    }
    printf("Parent exits\n");
    exit(0);
  }
  exit(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    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11cc <main+0x6c>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    11c7 <main+0x67>
mov    $0x0,%al
call   1050 <status@plt>
mov    $0x0,%al
call   1070 <resign@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a1 <main+0x41>
jmp    1225 <main+0xc5>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x3,-0x10(%rbp)
jge    1210 <main+0xb0>
mov    $0x0,%al
call   1050 <status@plt>
mov    $0x0,%al
call   1070 <resign@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ea <main+0x8a>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %ebx,%ebx
jne    122c <main+0xbc>
lea    0xe70(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xdde(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
xor    %eax,%eax
call   1060 <status@plt>
xor    %eax,%eax
call   1080 <resign@plt>
lea    0xdb6(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@.str = [17 x i8] c"Child: pid = %d\0A\00"
@.str.1 = [18 x i8] c"Parent: pid = %d\0A\00"
@str = [13 x i8] c"Parent exits\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
%2 = icmp eq i32 %1, 0
%3 = tail call i32 (...) @getpid()
br i1 %2, label %4, label %27
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%6 = tail call i32 (...) @status()
%7 = tail call i32 (...) @resign()
%8 = tail call i32 (...) @status()
%9 = tail call i32 (...) @resign()
%10 = tail call i32 (...) @status()
%11 = tail call i32 (...) @resign()
%12 = tail call i32 (...) @status()
%13 = tail call i32 (...) @resign()
%14 = tail call i32 (...) @status()
%15 = tail call i32 (...) @resign()
%16 = tail call i32 (...) @status()
%17 = tail call i32 (...) @resign()
%18 = tail call i32 (...) @status()
%19 = tail call i32 (...) @resign()
%20 = tail call i32 (...) @status()
%21 = tail call i32 (...) @resign()
%22 = tail call i32 (...) @status()
%23 = tail call i32 (...) @resign()
%24 = tail call i32 (...) @status()
%25 = tail call i32 (...) @resign()
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %0
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%29 = tail call i32 (...) @status()
%30 = tail call i32 (...) @resign()
%31 = tail call i32 (...) @status()
%32 = tail call i32 (...) @resign()
%33 = tail call i32 (...) @status()
%34 = tail call i32 (...) @resign()
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%36 = tail call i32 @exit(i32 0)
unreachable
}