#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*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ yield();
int main(int argc, char *argv[], char *envp[]) {
  int fd = open("libc/printf.c", 0);
  int pid = fork();
  printf("Back from 1st fork fd : %d\n", fd);
  if (pid == 0) {
    printf("In child of parent1(hello1)\n");
    close(fd);
    exit(0);
  } else {
    printf("In parent(hello1) with child pid %d\n", pid);
    close(fd);
    yield();
    while (1)
      ;
    printf("Calling fork again\n");
    int pid2 = fork();
    if (pid2 == 0) {
      printf("SHOULDNT COME HERE\n");
    } else {
      printf("In parent(hello1) with 2nd child pid %d\n", pid);
      printf("Back in parent1(hello1)\n");
    }
  }
  while (1)
    ;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xe7f(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x20(%rbp)
jne    11cd <main+0x6d>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%esi
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   1030 <yield@plt>
jmp    11ed <main+0x8d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe86(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ebp
lea    0xe7b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebp,%ebp
je     11c2 <main+0x52>
lea    0xe83(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1040 <yield@plt>
xchg   %ax,%ax
jmp    11c0 <main+0x50>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"libc/printf.c\00"
@.str.1 = [28 x i8] c"Back from 1st fork fd : %d\0A\00"
@.str.2 = [29 x i8] c"In child of parent1(hello1)\0A\00"
@.str.3 = [37 x i8] c"In parent(hello1) with child pid %d\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%11 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 0)
store i32 %11, i32* %8
%12 = call i32 (...) @fork()
store i32 %12, i32* %9
%13 = load i32, i32* %8
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* %8
%20 = call i32 @close(i32 %19)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %3
%23 = load i32, i32* %9
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = call i32 (...) @yield()
br label %28
28: ; preds = %22, %28
br label %28
}

LLVM IR (-O2):
@.str = [14 x i8] c"libc/printf.c\00"
@.str.1 = [28 x i8] c"Back from 1st fork fd : %d\0A\00"
@.str.3 = [37 x i8] c"In parent(hello1) with child pid %d\0A\00"
@str = [28 x i8] c"In child of parent1(hello1)\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 0)
%5 = tail call i32 (...) @fork()
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%7 = icmp eq i32 %5, 0
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @close(i32 %4)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %3
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%14 = tail call i32 @close(i32 %4)
%15 = tail call i32 (...) @yield()
br label %16
16: ; preds = %16, %12
br label %16
}