#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 int pid_t;

/* Variables and functions */
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
int wait_process(pid_t pid) {
  int status = -1;
  while (pid != -1) {
    pid = waitpid(pid, &status, 0);
    if (pid == -1) {
      fprintf(stderr, "waitpid error...\n");
      return -1;
    } else if (WIFEXITED(status) || WIFSIGNALED(status)) {
      fprintf(stderr, "process (pid %d) exited with status %d\n", pid, status);
      break;
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<wait_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11ea <wait_process+0xaa>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119a <wait_process+0x5a>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <wait_process+0xb0>
mov    -0xc(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
jne    11be <wait_process+0x7e>
mov    -0xc(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11e0 <wait_process+0xa0>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11ea <wait_process+0xaa>
jmp    11e5 <wait_process+0xa5>
jmp    1152 <wait_process+0x12>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_process>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0xffffffff,%ebx
je     11b1 <wait_process+0x71>
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11b7 <wait_process+0x77>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    1194 <wait_process+0x54>
mov    0x4(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     1160 <wait_process+0x20>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x4(%rsp),%ecx
lea    0xe6a(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x4(%rsp),%eax
jmp    11d3 <wait_process+0x93>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"waitpid error...\0A\00"
@.str.1 = [40 x i8] c"process (pid %d) exited with status %d\0A\00"
define i32 @wait_process(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %4
br label %5
5: ; preds = %30, %1
%6 = load i32, i32* %3
%7 = icmp ne i32 %6, -1
br i1 %7, label %8, label %31
8: ; preds = %5
%9 = load i32, i32* %3
%10 = call i32 @waitpid(i32 %9, i32* %4, i32 0)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %33
16: ; preds = %8
%17 = load i32, i32* %4
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* %4
%22 = call i64 @WIFSIGNALED(i32 %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20, %16
%25 = load i32, i32* @stderr
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %27)
br label %31
29: ; preds = %20
br label %30
30: ; preds = %29
br label %5
31: ; preds = %24, %5
%32 = load i32, i32* %4
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %13
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"waitpid error...\0A\00"
@.str.1 = [40 x i8] c"process (pid %d) exited with status %d\0A\00"
define i32 @wait_process(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 -1, i32* %2
br label %4
4: ; preds = %17, %1
%5 = phi i32 [ %0, %1 ], [ %8, %17 ]
%6 = icmp eq i32 %5, -1
br i1 %6, label %25, label %7
7: ; preds = %4
%8 = call i32 @waitpid(i32 %5, i32* %2, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %27
13: ; preds = %7
%14 = load i32, i32* %2
%15 = call i64 @WIFEXITED(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* %2
%19 = call i64 @WIFSIGNALED(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %4, label %21
21: ; preds = %17, %13
%22 = load i32, i32* @stderr
%23 = load i32, i32* %2
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 %23)
br label %25
25: ; preds = %4, %21
%26 = load i32, i32* %2
br label %27
27: ; preds = %25, %10
%28 = phi i32 [ -1, %10 ], [ %26, %25 ]
ret i32 %28
}