#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*/ ulong;
struct pt_regs {
  int dummy;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int ptrace_call(int /*<<< orphan*/, int /*<<< orphan*/, long *, int,
                struct pt_regs *);
int ptrace_getregs(int /*<<< orphan*/, struct pt_regs *);
int ptrace_ip(struct pt_regs *);
int ptrace_retval(struct pt_regs *);
int ptrace_call_wrapper(pid_t target_pid, const char *func_name,
                        void *func_addr, long *parameters, int param_num,
                        struct pt_regs *regs) {
  {
    printf("[D] ");
    printf("[+] Calling %s in target process.\n", func_name);
  };
  if (ptrace_call(target_pid, (ulong)func_addr, parameters, param_num, regs) ==
      -1)
    return -1;
  if (ptrace_getregs(target_pid, regs) == -1)
    return -1;
  {
    printf("[D] ");
    printf("[+] Target process returned from %s, return value=%x, pc=%x \n",
           func_name, ptrace_retval(regs), ptrace_ip(regs));
  };
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_call_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
call   1030 <ptrace_call@plt>
cmp    $0xffffffff,%eax
jne    11bd <ptrace_call_wrapper+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <ptrace_call_wrapper+0xd7>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1060 <ptrace_getregs@plt>
cmp    $0xffffffff,%eax
jne    11de <ptrace_call_wrapper+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <ptrace_call_wrapper+0xd7>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <ptrace_retval@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <ptrace_ip@plt>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_call_wrapper>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %r8d,%r15d
mov    %rcx,%r13
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe8d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe84(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r13,%rdx
mov    %r15d,%ecx
mov    %r12,%r8
call   1030 <ptrace_call@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11ef <ptrace_call_wrapper+0x9f>
mov    %ebp,%edi
mov    %r12,%rsi
call   1060 <ptrace_getregs@plt>
cmp    $0xffffffff,%eax
je     11ef <ptrace_call_wrapper+0x9f>
lea    0xe42(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
call   1070 <ptrace_retval@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1050 <ptrace_ip@plt>
lea    0xe47(%rip),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pt_regs = type { i32 }
@.str = [5 x i8] c"[D] \00"
@.str.1 = [35 x i8] c"[+] Calling %s in target process.\0A\00"
@.str.2 = [62 x i8] c"[+] Target process returned from %s, return value=%x, pc=%x \0A\00"
define i32 @ptrace_call_wrapper(i32 %0, i8* %1, i8* %2, i64* %3, i32 %4, %struct.pt_regs* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64*
%12 = alloca i32
%13 = alloca %struct.pt_regs*
store i32 %0, i32* %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i64* %3, i64** %11
store i32 %4, i32* %12
store %struct.pt_regs* %5, %struct.pt_regs** %13
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%15 = load i8*, i8** %9
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* %8
%18 = load i8*, i8** %10
%19 = ptrtoint i8* %18 to i32
%20 = load i64*, i64** %11
%21 = load i32, i32* %12
%22 = load %struct.pt_regs*, %struct.pt_regs** %13
%23 = call i32 @ptrace_call(i32 %17, i32 %19, i64* %20, i32 %21, %struct.pt_regs* %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %6
store i32 -1, i32* %7
br label %40
26: ; preds = %6
%27 = load i32, i32* %8
%28 = load %struct.pt_regs*, %struct.pt_regs** %13
%29 = call i32 @ptrace_getregs(i32 %27, %struct.pt_regs* %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 -1, i32* %7
br label %40
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%34 = load i8*, i8** %9
%35 = load %struct.pt_regs*, %struct.pt_regs** %13
%36 = call i32 @ptrace_retval(%struct.pt_regs* %35)
%37 = load %struct.pt_regs*, %struct.pt_regs** %13
%38 = call i32 @ptrace_ip(%struct.pt_regs* %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), i8* %34, i32 %36, i32 %38)
store i32 0, i32* %7
br label %40
40: ; preds = %32, %31, %25
%41 = load i32, i32* %7
ret i32 %41
}

LLVM IR (-O2):
%struct.pt_regs = type { i32 }
@.str = [5 x i8] c"[D] \00"
@.str.1 = [35 x i8] c"[+] Calling %s in target process.\0A\00"
@.str.2 = [62 x i8] c"[+] Target process returned from %s, return value=%x, pc=%x \0A\00"
define i32 @ptrace_call_wrapper(i32 %0, i8* %1, i8* %2, i64* %3, i32 %4, %struct.pt_regs* %5) {
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%9 = ptrtoint i8* %2 to i64
%10 = trunc i64 %9 to i32
%11 = tail call i32 @ptrace_call(i32 %0, i32 %10, i64* %3, i32 %4, %struct.pt_regs* %5)
%12 = icmp eq i32 %11, -1
br i1 %12, label %21, label %13
13: ; preds = %6
%14 = tail call i32 @ptrace_getregs(i32 %0, %struct.pt_regs* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @ptrace_retval(%struct.pt_regs* %5)
%19 = tail call i32 @ptrace_ip(%struct.pt_regs* %5)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %18, i32 %19)
br label %21
21: ; preds = %13, %6, %16
%22 = phi i32 [ 0, %16 ], [ -1, %6 ], [ -1, %13 ]
ret i32 %22
}