#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 */
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid;
  char buf[] = "a write to stdout\n";
  write(1, buf, strlen(buf));
  printf("before fork\n");
  pid = fork();
  if (pid < 0) {
    perror("fork");
  }
  if (pid == 0) {
    printf("in son process\n");
  } else {
    sleep(1);
    printf("in father process\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe83(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe80(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe7d(%rip),%ax
mov    %ax,-0x20(%rbp)
mov    0xe75(%rip),%al
mov    %al,-0x1e(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
lea    0xe4b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11ef <main+0x8f>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
cmpq   $0x0,-0x18(%rbp)
jne    120b <main+0xab>
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
jmp    1221 <main+0xc1>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movaps 0xe94(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0xa7475,0xf(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11d3 <main+0x73>
je     11bf <main+0x5f>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe62(%rip),%rdi
jmp    11c6 <main+0x66>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
jmp    11ac <main+0x4c>

LLVM IR (-O0):
@__const.main.buf = [19 x i8] c"a write to stdout\0A\00"
@.str = [13 x i8] c"before fork\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [16 x i8] c"in son process\0A\00"
@.str.3 = [19 x i8] c"in father process\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [19 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast [19 x i8]* %7 to i8*
%9 = getelementptr inbounds [19 x i8], [19 x i8]* %7, i64 0, i64 0
%10 = getelementptr inbounds [19 x i8], [19 x i8]* %7, i64 0, i64 0
%11 = call i32 @strlen(i8* %10)
%12 = call i32 @write(i32 1, i8* %9, i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = call i64 (...) @fork()
store i64 %14, i64* %6
%15 = load i64, i64* %6
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load i64, i64* %6
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %27
24: ; preds = %19
%25 = call i32 @sleep(i32 1)
%26 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %24, %22
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [19 x i8] c"a write to stdout\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@str = [12 x i8] c"before fork\00"
@str.4 = [18 x i8] c"in father process\00"
@str.5 = [15 x i8] c"in son process\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [19 x i8]
%4 = getelementptr inbounds [19 x i8], [19 x i8]* %3, i64 0, i64 0
%5 = call i32 @strlen(i8* %4)
%6 = call i32 @write(i32 1, i8* %4, i32 %5)
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %14
12: ; preds = %2
%13 = icmp eq i64 %8, 0
br i1 %13, label %16, label %14
14: ; preds = %10, %12
%15 = call i32 @sleep(i32 1)
br label %16
16: ; preds = %12, %14
%17 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0), %14 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0), %12 ]
%18 = call i32 @puts(i8* dereferenceable(1) %17)
ret i32 0
}