#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 */
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int get_eip(int /*<<< orphan*/);
int /*<<< orphan*/ procprint(char *);
int /*<<< orphan*/ wait(int *);
void xibugger_wait(pid_t pid) {
  int status;
  wait(&status);
  if (WIFSTOPPED(status)) {
    int eip = get_eip(pid);
    if (eip == 0) {
      procprint("target exited already.\n");
      exit(0);
    }
    return;
  }
  if (WIFEXITED(status)) {
    procprint("target exited already.\n");
    exit(0);
  }
  assert(0);
}


x86 assembly (clang, -O0):
<xibugger_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1090 <wait@plt>
mov    -0x8(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     11c3 <xibugger_wait+0x53>
mov    -0x4(%rbp),%edi
call   1030 <get_eip@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11be <xibugger_wait+0x4e>
lea    0xe4e(%rip),%rdi
call   1060 <procprint@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    11ef <xibugger_wait+0x7f>
mov    -0x8(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e8 <xibugger_wait+0x78>
lea    0xe24(%rip),%rdi
call   1060 <procprint@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1040 <assert@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xibugger_wait>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    0xc(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     119c <xibugger_wait+0x2c>
mov    %ebx,%edi
call   1030 <get_eip@plt>
test   %eax,%eax
jne    11b1 <xibugger_wait+0x41>
jmp    11b7 <xibugger_wait+0x47>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
jne    11b7 <xibugger_wait+0x47>
xor    %edi,%edi
call   1040 <assert@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe42(%rip),%rdi
call   1060 <procprint@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"target exited already.\0A\00"
define void @xibugger_wait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @wait(i32* %3)
%6 = load i32, i32* %3
%7 = call i64 @WIFSTOPPED(i32 %6)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i32 @get_eip(i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i32 @procprint(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %9
br label %27
18: ; preds = %1
%19 = load i32, i32* %3
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @procprint(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %18
%26 = call i32 @assert(i32 0)
br label %27
27: ; preds = %25, %17
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"target exited already.\0A\00"
define void @xibugger_wait(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @wait(i32* %2)
%5 = load i32, i32* %2
%6 = call i64 @WIFSTOPPED(i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %1
%9 = call i32 @get_eip(i32 %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = call i32 @procprint(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %1
%15 = load i32, i32* %2
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = call i32 @procprint(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %14
%22 = call i32 @assert(i32 0)
br label %23
23: ; preds = %8, %21
ret void
}