#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ perror(char *);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int wait_for_child(pid_t child_pid) {
  int rc;
  if (waitpid(child_pid, &rc, 0) == -1) {
    perror("waitpid");
    return 1;
  }
  if (WIFEXITED(rc))
    rc = WEXITSTATUS(rc);
  else
    rc = 1;
  return rc;
}


x86 assembly (clang, -O0):
<wait_for_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    117a <wait_for_child+0x3a>
lea    0xe97(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11a9 <wait_for_child+0x69>
mov    -0xc(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     119c <wait_for_child+0x5c>
mov    -0xc(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
mov    %eax,-0xc(%rbp)
jmp    11a3 <wait_for_child+0x63>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_child>:
push   %rax
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
je     116b <wait_for_child+0x2b>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     1177 <wait_for_child+0x37>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
pop    %rcx
ret
lea    0xe8e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
pop    %rcx
ret

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

LLVM IR (-O2):
@.str = [8 x i8] c"waitpid\00"
define i32 @wait_for_child(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %15
8: ; preds = %1
%9 = load i32, i32* %2
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = load i32, i32* %2
%14 = call i32 @WEXITSTATUS(i32 %13)
br label %15
15: ; preds = %12, %8, %6
%16 = phi i32 [ 1, %6 ], [ %14, %12 ], [ 1, %8 ]
ret i32 %16
}