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

/* Variables and functions */
scalar_t__ ECHILD;
int /*<<< orphan*/ PT_CONTINUE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int _error(char const *, char *, int);
int _ptrace_child(char const *);
scalar_t__ errno;
scalar_t__ fork();
scalar_t__ ptrace(int /*<<< orphan*/, scalar_t__, void *, int /*<<< orphan*/);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int _ptrace(char const *progname) {
  int ret = 0;
  pid_t pid;
  pid_t p;
  int status;
  if ((pid = fork()) < 0)
    return -_error(progname, "fork", 1);
  if (pid == 0)
    return _ptrace_child(progname);
  for (;;)
    if ((p = waitpid(pid, &status, 0)) == pid) {
      if (WIFEXITED(status)) {
        ret = WEXITSTATUS(status);
        break;
      } else if (WIFSIGNALED(status)) {
        ret = 1;
        break;
      } else if (WIFSTOPPED(status) &&
                 ptrace(PT_CONTINUE, pid, (void *)1, 0) == 0)
        break;
    } else if (p == -1) {
      ret = -_error(progname, "waitpid", 1);
      if (errno == ECHILD)
        break;
    }
  return ret;
}


x86 assembly (clang, -O0):
<_ptrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    11db <_ptrace+0x4b>
mov    -0x10(%rbp),%rdi
lea    0xe3d(%rip),%rsi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12ff <_ptrace+0x16f>
cmpq   $0x0,-0x20(%rbp)
jne    11f7 <_ptrace+0x67>
mov    -0x10(%rbp),%rdi
call   1030 <_ptrace_child@plt>
mov    %eax,-0x4(%rbp)
jmp    12ff <_ptrace+0x16f>
jmp    11fc <_ptrace+0x6c>
mov    -0x20(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    %rax,-0x28(%rbp)
cmp    -0x20(%rbp),%rax
jne    12a2 <_ptrace+0x112>
mov    -0x2c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     123b <_ptrace+0xab>
mov    -0x2c(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x14(%rbp)
jmp    12f9 <_ptrace+0x169>
mov    -0x2c(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1259 <_ptrace+0xc9>
movl   $0x1,-0x14(%rbp)
jmp    12f9 <_ptrace+0x169>
mov    -0x2c(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1293 <_ptrace+0x103>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1070 <ptrace@plt>
cmp    $0x0,%rax
jne    1293 <_ptrace+0x103>
jmp    12f9 <_ptrace+0x169>
jmp    1298 <_ptrace+0x108>
jmp    129d <_ptrace+0x10d>
jmp    12f4 <_ptrace+0x164>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    12ef <_ptrace+0x15f>
mov    -0x10(%rbp),%rdi
lea    0xd4d(%rip),%rsi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    0x2d06(%rip),%rax
mov    (%rax),%rax
mov    0x2cf4(%rip),%rcx
cmp    (%rcx),%rax
jne    12ea <_ptrace+0x15a>
jmp    12f9 <_ptrace+0x169>
jmp    12ef <_ptrace+0x15f>
jmp    12f4 <_ptrace+0x164>
jmp    11fc <_ptrace+0x6c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_ptrace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1264 <_ptrace+0xd4>
je     1280 <_ptrace+0xf0>
mov    %rax,%rbx
xor    %r15d,%r15d
lea    0x4(%rsp),%rbp
mov    0x2e1a(%rip),%r13
mov    0x2e03(%rip),%r12
jmp    11d6 <_ptrace+0x46>
nop
cmp    $0xffffffffffffffff,%rax
je     1234 <_ptrace+0xa4>
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
cmp    %rbx,%rax
jne    11d0 <_ptrace+0x40>
mov    0x4(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
jne    1296 <_ptrace+0x106>
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
jne    12a0 <_ptrace+0x110>
mov    0x4(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     11d6 <_ptrace+0x46>
mov    0x0(%r13),%edi
mov    $0x1,%edx
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1070 <ptrace@plt>
test   %rax,%rax
jne    11d6 <_ptrace+0x46>
jmp    12a6 <_ptrace+0x116>
mov    %r14,%rdi
lea    0xdc7(%rip),%rsi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%r15d
neg    %r15d
mov    0x2d83(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
jne    11d6 <_ptrace+0x46>
jmp    12a6 <_ptrace+0x116>
lea    0xd95(%rip),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1060 <_error@plt>
mov    %eax,%r15d
neg    %r15d
jmp    12a6 <_ptrace+0x116>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <_ptrace_child@plt>
call   1080 <WEXITSTATUS@plt>
mov    %eax,%r15d
jmp    12a6 <_ptrace+0x116>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fork\00"
@PT_CONTINUE = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@errno = global i64 0
@ECHILD = global i64 0
define i32 @_ptrace(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
%8 = call i64 (...) @fork()
store i64 %8, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @_error(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1)
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %67
14: ; preds = %1
%15 = load i64, i64* %5
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %3
%19 = call i32 @_ptrace_child(i8* %18)
store i32 %19, i32* %2
br label %67
20: ; preds = %14
br label %21
21: ; preds = %64, %20
%22 = load i64, i64* %5
%23 = call i64 @waitpid(i64 %22, i32* %7, i32 0)
store i64 %23, i64* %6
%24 = load i64, i64* %5
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %51
26: ; preds = %21
%27 = load i32, i32* %7
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %7
%32 = call i32 @WEXITSTATUS(i32 %31)
store i32 %32, i32* %4
br label %65
33: ; preds = %26
%34 = load i32, i32* %7
%35 = call i64 @WIFSIGNALED(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 1, i32* %4
br label %65
38: ; preds = %33
%39 = load i32, i32* %7
%40 = call i64 @WIFSTOPPED(i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %38
%43 = load i32, i32* @PT_CONTINUE
%44 = load i64, i64* %5
%45 = call i64 @ptrace(i32 %43, i64 %44, i8* inttoptr (i64 1 to i8*), i32 0)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
br label %65
48: ; preds = %42, %38
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49
br label %64
51: ; preds = %21
%52 = load i64, i64* %6
%53 = icmp eq i64 %52, -1
br i1 %53, label %54, label %63
54: ; preds = %51
%55 = load i8*, i8** %3
%56 = call i32 @_error(i8* %55, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 1)
%57 = sub nsw i32 0, %56
store i32 %57, i32* %4
%58 = load i64, i64* @errno
%59 = load i64, i64* @ECHILD
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %54
br label %65
62: ; preds = %54
br label %63
63: ; preds = %62, %51
br label %64
64: ; preds = %63, %50
br label %21
65: ; preds = %61, %47, %37, %30
%66 = load i32, i32* %4
store i32 %66, i32* %2
br label %67
67: ; preds = %65, %17, %10
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@.str = [5 x i8] c"fork\00"
@PT_CONTINUE = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@errno = global i64 0
@ECHILD = global i64 0
define i32 @_ptrace(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @_error(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1)
%8 = sub nsw i32 0, %7
br label %45
9: ; preds = %1
%10 = icmp eq i64 %4, 0
br i1 %10, label %11, label %43
11: ; preds = %9
%12 = tail call i32 @_ptrace_child(i8* %0)
br label %45
13: ; preds = %36, %43
%14 = call i64 @waitpid(i64 %4, i32* %2, i32 0)
%15 = icmp eq i64 %14, %4
br i1 %15, label %16, label %34
16: ; preds = %13
%17 = load i32, i32* %2
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp eq i64 %18, 0
%20 = load i32, i32* %2
br i1 %19, label %23, label %21
21: ; preds = %16
%22 = call i32 @WEXITSTATUS(i32 %20)
br label %45
23: ; preds = %16
%24 = call i64 @WIFSIGNALED(i32 %20)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %45
26: ; preds = %23
%27 = load i32, i32* %2
%28 = call i64 @WIFSTOPPED(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %26
%31 = load i32, i32* @PT_CONTINUE
%32 = call i64 @ptrace(i32 %31, i64 %4, i8* inttoptr (i64 1 to i8*), i32 0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %45, label %36
34: ; preds = %13
%35 = icmp eq i64 %14, -1
br i1 %35, label %37, label %36
36: ; preds = %34, %26, %30
br label %13
37: ; preds = %34
%38 = call i32 @_error(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 1)
%39 = sub nsw i32 0, %38
%40 = load i64, i64* @errno
%41 = load i64, i64* @ECHILD
%42 = icmp eq i64 %40, %41
br i1 %42, label %45, label %43
43: ; preds = %9, %37
%44 = phi i32 [ %39, %37 ], [ 0, %9 ]
br label %13
45: ; preds = %30, %23, %37, %21, %11, %6
%46 = phi i32 [ %8, %6 ], [ %12, %11 ], [ %22, %21 ], [ %44, %30 ], [ 1, %23 ], [ %39, %37 ]
ret i32 %46
}