#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 /*<<< orphan*/ PTRACE_SYSCALL;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ ptrace(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                  int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
int wait_for_syscall(pid_t child) {
  int status;
  for (;;) {
    if (ptrace(PTRACE_SYSCALL, child, 0, 0) < 0) {
      perror("PTRACE_SYSCALL");
      return -1;
    }
    if (wait(&status) < 0) {
      perror("wait");
      return -1;
    }
    if (WIFSTOPPED(status))
      return 0;
    if (WIFEXITED(status))
      return -1;
    fprintf(stderr, "[not stopped]\n");
  }
}


x86 assembly (clang, -O0):
<wait_for_syscall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1050 <ptrace@plt>
cmp    $0x0,%rax
jge    11a2 <wait_for_syscall+0x42>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <wait_for_syscall+0xc3>
lea    -0xc(%rbp),%rdi
call   1080 <wait@plt>
cmp    $0x0,%rax
jge    11cd <wait_for_syscall+0x6d>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <wait_for_syscall+0xc3>
mov    -0xc(%rbp),%edi
call   1040 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     11eb <wait_for_syscall+0x8b>
movl   $0x0,-0x4(%rbp)
jmp    1223 <wait_for_syscall+0xc3>
mov    -0xc(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1209 <wait_for_syscall+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <wait_for_syscall+0xc3>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
call   1030 <fprintf@plt>
jmp    116b <wait_for_syscall+0xb>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_syscall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e5c(%rip),%r13
mov    0x0(%r13),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
lea    0xe76(%rip),%r14
test   %rax,%rax
js     1205 <wait_for_syscall+0xa5>
lea    0x4(%rsp),%r15
mov    0x2e45(%rip),%rbp
lea    0xe72(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
call   1080 <wait@plt>
test   %rax,%rax
js     11fe <wait_for_syscall+0x9e>
mov    0x4(%rsp),%edi
call   1040 <WIFSTOPPED@plt>
test   %rax,%rax
jne    11fa <wait_for_syscall+0x9a>
mov    0x4(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
jne    120d <wait_for_syscall+0xad>
mov    0x0(%rbp),%edi
mov    %r12,%rsi
call   1030 <fprintf@plt>
mov    0x0(%r13),%edi
mov    %ebx,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <ptrace@plt>
test   %rax,%rax
jns    11b0 <wait_for_syscall+0x50>
jmp    1205 <wait_for_syscall+0xa5>
xor    %eax,%eax
jmp    1212 <wait_for_syscall+0xb2>
lea    0xe0a(%rip),%r14
mov    %r14,%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PTRACE_SYSCALL = global i32 0
@.str = [15 x i8] c"PTRACE_SYSCALL\00"
@.str.1 = [5 x i8] c"wait\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"[not stopped]\0A\00"
define i32 @wait_for_syscall(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
br label %5
5: ; preds = %27, %1
%6 = load i32, i32* @PTRACE_SYSCALL
%7 = load i32, i32* %3
%8 = call i64 @ptrace(i32 %6, i32 %7, i32 0, i32 0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
12: ; preds = %5
%13 = call i64 @wait(i32* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
17: ; preds = %12
%18 = load i32, i32* %4
%19 = call i64 @WIFSTOPPED(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 0, i32* %2
br label %30
22: ; preds = %17
%23 = load i32, i32* %4
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %2
br label %30
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %5
30: ; preds = %26, %21, %15, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@PTRACE_SYSCALL = global i32 0
@.str = [15 x i8] c"PTRACE_SYSCALL\00"
@.str.1 = [5 x i8] c"wait\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"[not stopped]\0A\00"
define i32 @wait_for_syscall(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @PTRACE_SYSCALL
%5 = call i64 @ptrace(i32 %4, i32 %0, i32 0, i32 0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %1, %18
%8 = call i64 @wait(i32* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %7
%11 = load i32, i32* %2
%12 = call i64 @WIFSTOPPED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %10
%15 = load i32, i32* %2
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @PTRACE_SYSCALL
%22 = call i64 @ptrace(i32 %21, i32 %0, i32 0, i32 0)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %7
24: ; preds = %7, %18, %1
%25 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %18 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %7 ]
%26 = call i32 @perror(i8* %25)
br label %27
27: ; preds = %14, %10, %24
%28 = phi i32 [ -1, %24 ], [ -1, %14 ], [ 0, %10 ]
ret i32 %28
}