#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 sys_fork();
int /*<<< orphan*/ sys_puts(char *);
int /*<<< orphan*/ sys_yield();
int main(void) {
  char child_buffer[] = "child: 0\n";
  char *child_ptr = child_buffer + 7;
  char parent_buffer[] = "parent: 0\n";
  char *parent_ptr = parent_buffer + 8;
  for (int i = 0; i < 3; i++) {
    int r = sys_fork();
    if (r == 0) {
      *child_ptr = '0' + i;
      sys_puts(child_buffer);
      return 1;
    } else if (r == -1) {
      sys_puts("can't fork\n");
      return -1;
    } else {
      *parent_ptr = 'a' + i;
      sys_puts(parent_buffer);
      sys_yield();
      sys_puts(parent_buffer);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0xe(%rbp)
mov    0xeb7(%rip),%ax
mov    %ax,-0x6(%rbp)
lea    -0xe(%rbp),%rax
add    $0x7,%rax
mov    %rax,-0x18(%rbp)
mov    0xea2(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe9f(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe97(%rip),%al
mov    %al,-0x19(%rbp)
lea    -0x23(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x3,-0x34(%rbp)
jge    1235 <main+0x105>
mov    $0x0,%al
call   1030 <sys_fork@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jne    11d4 <main+0xa4>
mov    -0x34(%rbp),%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
lea    -0xe(%rbp),%rdi
call   1050 <sys_puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    123c <main+0x10c>
cmpl   $0xffffffff,-0x38(%rbp)
jne    11f6 <main+0xc6>
lea    0xe30(%rip),%rdi
call   1050 <sys_puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123c <main+0x10c>
mov    -0x34(%rbp),%eax
add    $0x61,%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
lea    -0x23(%rbp),%rdi
call   1050 <sys_puts@plt>
mov    $0x0,%al
call   1040 <sys_yield@plt>
lea    -0x23(%rbp),%rdi
call   1050 <sys_puts@plt>
jmp    1222 <main+0xf2>
jmp    1227 <main+0xf7>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1193 <main+0x63>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movabs $0x30203a646c696863,%rax
3a 20 30
mov    %rax,0x18(%rsp)
movw   $0xa,0x20(%rsp)
movabs $0x203a746e65726170,%rax
74 3a 20
mov    %rax,0x8(%rsp)
movl   $0xa3020,0xf(%rsp)
xor    %eax,%eax
call   1030 <sys_fork@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0xc5>
mov    $0x30,%cl
test   %eax,%eax
je     11e0 <main+0xb0>
movb   $0x61,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sys_puts@plt>
xor    %eax,%eax
call   1040 <sys_yield@plt>
mov    %rbx,%rdi
call   1050 <sys_puts@plt>
xor    %eax,%eax
call   1030 <sys_fork@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0xc5>
mov    $0x31,%cl
test   %eax,%eax
je     11e0 <main+0xb0>
movb   $0x62,0x10(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sys_puts@plt>
xor    %eax,%eax
call   1040 <sys_yield@plt>
mov    %rbx,%rdi
call   1050 <sys_puts@plt>
xor    %eax,%eax
call   1030 <sys_fork@plt>
cmp    $0xffffffff,%eax
je     11f5 <main+0xc5>
mov    $0x32,%cl
test   %eax,%eax
jne    1210 <main+0xe0>
mov    %cl,0x1f(%rsp)
lea    0x18(%rsp),%rdi
call   1050 <sys_puts@plt>
mov    $0x1,%ebx
jmp    1206 <main+0xd6>
lea    0xe19(%rip),%rdi
call   1050 <sys_puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
movb   $0x63,0x10(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1050 <sys_puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <sys_yield@plt>
mov    %r14,%rdi
call   1050 <sys_puts@plt>
jmp    1206 <main+0xd6>

LLVM IR (-O0):
@__const.main.child_buffer = [10 x i8] c"child: 0\0A\00"
@__const.main.parent_buffer = [11 x i8] c"parent: 0\0A\00"
@.str = [12 x i8] c"can't fork\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i8*
%4 = alloca [11 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [10 x i8]* %2 to i8*
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds i8, i8* %9, i64 7
store i8* %10, i8** %3
%11 = bitcast [11 x i8]* %4 to i8*
%12 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds i8, i8* %12, i64 8
store i8* %13, i8** %5
store i32 0, i32* %6
br label %14
14: ; preds = %45, %0
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %48
17: ; preds = %14
%18 = call i32 (...) @sys_fork()
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i32, i32* %6
%23 = add nsw i32 48, %22
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %3
store i8 %24, i8* %25
%26 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%27 = call i32 @sys_puts(i8* %26)
store i32 1, i32* %1
br label %49
28: ; preds = %17
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @sys_puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %49
33: ; preds = %28
%34 = load i32, i32* %6
%35 = add nsw i32 97, %34
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %5
store i8 %36, i8* %37
%38 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%39 = call i32 @sys_puts(i8* %38)
%40 = call i32 (...) @sys_yield()
%41 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%42 = call i32 @sys_puts(i8* %41)
br label %43
43: ; preds = %33
br label %44
44: ; preds = %43
br label %45
45: ; preds = %44
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %14
48: ; preds = %14
store i32 0, i32* %1
br label %49
49: ; preds = %48, %31, %21
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@__const.main.child_buffer = [10 x i8] c"child: 0\0A\00"
@__const.main.parent_buffer = [11 x i8] c"parent: 0\0A\00"
@.str = [12 x i8] c"can't fork\0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = alloca [11 x i8]
%3 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 7
%5 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 8
%7 = call i32 (...) @sys_fork()
switch i32 %7, label %13 [
i32 0, label %8
i32 -1, label %11
]
8: ; preds = %18, %13, %0
%9 = phi i8 [ 48, %0 ], [ 49, %13 ], [ 50, %18 ]
store i8 %9, i8* %4
%10 = call i32 @sys_puts(i8* %3)
br label %27
11: ; preds = %18, %13, %0
%12 = call i32 @sys_puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %27
13: ; preds = %0
store i8 97, i8* %6
%14 = call i32 @sys_puts(i8* %5)
%15 = call i32 (...) @sys_yield()
%16 = call i32 @sys_puts(i8* %5)
%17 = call i32 (...) @sys_fork()
switch i32 %17, label %18 [
i32 0, label %8
i32 -1, label %11
]
18: ; preds = %13
store i8 98, i8* %6
%19 = call i32 @sys_puts(i8* %5)
%20 = call i32 (...) @sys_yield()
%21 = call i32 @sys_puts(i8* %5)
%22 = call i32 (...) @sys_fork()
switch i32 %22, label %23 [
i32 0, label %8
i32 -1, label %11
]
23: ; preds = %18
store i8 99, i8* %6
%24 = call i32 @sys_puts(i8* %5)
%25 = call i32 (...) @sys_yield()
%26 = call i32 @sys_puts(i8* %5)
br label %27
27: ; preds = %23, %8, %11
%28 = phi i32 [ 1, %8 ], [ -1, %11 ], [ 0, %23 ]
ret i32 %28
}