#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int waitpid(int, int *, int /*<<< orphan*/);
int cmdwait(int pid) {
  int s;
  if (-1 == waitpid(pid, &s, 0))
    return -1;
  if (WIFEXITED(s))
    return WEXITSTATUS(s);
  else if (WIFSIGNALED(s))
    return (128 + WTERMSIG(s));
  return 0;
}


x86 assembly (clang, -O0):
<cmdwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1184 <cmdwait+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <cmdwait+0x89>
mov    -0xc(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11a6 <cmdwait+0x56>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    11d9 <cmdwait+0x89>
mov    -0xc(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11cd <cmdwait+0x7d>
mov    -0xc(%rbp),%edi
call   1040 <WTERMSIG@plt>
add    $0x80,%eax
mov    %eax,-0x4(%rbp)
jmp    11d9 <cmdwait+0x89>
jmp    11d2 <cmdwait+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmdwait>:
push   %rax
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     117b <cmdwait+0x2b>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     1182 <cmdwait+0x32>
call   1050 <WEXITSTATUS@plt>
pop    %rcx
ret
mov    $0xffffffff,%eax
pop    %rcx
ret
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     119a <cmdwait+0x4a>
mov    0x4(%rsp),%edi
call   1040 <WTERMSIG@plt>
sub    $0xffffff80,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @cmdwait(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 -1, %6
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %26
9: ; preds = %1
%10 = load i32, i32* %4
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %4
%15 = call i32 @WEXITSTATUS(i32 %14)
store i32 %15, i32* %2
br label %26
16: ; preds = %9
%17 = load i32, i32* %4
%18 = call i64 @WIFSIGNALED(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* %4
%22 = call i32 @WTERMSIG(i32 %21)
%23 = add nsw i32 128, %22
store i32 %23, i32* %2
br label %26
24: ; preds = %16
br label %25
25: ; preds = %24
store i32 0, i32* %2
br label %26
26: ; preds = %25, %20, %13, %8
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
define i32 @cmdwait(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 %20, label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
%10 = load i32, i32* %2
br i1 %9, label %13, label %11
11: ; preds = %6
%12 = call i32 @WEXITSTATUS(i32 %10)
br label %20
13: ; preds = %6
%14 = call i64 @WIFSIGNALED(i32 %10)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* %2
%18 = call i32 @WTERMSIG(i32 %17)
%19 = add nsw i32 %18, 128
br label %20
20: ; preds = %13, %1, %16, %11
%21 = phi i32 [ %12, %11 ], [ %19, %16 ], [ -1, %1 ], [ 0, %13 ]
ret i32 %21
}