#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 fork();
int /*<<< orphan*/ ltoa(int /*<<< orphan*/, char *);
int open(char *, int);
int strlen(char *);
int /*<<< orphan*/ wastetime();
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd;
  char buf[21];
  long l = 0;
  char *label;
  int pid;
  fd = open("/dev/cons", (1 << 1));
  pid = fork();
  if (pid == 0) {
    label = "child: ";
  } else if (pid > 0) {
    label = "parent: ";
  } else {
    char *msg = "fork failed\n";
    write(fd, msg, strlen(msg));
    for (;;)
      ;
  }
  for (;;) {
    write(fd, label, strlen(label));
    ltoa(l++, buf);
    write(fd, buf, strlen(buf));
    write(fd, "\n", 1);
    wastetime();
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe7f(%rip),%rdi
mov    $0x2,%esi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11b2 <main+0x52>
lea    0xe61(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1206 <main+0xa6>
cmpl   $0x0,-0x34(%rbp)
jle    11cc <main+0x6c>
lea    0xe4f(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1201 <main+0xa1>
lea    0xe48(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
jmp    11fc <main+0x9c>
jmp    1206 <main+0xa6>
jmp    120b <main+0xab>
mov    -0x8(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   1030 <ltoa@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    0xda9(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x0,%al
call   1060 <wastetime@plt>
jmp    120b <main+0xab>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xe8d(%rip),%rdi
mov    $0x2,%esi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     1195 <main+0x35>
jle    11fd <main+0x9d>
lea    0xe7f(%rip),%r14
jmp    119c <main+0x3c>
lea    0xe6e(%rip),%r14
mov    %rsp,%r15
lea    0xe80(%rip),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1030 <ltoa@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %eax,%eax
call   1060 <wastetime@plt>
add    $0x1,%ebp
jmp    11b0 <main+0x50>
lea    0xe17(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
nopl   0x0(%rax,%rax,1)
jmp    1220 <main+0xc0>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/cons\00"
@.str.1 = [8 x i8] c"child: \00"
@.str.2 = [9 x i8] c"parent: \00"
@.str.3 = [13 x i8] c"fork failed\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [21 x i8]
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %1
store i64 0, i64* %4
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 2)
store i32 %8, i32* %2
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %5
br label %25
13: ; preds = %0
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8** %5
br label %24
17: ; preds = %13
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8** %7
%18 = load i32, i32* %2
%19 = load i8*, i8** %7
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = call i32 @write(i32 %18, i8* %19, i32 %21)
br label %23
23: ; preds = %23, %17
br label %23
24: ; preds = %16
br label %25
25: ; preds = %24, %12
br label %26
26: ; preds = %26, %25
%27 = load i32, i32* %2
%28 = load i8*, i8** %5
%29 = load i8*, i8** %5
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @write(i32 %27, i8* %28, i32 %30)
%32 = load i64, i64* %4
%33 = add nsw i64 %32, 1
store i64 %33, i64* %4
%34 = trunc i64 %32 to i32
%35 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%36 = call i32 @ltoa(i32 %34, i8* %35)
%37 = load i32, i32* %2
%38 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%39 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = call i32 @write(i32 %37, i8* %38, i32 %40)
%42 = load i32, i32* %2
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%44 = call i32 (...) @wastetime()
br label %26
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/cons\00"
@.str.1 = [8 x i8] c"child: \00"
@.str.2 = [9 x i8] c"parent: \00"
@.str.3 = [13 x i8] c"fork failed\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca [21 x i8]
%2 = getelementptr inbounds [21 x i8], [21 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 2)
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
br i1 %5, label %12, label %6
6: ; preds = %0
%7 = icmp sgt i32 %4, 0
br i1 %7, label %12, label %8
8: ; preds = %6
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%10 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %11, %8
br label %11
12: ; preds = %6, %0
%13 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %6 ]
br label %14
14: ; preds = %14, %12
%15 = phi i64 [ 0, %12 ], [ %18, %14 ]
%16 = call i32 @strlen(i8* %13)
%17 = call i32 @write(i32 %3, i8* %13, i32 %16)
%18 = add nuw nsw i64 %15, 1
%19 = trunc i64 %15 to i32
%20 = call i32 @ltoa(i32 %19, i8* %2)
%21 = call i32 @strlen(i8* %2)
%22 = call i32 @write(i32 %3, i8* %2, i32 %21)
%23 = call i32 @write(i32 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%24 = call i32 (...) @wastetime()
br label %14
}