#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 {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int atoi(int /*<<< orphan*/);
scalar_t__ dlsym(int /*<<< orphan*/ *, char *);
unsigned long long find_lib_base(int, char *);
int getopt(int, char **, char *);
int getpid();
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, int);
int ptrace_attach(int);
int ptrace_call(int, unsigned long long, int /*<<< orphan*/, int /*<<< orphan*/,
                int /*<<< orphan*/);
int /*<<< orphan*/ ptrace_detach(int);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  int c = 0;
  int ret = 0;
  pid_t t_pid = 0;
  unsigned long long int loader_libc;
  unsigned long long int loader_dlopen;
  unsigned long long int T_libc;
  unsigned long long int T_dlopen;
  struct user_regs_struct saved_regs;
  while ((c = getopt(argc, argv, "p:h")) != -1) {
    switch (c) {
    case 'p':
      t_pid = atoi(optarg);
      break;
    case 'h':
    default:
      usage();
      return 0;
    }
  }
  printf("target pid: %d\n", t_pid);
  loader_libc = find_lib_base(getpid(), "libc-");
  loader_dlopen = (unsigned long)dlsym(NULL, "__libc_dlopen_mode");
  T_libc = find_lib_base(t_pid, "libc-");
  T_dlopen = T_libc + (loader_dlopen - loader_libc);
  printf("loader_libc is %lx\n", loader_libc);
  printf("T_libc is %lx\n", T_libc);
  printf("loader_dlopen is %lx\n", loader_dlopen);
  printf("T_dlopen is %lx\n", T_dlopen);
  ret = ptrace_attach(t_pid);
  if (ret != 0) {
    printf("ptrace_attach error, ret is %d\n", ret);
    return -1;
  }
  ret = ptrace_call(t_pid, T_dlopen, 0, 0, 0);
  if (ret != 0) {
    printf("ptrace_call dlopen error, ret is %d\n", ret);
    return -1;
  }
  ptrace_detach(t_pid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe27(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1242 <main+0xa2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
sub    $0x68,%eax
je     1225 <main+0x85>
jmp    11fe <main+0x5e>
mov    -0x4c(%rbp),%eax
sub    $0x70,%eax
jne    122a <main+0x8a>
jmp    120f <main+0x6f>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    123d <main+0x9d>
jmp    122a <main+0x8a>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0x0,-0x4(%rbp)
jmp    1368 <main+0x1c8>
jmp    11cb <main+0x2b>
mov    -0x1c(%rbp),%esi
lea    0xdb8(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%edi
lea    0xdb3(%rip),%rsi
call   1070 <find_lib_base@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xda5(%rip),%rsi
call   10b0 <dlsym@plt>
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xd8c(%rip),%rsi
call   1070 <find_lib_base@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    -0x28(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%esi
lea    0xd7c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %eax,%esi
lea    0xd7e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rax
mov    %eax,%esi
lea    0xd7b(%rip),%rdi
call   1060 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %eax,%esi
lea    0xd7f(%rip),%rdi
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1030 <ptrace_attach@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     131c <main+0x17c>
mov    -0x18(%rbp),%esi
lea    0xd6c(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <main+0x1c8>
mov    -0x1c(%rbp),%edi
mov    -0x40(%rbp),%rsi
xor    %r8d,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1040 <ptrace_call@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1359 <main+0x1b9>
mov    -0x18(%rbp),%esi
lea    0xd4f(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1368 <main+0x1c8>
mov    -0x1c(%rbp),%edi
call   1080 <ptrace_detach@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
mov    %edi,%ebx
xor    %r15d,%r15d
lea    0xe49(%rip),%r14
mov    0x2e3a(%rip),%r12
xchg   %ax,%ax
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
call   10a0 <getopt@plt>
cmp    $0x70,%eax
jne    11e0 <main+0x40>
mov    (%r12),%edi
call   10c0 <atoi@plt>
mov    %eax,%r15d
jmp    11c0 <main+0x20>
cmp    $0xffffffff,%eax
jne    128a <main+0xea>
lea    0xe14(%rip),%rdi
mov    %r15d,%esi
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe0e(%rip),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <find_lib_base@plt>
mov    %rax,%rbp
lea    0xe00(%rip),%rsi
xor    %edi,%edi
call   10b0 <dlsym@plt>
mov    %rax,%r12
mov    %r15d,%edi
mov    %r14,%rsi
call   1070 <find_lib_base@plt>
mov    %rax,%rbx
mov    %r12,%r14
sub    %rbp,%r14
add    %rax,%r14
lea    0xdeb(%rip),%rdi
mov    %ebp,%esi
call   1060 <printf@plt>
lea    0xdf1(%rip),%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
lea    0xdf2(%rip),%rdi
mov    %r12d,%esi
call   1060 <printf@plt>
lea    0xdf9(%rip),%rdi
mov    %r14d,%esi
call   1060 <printf@plt>
mov    %r15d,%edi
call   1030 <ptrace_attach@plt>
test   %eax,%eax
je     1295 <main+0xf5>
lea    0xdef(%rip),%rdi
jmp    12b4 <main+0x114>
xor    %ebp,%ebp
xor    %eax,%eax
call   1090 <usage@plt>
jmp    12c0 <main+0x120>
xor    %ebp,%ebp
mov    %r15d,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <ptrace_call@plt>
test   %eax,%eax
je     12cb <main+0x12b>
lea    0xde3(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
call   1080 <ptrace_detach@plt>
jmp    12c0 <main+0x120>

LLVM IR (-O0):
%struct.user_regs_struct = type { i32 }
@.str = [4 x i8] c"p:h\00"
@optarg = global i32 0
@.str.1 = [16 x i8] c"target pid: %d\0A\00"
@.str.2 = [6 x i8] c"libc-\00"
@.str.3 = [19 x i8] c"__libc_dlopen_mode\00"
@.str.4 = [20 x i8] c"loader_libc is %lx\0A\00"
@.str.5 = [15 x i8] c"T_libc is %lx\0A\00"
@.str.6 = [22 x i8] c"loader_dlopen is %lx\0A\00"
@.str.7 = [17 x i8] c"T_dlopen is %lx\0A\00"
@.str.8 = [32 x i8] c"ptrace_attach error, ret is %d\0A\00"
@.str.9 = [37 x i8] c"ptrace_call dlopen error, ret is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca %struct.user_regs_struct
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %14
14: ; preds = %27, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call i32 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %17, i32* %6
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i32, i32* %6
switch i32 %20, label %25 [
i32 112, label %21
i32 104, label %24
]
21: ; preds = %19
%22 = load i32, i32* @optarg
%23 = call i32 @atoi(i32 %22)
store i32 %23, i32* %8
br label %27
24: ; preds = %19
br label %25
25: ; preds = %19, %24
%26 = call i32 (...) @usage()
store i32 0, i32* %3
br label %72
27: ; preds = %21
br label %14
28: ; preds = %14
%29 = load i32, i32* %8
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 (...) @getpid()
%32 = call i64 @find_lib_base(i32 %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i64 %32, i64* %9
%33 = call i64 @dlsym(i32* null, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i64 %33, i64* %10
%34 = load i32, i32* %8
%35 = call i64 @find_lib_base(i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i64 %35, i64* %11
%36 = load i64, i64* %11
%37 = load i64, i64* %10
%38 = load i64, i64* %9
%39 = sub i64 %37, %38
%40 = add i64 %36, %39
store i64 %40, i64* %12
%41 = load i64, i64* %9
%42 = trunc i64 %41 to i32
%43 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = load i64, i64* %11
%45 = trunc i64 %44 to i32
%46 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %45)
%47 = load i64, i64* %10
%48 = trunc i64 %47 to i32
%49 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = load i64, i64* %12
%51 = trunc i64 %50 to i32
%52 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %8
%54 = call i32 @ptrace_attach(i32 %53)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %28
%58 = load i32, i32* %7
%59 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %58)
store i32 -1, i32* %3
br label %72
60: ; preds = %28
%61 = load i32, i32* %8
%62 = load i64, i64* %12
%63 = call i32 @ptrace_call(i32 %61, i64 %62, i32 0, i32 0, i32 0)
store i32 %63, i32* %7
%64 = load i32, i32* %7
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %60
%67 = load i32, i32* %7
%68 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0), i32 %67)
store i32 -1, i32* %3
br label %72
69: ; preds = %60
%70 = load i32, i32* %8
%71 = call i32 @ptrace_detach(i32 %70)
store i32 0, i32* %3
br label %72
72: ; preds = %69, %66, %57, %25
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [4 x i8] c"p:h\00"
@optarg = global i32 0
@.str.1 = [16 x i8] c"target pid: %d\0A\00"
@.str.2 = [6 x i8] c"libc-\00"
@.str.3 = [19 x i8] c"__libc_dlopen_mode\00"
@.str.4 = [20 x i8] c"loader_libc is %lx\0A\00"
@.str.5 = [15 x i8] c"T_libc is %lx\0A\00"
@.str.6 = [22 x i8] c"loader_dlopen is %lx\0A\00"
@.str.7 = [17 x i8] c"T_dlopen is %lx\0A\00"
@.str.8 = [32 x i8] c"ptrace_attach error, ret is %d\0A\00"
@.str.9 = [37 x i8] c"ptrace_call dlopen error, ret is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %6, %2
%4 = phi i32 [ 0, %2 ], [ %8, %6 ]
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
switch i32 %5, label %9 [
i32 -1, label %11
i32 112, label %6
]
6: ; preds = %3
%7 = load i32, i32* @optarg
%8 = tail call i32 @atoi(i32 %7)
br label %3
9: ; preds = %3
%10 = tail call i32 (...) @usage()
br label %38
11: ; preds = %3
%12 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%13 = tail call i32 (...) @getpid()
%14 = tail call i64 @find_lib_base(i32 %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i64 @dlsym(i32* null, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i64 @find_lib_base(i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = sub i64 %15, %14
%18 = add i64 %16, %17
%19 = trunc i64 %14 to i32
%20 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%21 = trunc i64 %16 to i32
%22 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %21)
%23 = trunc i64 %15 to i32
%24 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = trunc i64 %18 to i32
%26 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %25)
%27 = tail call i32 @ptrace_attach(i32 %4)
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %11
%30 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %27)
br label %38
31: ; preds = %11
%32 = tail call i32 @ptrace_call(i32 %4, i64 %18, i32 0, i32 0, i32 0)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.9, i64 0, i64 0), i32 %32)
br label %38
36: ; preds = %31
%37 = tail call i32 @ptrace_detach(i32 %4)
br label %38
38: ; preds = %36, %34, %29, %9
%39 = phi i32 [ 0, %9 ], [ -1, %29 ], [ -1, %34 ], [ 0, %36 ]
ret i32 %39
}