#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 {
  unsigned long long rip;
  unsigned long long rax;
  int /*<<< orphan*/ rsi;
  scalar_t__ rsp;
  scalar_t__ rdi;
};
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ invalid;

/* Variables and functions */
int /*<<< orphan*/ RTLD_NOW;
int /*<<< orphan*/ memcpy(struct user_regs_struct *, struct user_regs_struct *,
                          int);
int /*<<< orphan*/ memset(struct user_regs_struct *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ ptrace_cont(int /*<<< orphan*/);
int ptrace_getregs(int /*<<< orphan*/, struct user_regs_struct *);
int ptrace_poketext(int /*<<< orphan*/, scalar_t__, char *, int);
int /*<<< orphan*/ ptrace_setregs(int /*<<< orphan*/,
                                  struct user_regs_struct *);
int strlen(char *);
int ptrace_call(pid_t pid, unsigned long long int func_addr, unsigned long arg1,
                unsigned long arg2, unsigned long *func_ret) {
  int ret = 0;
  struct user_regs_struct regs;
  struct user_regs_struct saved_regs;
  char filename[64] = "./libnew.so";
  unsigned long invalid = 0x00;
  unsigned long long int dlopen_ret = 0;
  memset(&saved_regs, 0, sizeof(struct user_regs_struct));
  ret = ptrace_getregs(pid, &saved_regs);
  if (ret != 0) {
    printf("ptrace_getregs error, ret is %d\n", ret);
    return -1;
  }
  memcpy(&regs, &saved_regs, sizeof(struct user_regs_struct));
  regs.rsp -= sizeof(invalid);
  ret = ptrace_poketext(pid, regs.rsp, (void *)&invalid, sizeof(invalid));
  if (ret != 0) {
    return -1;
  }
  ptrace_poketext(pid, regs.rsp + 512, filename, strlen(filename) + 1);
  regs.rip = func_addr;
  regs.rdi = regs.rsp + 512;
  regs.rsi = RTLD_NOW;
  ptrace_setregs(pid, &regs);
  ptrace_cont(pid);
  ptrace_getregs(pid, &regs);
  dlopen_ret = regs.rax;
  ptrace_setregs(pid, &saved_regs);
  return 0;
}


x86 assembly (clang, -O0):
<ptrace_call>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0xc0(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x40,%edx
call   1080 <memcpy@plt>
movq   $0x0,-0xc8(%rbp)
movq   $0x0,-0xd0(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
call   1090 <ptrace_getregs@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1217 <ptrace_call+0x97>
mov    -0x2c(%rbp),%esi
lea    0xe3a(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <ptrace_call+0x198>
lea    -0x58(%rbp),%rdi
lea    -0x80(%rbp),%rsi
mov    $0x28,%edx
call   1080 <memcpy@plt>
mov    -0x40(%rbp),%rax
sub    $0x8,%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x40(%rbp),%rsi
lea    -0xc8(%rbp),%rdx
mov    $0x8,%ecx
call   1070 <ptrace_poketext@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1266 <ptrace_call+0xe6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <ptrace_call+0x198>
mov    -0x8(%rbp),%eax
mov    %eax,-0xe4(%rbp)
mov    -0x40(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0xe0(%rbp)
lea    -0xc0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
lea    -0xc0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rsi
mov    -0xd8(%rbp),%rdx
mov    %eax,%ecx
add    $0x1,%ecx
call   1070 <ptrace_poketext@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x40(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x38(%rbp)
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x58(%rbp),%rsi
call   10a0 <ptrace_setregs@plt>
mov    -0x8(%rbp),%edi
call   1030 <ptrace_cont@plt>
mov    -0x8(%rbp),%edi
lea    -0x58(%rbp),%rsi
call   1090 <ptrace_getregs@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
call   10a0 <ptrace_setregs@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptrace_call>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xa0,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movaps 0xe95(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xe79(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xe5d(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xe41(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movq   $0x0,(%rsp)
lea    0x78(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <ptrace_getregs@plt>
test   %eax,%eax
je     1206 <ptrace_call+0x86>
lea    0xe4b(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0xffffffff,%eax
jmp    12b6 <ptrace_call+0x136>
lea    0x8(%rsp),%rdi
lea    0x78(%rsp),%rsi
mov    $0x28,%edx
call   1080 <memcpy@plt>
mov    0x20(%rsp),%rsi
add    $0xfffffffffffffff8,%rsi
mov    %rsi,0x20(%rsp)
mov    %rsp,%rdx
mov    %ebp,%edi
mov    $0x8,%ecx
call   1070 <ptrace_poketext@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    12b6 <ptrace_call+0x136>
mov    $0x200,%ebx
mov    0x20(%rsp),%r12
add    %rbx,%r12
lea    0x30(%rsp),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%ecx
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1070 <ptrace_poketext@plt>
mov    %r14,0x8(%rsp)
add    0x20(%rsp),%rbx
mov    %rbx,0x28(%rsp)
mov    0x2d66(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <ptrace_setregs@plt>
mov    %ebp,%edi
call   1030 <ptrace_cont@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <ptrace_getregs@plt>
lea    0x78(%rsp),%rsi
mov    %ebp,%edi
call   10a0 <ptrace_setregs@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.user_regs_struct = type { i64, i64, i32, i64, i64 }
@__const.ptrace_call.filename = [64 x i8] c"./libnew.so\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [33 x i8] c"ptrace_getregs error, ret is %d\0A\00"
@RTLD_NOW = global i32 0
define i32 @ptrace_call(i32 %0, i64 %1, i64 %2, i64 %3, i64* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i32
%13 = alloca %struct.user_regs_struct
%14 = alloca %struct.user_regs_struct
%15 = alloca [64 x i8]
%16 = alloca i64
%17 = alloca i64
store i32 %0, i32* %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64* %4, i64** %11
store i32 0, i32* %12
%18 = bitcast [64 x i8]* %15 to i8*
store i64 0, i64* %16
store i64 0, i64* %17
%19 = call i32 @memset(%struct.user_regs_struct* %14, i32 0, i32 40)
%20 = load i32, i32* %7
%21 = call i32 @ptrace_getregs(i32 %20, %struct.user_regs_struct* %14)
store i32 %21, i32* %12
%22 = load i32, i32* %12
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %5
%25 = load i32, i32* %12
%26 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 -1, i32* %6
br label %68
27: ; preds = %5
%28 = call i32 @memcpy(%struct.user_regs_struct* %13, %struct.user_regs_struct* %14, i32 40)
%29 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 3
%30 = load i64, i64* %29
%31 = sub i64 %30, 8
store i64 %31, i64* %29
%32 = load i32, i32* %7
%33 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 3
%34 = load i64, i64* %33
%35 = bitcast i64* %16 to i8*
%36 = call i32 @ptrace_poketext(i32 %32, i64 %34, i8* %35, i32 8)
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %27
store i32 -1, i32* %6
br label %68
40: ; preds = %27
%41 = load i32, i32* %7
%42 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 3
%43 = load i64, i64* %42
%44 = add nsw i64 %43, 512
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %15, i64 0, i64 0
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %15, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 %47, 1
%49 = call i32 @ptrace_poketext(i32 %41, i64 %44, i8* %45, i32 %48)
%50 = load i64, i64* %8
%51 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 0
store i64 %50, i64* %51
%52 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 3
%53 = load i64, i64* %52
%54 = add nsw i64 %53, 512
%55 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 4
store i64 %54, i64* %55
%56 = load i32, i32* @RTLD_NOW
%57 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 2
store i32 %56, i32* %57
%58 = load i32, i32* %7
%59 = call i32 @ptrace_setregs(i32 %58, %struct.user_regs_struct* %13)
%60 = load i32, i32* %7
%61 = call i32 @ptrace_cont(i32 %60)
%62 = load i32, i32* %7
%63 = call i32 @ptrace_getregs(i32 %62, %struct.user_regs_struct* %13)
%64 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %13, i32 0, i32 1
%65 = load i64, i64* %64
store i64 %65, i64* %17
%66 = load i32, i32* %7
%67 = call i32 @ptrace_setregs(i32 %66, %struct.user_regs_struct* %14)
store i32 0, i32* %6
br label %68
68: ; preds = %40, %39, %24
%69 = load i32, i32* %6
ret i32 %69
}

LLVM IR (-O2):
%struct.user_regs_struct = type { i64, i64, i32, i64, i64 }
@__const.ptrace_call.filename = [64 x i8] c"./libnew.so\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [33 x i8] c"ptrace_getregs error, ret is %d\0A\00"
@RTLD_NOW = global i32 0
define i32 @ptrace_call(i32 %0, i64 %1, i64 %2, i64 %3, i64* nocapture readnone %4) {
%6 = alloca %struct.user_regs_struct
%7 = alloca %struct.user_regs_struct
%8 = alloca [64 x i8]
%9 = alloca i64
%10 = bitcast %struct.user_regs_struct* %6 to i8*
%11 = bitcast %struct.user_regs_struct* %7 to i8*
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%13 = bitcast i64* %9 to i8*
store i64 0, i64* %9
%14 = call i32 @memset(%struct.user_regs_struct* %7, i32 0, i32 40)
%15 = call i32 @ptrace_getregs(i32 %0, %struct.user_regs_struct* %7)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %5
%18 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %42
19: ; preds = %5
%20 = call i32 @memcpy(%struct.user_regs_struct* %6, %struct.user_regs_struct* %7, i32 40)
%21 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i64 0, i32 3
%22 = load i64, i64* %21
%23 = add i64 %22, -8
store i64 %23, i64* %21
%24 = call i32 @ptrace_poketext(i32 %0, i64 %23, i8* %13, i32 8)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %42
26: ; preds = %19
%27 = load i64, i64* %21
%28 = add nsw i64 %27, 512
%29 = call i32 @strlen(i8* %12)
%30 = add nsw i32 %29, 1
%31 = call i32 @ptrace_poketext(i32 %0, i64 %28, i8* %12, i32 %30)
%32 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i64 0, i32 0
store i64 %1, i64* %32
%33 = load i64, i64* %21
%34 = add nsw i64 %33, 512
%35 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i64 0, i32 4
store i64 %34, i64* %35
%36 = load i32, i32* @RTLD_NOW
%37 = getelementptr inbounds %struct.user_regs_struct, %struct.user_regs_struct* %6, i64 0, i32 2
store i32 %36, i32* %37
%38 = call i32 @ptrace_setregs(i32 %0, %struct.user_regs_struct* %6)
%39 = call i32 @ptrace_cont(i32 %0)
%40 = call i32 @ptrace_getregs(i32 %0, %struct.user_regs_struct* %6)
%41 = call i32 @ptrace_setregs(i32 %0, %struct.user_regs_struct* %7)
br label %42
42: ; preds = %19, %26, %17
%43 = phi i32 [ -1, %17 ], [ 0, %26 ], [ -1, %19 ]
ret i32 %43
}