#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 wait_for_process(scalar_t__);
int spawn_and_wait() {
  for (;;) {
    pid_t pid = fork();
    if (pid < 0) {
      perror("fork");
      return -1;
    } else if (pid == 0)
      return 0;
    else {
      if (wait_for_process(pid) == -1) {
        return -1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<spawn_and_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1166 <spawn_and_wait+0x36>
lea    0xeab(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <spawn_and_wait+0x7a>
cmpq   $0x0,-0x10(%rbp)
jne    117d <spawn_and_wait+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    11aa <spawn_and_wait+0x7a>
mov    -0x10(%rbp),%rdi
call   1030 <wait_for_process@plt>
cmp    $0xffffffff,%eax
jne    119b <spawn_and_wait+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <spawn_and_wait+0x7a>
jmp    11a0 <spawn_and_wait+0x70>
jmp    11a5 <spawn_and_wait+0x75>
jmp    1138 <spawn_and_wait+0x8>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_and_wait>:
push   %rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
js     1166 <spawn_and_wait+0x36>
je     1162 <spawn_and_wait+0x32>
mov    %rax,%rdi
call   1030 <wait_for_process@plt>
cmp    $0xffffffff,%eax
jne    1140 <spawn_and_wait+0x10>
mov    $0xffffffff,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret
lea    0xe93(%rip),%rdi
call   1040 <perror@plt>
jmp    115b <spawn_and_wait+0x2b>

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
define i32 @spawn_and_wait() {
%1 = alloca i32
%2 = alloca i64
br label %3
3: ; preds = %20, %0
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %21
9: ; preds = %3
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 0, i32* %1
br label %21
13: ; preds = %9
%14 = load i64, i64* %2
%15 = call i32 @wait_for_process(i64 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %1
br label %21
18: ; preds = %13
br label %19
19: ; preds = %18
br label %20
20: ; preds = %19
br label %3
21: ; preds = %17, %12, %7
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
define i32 @spawn_and_wait() {
br label %1
1: ; preds = %8, %0
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %11
6: ; preds = %1
%7 = icmp eq i64 %2, 0
br i1 %7, label %11, label %8
8: ; preds = %6
%9 = tail call i32 @wait_for_process(i64 %2)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %1
11: ; preds = %6, %8, %4
%12 = phi i32 [ -1, %4 ], [ 0, %6 ], [ -1, %8 ]
ret i32 %12
}