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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ perror(char *);
int ptrace(int /*<<< orphan*/, int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int start_trace(pid_t child) {
  int ret;
  ret = ptrace(PTRACE_ATTACH, child, NULL, NULL);
  if (ret) {
    perror("ptrace(PTRACE_ATTACH) failed");
    return 1;
  }
  ret = waitpid(child, NULL, 0);
  if (ret != child) {
    perror("waitpid() failed");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<start_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1030 <ptrace@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1178 <start_trace+0x48>
lea    0xe99(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b4 <start_trace+0x84>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     11ad <start_trace+0x7d>
lea    0xe81(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b4 <start_trace+0x84>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_trace>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2eac(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    %ebp,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <ptrace@plt>
test   %eax,%eax
jne    1167 <start_trace+0x37>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    %ebp,%eax
jne    1170 <start_trace+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe92(%rip),%rdi
jmp    1177 <start_trace+0x47>
lea    0xea6(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%ebx
jmp    115e <start_trace+0x2e>

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

LLVM IR (-O2):
@PTRACE_ATTACH = global i32 0
@.str = [29 x i8] c"ptrace(PTRACE_ATTACH) failed\00"
@.str.1 = [17 x i8] c"waitpid() failed\00"
define i32 @start_trace(i32 %0) {
%2 = load i32, i32* @PTRACE_ATTACH
%3 = tail call i32 @ptrace(i32 %2, i32 %0, i32* null, i32* null)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @waitpid(i32 %0, i32* null, i32 0)
%7 = icmp eq i32 %6, %0
br i1 %7, label %11, label %8
8: ; preds = %5, %1
%9 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %5 ]
%10 = tail call i32 @perror(i8* %9)
br label %11
11: ; preds = %8, %5
%12 = phi i32 [ 0, %5 ], [ 1, %8 ]
ret i32 %12
}