#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 /*<<< orphan*/ child_proc();
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  pid_t cpid;
  fprintf(stderr, "main process %d started!\n", getpid());
  cpid = fork();
  if (cpid == -1) {
    perror("fork");
  } else if (cpid == 0) {
    child_proc();
  }
  wait(NULL);
  printf("main exiting!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11cf <main+0x5f>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
jmp    11e5 <main+0x75>
cmpl   $0x0,-0x14(%rbp)
jne    11e0 <main+0x70>
mov    $0x0,%al
call   1060 <child_proc@plt>
jmp    11e5 <main+0x75>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <wait@plt>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe78(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11af <main+0x3f>
cmp    $0xffffffff,%eax
jne    11b6 <main+0x46>
lea    0xe72(%rip),%rdi
call   1070 <perror@plt>
jmp    11b6 <main+0x46>
xor    %eax,%eax
call   1060 <child_proc@plt>
xor    %edi,%edi
call   1080 <wait@plt>
lea    0xe5b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"main process %d started!\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [15 x i8] c"main exiting!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* @stderr
%8 = call i32 (...) @getpid()
%9 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 (...) @fork()
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
15: ; preds = %2
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 (...) @child_proc()
br label %20
20: ; preds = %18, %15
br label %21
21: ; preds = %20, %13
%22 = call i32 @wait(i32* null)
%23 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"main process %d started!\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@str = [14 x i8] c"main exiting!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @stderr
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 (...) @fork()
switch i32 %6, label %11 [
i32 -1, label %7
i32 0, label %9
]
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %11
9: ; preds = %2
%10 = tail call i32 (...) @child_proc()
br label %11
11: ; preds = %2, %9, %7
%12 = tail call i32 @wait(i32* null)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
ret i32 0
}