#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 */
struct user_regs_struct {
  long rdi;
  long rsi;
  long rdx;
  long rcx;
  long r8;
  long r9;
  long rip;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ printf(char *, ...);
int ptrace_continue(int /*<<< orphan*/);
scalar_t__ ptrace_setregs(int /*<<< orphan*/, struct user_regs_struct *);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int ptrace_call(pid_t pid, long addr, long *args, int nargs,
                struct user_regs_struct regs) {
  int i;
  long *urgs[6] = {&regs.rdi, &regs.rsi, &regs.rdx,
                   &regs.rcx, &regs.r8,  &regs.r9};
  for (i = 0; i < nargs && i < 6; i++) {
    *urgs[i] = args[i];
  }
  regs.rip = addr;
  if (ptrace_setregs(pid, &regs) != 0 || ptrace_continue(pid) != 0) {
    {
      printf("[E] ");
      printf("error\n");
    };
    return -1;
  }
  int stat = 0;
  waitpid(pid, &stat, WUNTRACED);
  int max = 10;
  while (stat != 0xb7f && max-- != 0) {
    {
      printf("[I] ");
      printf("stat: %p\n", stat);
    };
    if (ptrace_continue(pid) == -1) {
      {
        printf("[E] ");
        printf("error waitpid\n");
      };
      return -1;
    }
    waitpid(pid, &stat, WUNTRACED);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_call>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %rax,-0x50(%rbp)
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rcx
add    $0x10,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0x18,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0x20,%rcx
mov    %rcx,-0x30(%rbp)
add    $0x28,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
cmp    -0x1c(%rbp),%ecx
mov    %al,-0x61(%rbp)
jge    11b8 <ptrace_call+0x78>
cmpl   $0x6,-0x20(%rbp)
setl   %al
mov    %al,-0x61(%rbp)
mov    -0x61(%rbp),%al
test   $0x1,%al
jne    11c8 <ptrace_call+0x88>
jmp    11ee <ptrace_call+0xae>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
movslq -0x20(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119d <ptrace_call+0x5d>
mov    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    %rax,0x30(%rsi)
mov    -0x8(%rbp),%edi
call   1060 <ptrace_setregs@plt>
cmp    $0x0,%rax
jne    121d <ptrace_call+0xdd>
mov    -0x8(%rbp),%edi
call   1050 <ptrace_continue@plt>
cmp    $0x0,%eax
je     1245 <ptrace_call+0x105>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <ptrace_call+0x1d7>
movl   $0x0,-0x54(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rsi
call   1040 <waitpid@plt>
movl   $0xa,-0x58(%rbp)
xor    %eax,%eax
cmpl   $0xb7f,-0x54(%rbp)
mov    %al,-0x62(%rbp)
je     128e <ptrace_call+0x14e>
mov    -0x58(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x58(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x62(%rbp)
mov    -0x62(%rbp),%al
test   $0x1,%al
jne    129e <ptrace_call+0x15e>
jmp    1310 <ptrace_call+0x1d0>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x54(%rbp),%esi
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <ptrace_continue@plt>
cmp    $0xffffffff,%eax
jne    12f6 <ptrace_call+0x1b6>
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <ptrace_call+0x1d7>
mov    -0x8(%rbp),%edi
mov    0x2ce0(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rsi
call   1040 <waitpid@plt>
jmp    1268 <ptrace_call+0x128>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_call>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rax
mov    %edi,%ebx
lea    0x80(%rsp),%rsi
mov    %rsi,0x10(%rsp)
lea    0x88(%rsp),%rdi
mov    %rdi,0x18(%rsp)
lea    0x90(%rsp),%rdi
mov    %rdi,0x20(%rsp)
lea    0x98(%rsp),%rdi
mov    %rdi,0x28(%rsp)
lea    0xa0(%rsp),%rdi
mov    %rdi,0x30(%rsp)
lea    0xa8(%rsp),%rdi
mov    %rdi,0x38(%rsp)
test   %ecx,%ecx
jle    1220 <ptrace_call+0xd0>
add    $0xffffffff,%ecx
cmp    $0x5,%ecx
mov    $0x5,%edi
cmovb  %ecx,%edi
mov    (%rdx),%rcx
mov    %rcx,(%rsi)
test   %edi,%edi
je     1220 <ptrace_call+0xd0>
add    $0x1,%edi
mov    0x18(%rsp),%rcx
mov    0x8(%rdx),%rbp
mov    %rbp,(%rcx)
cmp    $0x2,%edi
je     1220 <ptrace_call+0xd0>
mov    0x20(%rsp),%rcx
mov    0x10(%rdx),%rbp
mov    %rbp,(%rcx)
cmp    $0x3,%edi
je     1220 <ptrace_call+0xd0>
mov    0x28(%rsp),%rcx
mov    0x18(%rdx),%rbp
mov    %rbp,(%rcx)
cmp    $0x4,%edi
je     1220 <ptrace_call+0xd0>
mov    0x30(%rsp),%rcx
mov    0x20(%rdx),%rbp
mov    %rbp,(%rcx)
cmp    $0x5,%edi
je     1220 <ptrace_call+0xd0>
mov    0x38(%rsp),%rcx
mov    0x28(%rdx),%rdx
mov    %rdx,(%rcx)
mov    %rax,0x30(%rsi)
mov    %ebx,%edi
call   1070 <ptrace_setregs@plt>
test   %rax,%rax
jne    123b <ptrace_call+0xeb>
mov    %ebx,%edi
call   1060 <ptrace_continue@plt>
test   %eax,%eax
je     126b <ptrace_call+0x11b>
lea    0xdbe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdd2(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movl   $0x0,0xc(%rsp)
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1050 <waitpid@plt>
xor    %ebp,%ebp
cmpl   $0xb7f,0xc(%rsp)
je     125a <ptrace_call+0x10a>
mov    $0xfffffff5,%r13d
lea    0xd64(%rip),%r14
xor    %ebp,%ebp
lea    0xd60(%rip),%r15
lea    0xc(%rsp),%r12
nop
inc    %r13d
je     125a <ptrace_call+0x10a>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <ptrace_continue@plt>
cmp    $0xffffffff,%eax
je     12fb <ptrace_call+0x1ab>
mov    0x2d00(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r12,%rsi
call   1050 <waitpid@plt>
cmpl   $0xb7f,0xc(%rsp)
jne    12b0 <ptrace_call+0x160>
jmp    125a <ptrace_call+0x10a>
lea    0xcfe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xd04(%rip),%rdi
jmp    1250 <ptrace_call+0x100>

LLVM IR (-O0):
%struct.user_regs_struct = type { i64, i64, i64, i64, i64, i64, i64 }
@.str = [5 x i8] c"[E] \00"
@.str.1 = [7 x i8] c"error\0A\00"
@WUNTRACED = global i32 0
@.str.2 = [5 x i8] c"[I] \00"
@.str.3 = [10 x i8] c"stat: %p\0A\00"
@.str.4 = [15 x i8] c"error waitpid\0A\00"
define i32 @ptrace_call(i32 %0, i64 %1, i64* %2, i32 %3, %struct.user_regs_struct* byval(%struct.user_regs_struct) align 8 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [6 x i64*]
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %7
store i64 %1, i64* %8
store i64* %2, i64** %9
store i32 %3, i32* %10
%15 = getelementptr inbounds [6 x i64*], [6 x i64*]* %12, i64 0, i64 0
%16 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i32 0, i32 0
store i64* %16, i64** %15
%17 = getelementptr inbounds i64*, i64** %15, i64 1
%18 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i32 0, i32 1
store i64* %18, i64** %17
%19 = getelementptr inbounds i64*, i64** %17, i64 1
%20 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i32 0, i32 2
store i64* %20, i64** %19
%21 = getelementptr inbounds i64*, i64** %19, i64 1
%22 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i32 0, i32 3
store i64* %22, i64** %21
%23 = getelementptr inbounds i64*, i64** %21, i64 1
%24 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i32 0, i32 4
store i64* %24, i64** %23
%25 = getelementptr inbounds i64*, i64** %23, i64 1
%26 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i32 0, i32 5
store i64* %26, i64** %25
store i32 0, i32* %11
br label %27
27: ; preds = %46, %5
%28 = load i32, i32* %11
%29 = load i32, i32* %10
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* %11
%33 = icmp slt i32 %32, 6
br label %34
34: ; preds = %31, %27
%35 = phi i1 [ false, %27 ], [ %33, %31 ]
br i1 %35, label %36, label %49
36: ; preds = %34
%37 = load i64*, i64** %9
%38 = load i32, i32* %11
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i64, i64* %37, i64 %39
%41 = load i64, i64* %40
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [6 x i64*], [6 x i64*]* %12, i64 0, i64 %43
%45 = load i64*, i64** %44
store i64 %41, i64* %45
br label %46
46: ; preds = %36
%47 = load i32, i32* %11
%48 = add nsw i32 %47, 1
store i32 %48, i32* %11
br label %27
49: ; preds = %34
%50 = load i64, i64* %8
%51 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i32 0, i32 6
store i64 %50, i64* %51
%52 = load i32, i32* %7
%53 = call i64 @ptrace_setregs(i32 %52, %struct.user_regs_struct* %4)
%54 = icmp ne i64 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %49
%56 = load i32, i32* %7
%57 = call i32 @ptrace_continue(i32 %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55, %49
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %90
62: ; preds = %55
store i32 0, i32* %13
%63 = load i32, i32* %7
%64 = load i32, i32* @WUNTRACED
%65 = call i32 @waitpid(i32 %63, i32* %13, i32 %64)
store i32 10, i32* %14
br label %66
66: ; preds = %85, %62
%67 = load i32, i32* %13
%68 = icmp ne i32 %67, 2943
br i1 %68, label %69, label %73
69: ; preds = %66
%70 = load i32, i32* %14
%71 = add nsw i32 %70, -1
store i32 %71, i32* %14
%72 = icmp ne i32 %70, 0
br label %73
73: ; preds = %69, %66
%74 = phi i1 [ false, %66 ], [ %72, %69 ]
br i1 %74, label %75, label %89
75: ; preds = %73
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%77 = load i32, i32* %13
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %77)
%79 = load i32, i32* %7
%80 = call i32 @ptrace_continue(i32 %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %85
82: ; preds = %75
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %6
br label %90
85: ; preds = %75
%86 = load i32, i32* %7
%87 = load i32, i32* @WUNTRACED
%88 = call i32 @waitpid(i32 %86, i32* %13, i32 %87)
br label %66
89: ; preds = %73
store i32 0, i32* %6
br label %90
90: ; preds = %89, %82, %59
%91 = load i32, i32* %6
ret i32 %91
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i64, i64, i64, i64, i64, i64, i64 }
@.str = [5 x i8] c"[E] \00"
@WUNTRACED = global i32 0
@.str.2 = [5 x i8] c"[I] \00"
@.str.3 = [10 x i8] c"stat: %p\0A\00"
@str = [14 x i8] c"error waitpid\00"
@str.5 = [6 x i8] c"error\00"
define i32 @ptrace_call(i32 %0, i64 %1, i64* nocapture %2, i32 %3, %struct.user_regs_struct* byval(%struct.user_regs_struct) align 8 %4) {
%6 = alloca [6 x i64*]
%7 = alloca i32
%8 = bitcast [6 x i64*]* %6 to i8*
%9 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 0
%10 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 0
store i64* %10, i64** %9
%11 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 1
%12 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 1
store i64* %12, i64** %11
%13 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 2
%14 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 2
store i64* %14, i64** %13
%15 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 3
%16 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 3
store i64* %16, i64** %15
%17 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 4
%18 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 4
store i64* %18, i64** %17
%19 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 5
%20 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 5
store i64* %20, i64** %19
%21 = icmp sgt i32 %3, 0
br i1 %21, label %22, label %57
22: ; preds = %5
%23 = add nsw i32 %3, -1
%24 = call i32 @llvm.umin.i32(i32 %23, i32 5)
%25 = add nuw nsw i32 %24, 1
%26 = load i64, i64* %2
store i64 %26, i64* %10
%27 = icmp eq i32 %24, 0
br i1 %27, label %57, label %28
28: ; preds = %22
%29 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 1
%30 = load i64*, i64** %29
%31 = getelementptr inbounds i64, i64* %2, i64 1
%32 = load i64, i64* %31
store i64 %32, i64* %30
%33 = icmp eq i32 %25, 2
br i1 %33, label %57, label %34
34: ; preds = %28
%35 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 2
%36 = load i64*, i64** %35
%37 = getelementptr inbounds i64, i64* %2, i64 2
%38 = load i64, i64* %37
store i64 %38, i64* %36
%39 = icmp eq i32 %25, 3
br i1 %39, label %57, label %40
40: ; preds = %34
%41 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 3
%42 = load i64*, i64** %41
%43 = getelementptr inbounds i64, i64* %2, i64 3
%44 = load i64, i64* %43
store i64 %44, i64* %42
%45 = icmp eq i32 %25, 4
br i1 %45, label %57, label %46
46: ; preds = %40
%47 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 4
%48 = load i64*, i64** %47
%49 = getelementptr inbounds i64, i64* %2, i64 4
%50 = load i64, i64* %49
store i64 %50, i64* %48
%51 = icmp eq i32 %25, 5
br i1 %51, label %57, label %52
52: ; preds = %46
%53 = getelementptr inbounds [6 x i64*], [6 x i64*]* %6, i64 0, i64 5
%54 = load i64*, i64** %53
%55 = getelementptr inbounds i64, i64* %2, i64 5
%56 = load i64, i64* %55
store i64 %56, i64* %54
br label %57
57: ; preds = %28, %34, %40, %46, %52, %22, %5
%58 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %4, i64 0, i32 6
store i64 %1, i64* %58
%59 = call i64 @ptrace_setregs(i32 %0, %struct.user_regs_struct* %4)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = call i32 @ptrace_continue(i32 %0)
%63 = icmp eq i32 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %61, %57
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.5, i64 0, i64 0))
br label %93
67: ; preds = %61
%68 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%69 = load i32, i32* @WUNTRACED
%70 = call i32 @waitpid(i32 %0, i32* %7, i32 %69)
%71 = load i32, i32* %7
%72 = icmp eq i32 %71, 2943
br i1 %72, label %91, label %73
73: ; preds = %67, %86
%74 = phi i32 [ %75, %86 ], [ 10, %67 ]
%75 = add nsw i32 %74, -1
%76 = icmp eq i32 %74, 0
br i1 %76, label %91, label %77
77: ; preds = %73
%78 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%79 = load i32, i32* %7
%80 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %79)
%81 = call i32 @ptrace_continue(i32 %0)
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %86
83: ; preds = %77
%84 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%85 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %91
86: ; preds = %77
%87 = load i32, i32* @WUNTRACED
%88 = call i32 @waitpid(i32 %0, i32* %7, i32 %87)
%89 = load i32, i32* %7
%90 = icmp eq i32 %89, 2943
br i1 %90, label %91, label %73
91: ; preds = %86, %73, %67, %83
%92 = phi i32 [ -1, %83 ], [ 0, %67 ], [ 0, %73 ], [ 0, %86 ]
br label %93
93: ; preds = %91, %64
%94 = phi i32 [ -1, %64 ], [ %92, %91 ]
ret i32 %94
}