#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 option {
  char *member_0;
  int member_1;
  char member_3;
  int /*<<< orphan*/ *member_2;
};

/* Variables and functions */
int atoi(char *);
int getopt_long(int, char **, char const *, struct option const *,
                int /*<<< orphan*/ *);
int /*<<< orphan*/ inject_remote_process(int, char *, char *, char *,
                                         int /*<<< orphan*/);
char *optarg;
int /*<<< orphan*/ print_usage(char const *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strlen(char *);
char *strrchr(char *, char);
int main(int argc, char **argv) {
  int target_pid;
  char *libpath;
  const char *pname = strrchr(argv[0], '/') + 1;
  if (argc < 2)
    print_usage(pname, 1);
  int next_opt;
  const char *short_opts = "hp:l:";
  const struct option long_opts[] = {{"help", 0, NULL, 'h'},
                                     {"pid", 1, NULL, 'p'},
                                     {"libpath", 1, NULL, 'l'},
                                     {NULL, 0, NULL, 0}};
  do {
    next_opt = getopt_long(argc, argv, short_opts, long_opts, NULL);
    switch (next_opt) {
    case 'h':
      print_usage(pname, 0);
    case 'p':
      target_pid = atoi(optarg);
      break;
    case 'l':
      libpath = optarg;
      break;
    case '?':
      printf("\n");
      print_usage(pname, 1);
    case -1:
      break;
    default:;
    }
  } while (next_opt != -1);
  char *param = "";
  inject_remote_process(target_pid, libpath, "hook_entry", param,
                        strlen(param));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ca <main+0x4a>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1090 <print_usage@plt>
lea    0xe2f(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0x2bcd(%rip),%rsi
mov    $0x60,%edx
call   1070 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
lea    -0xa0(%rbp),%rcx
call   1040 <getopt_long@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0xac(%rbp)
sub    $0xffffffff,%eax
je     12c2 <main+0x142>
jmp    1223 <main+0xa3>
mov    -0xac(%rbp),%eax
sub    $0x3f,%eax
je     12a8 <main+0x128>
jmp    1237 <main+0xb7>
mov    -0xac(%rbp),%eax
sub    $0x68,%eax
je     1273 <main+0xf3>
jmp    124b <main+0xcb>
mov    -0xac(%rbp),%eax
sub    $0x6c,%eax
je     1295 <main+0x115>
jmp    125f <main+0xdf>
mov    -0xac(%rbp),%eax
sub    $0x70,%eax
je     127e <main+0xfe>
jmp    12c7 <main+0x147>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1090 <print_usage@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    12cc <main+0x14c>
mov    0x2d54(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    12cc <main+0x14c>
lea    0xd68(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1090 <print_usage@plt>
jmp    12cc <main+0x14c>
jmp    12cc <main+0x14c>
jmp    12d1 <main+0x151>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11ed <main+0x6d>
lea    0xd36(%rip),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    -0xa8(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0xa8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xc4(%rbp),%edi
mov    -0xc0(%rbp),%rsi
mov    -0xb8(%rbp),%rcx
mov    %eax,%r8d
lea    0xce4(%rip),%rdx
call   10a0 <inject_remote_process@plt>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rsi),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
add    $0x1,%rax
mov    %rax,(%rsp)
cmp    $0x1,%ebp
jg     11ab <main+0x3b>
mov    (%rsp),%rdi
mov    $0x1,%esi
call   1080 <print_usage@plt>
movaps 0x2c4e(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0x2c32(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0x2c16(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0x2bfa(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0x2bde(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0x2bc2(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xe06(%rip),%r12
lea    0x10(%rsp),%r13
jmp    1236 <main+0xc6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%rsp),%rdi
mov    $0x1,%esi
call   1080 <print_usage@plt>
nopl   0x0(%rax,%rax,1)
cmp    $0xffffffff,%r14d
je     12a1 <main+0x131>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %r8d,%r8d
call   1050 <getopt_long@plt>
mov    %eax,%r14d
cmp    $0x6b,%eax
jg     1270 <main+0x100>
cmp    $0x3f,%r14d
je     1210 <main+0xa0>
cmp    $0x68,%r14d
jne    1230 <main+0xc0>
mov    (%rsp),%rdi
xor    %esi,%esi
call   1080 <print_usage@plt>
jmp    127c <main+0x10c>
nopw   0x0(%rax,%rax,1)
cmp    $0x6c,%r14d
je     1290 <main+0x120>
cmp    $0x70,%r14d
jne    1230 <main+0xc0>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,%r15d
jmp    1230 <main+0xc0>
mov    0x2d59(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
jmp    1230 <main+0xc0>
lea    0xd5d(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0xd60(%rip),%rdx
mov    %r15d,%edi
mov    0x8(%rsp),%rsi
mov    %rbx,%rcx
mov    %eax,%r8d
call   1090 <inject_remote_process@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.option = type { i8*, i32, i8, i32* }
@.str = [6 x i8] c"hp:l:\00"
@.str.1 = [5 x i8] c"help\00"
@.str.2 = [4 x i8] c"pid\00"
@.str.3 = [8 x i8] c"libpath\00"
@__const.main.long_opts = [4 x %struct.option] [%struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i32 0, i8 0, i32* inttoptr (i64 104 to i32*) }, %struct.option { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i32 1, i8 0, i32* inttoptr (i64 112 to i32*) }, %struct.option { i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i32 0, i32 0), i32 1, i8 0, i32* inttoptr (i64 108 to i32*) }, %struct.option zeroinitializer]
@optarg = global i8* null
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [11 x i8] c"hook_entry\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca [4 x %struct.option]
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i8* @strrchr(i8* %15, i8 signext 47)
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %17, i8** %8
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 2
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i8*, i8** %8
%22 = call i32 @print_usage(i8* %21, i32 1)
br label %23
23: ; preds = %20, %2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %10
%24 = bitcast [4 x %struct.option]* %11 to i8*
br label %25
25: ; preds = %47, %23
%26 = load i32, i32* %4
%27 = load i8**, i8*** %5
%28 = load i8*, i8** %10
%29 = getelementptr inbounds [4 x %struct.option], [4 x %struct.option]* %11, i64 0, i64 0
%30 = call i32 @getopt_long(i32 %26, i8** %27, i8* %28, %struct.option* %29, i32* null)
store i32 %30, i32* %9
%31 = load i32, i32* %9
switch i32 %31, label %45 [
i32 104, label %32
i32 112, label %35
i32 108, label %38
i32 63, label %40
i32 -1, label %44
]
32: ; preds = %25
%33 = load i8*, i8** %8
%34 = call i32 @print_usage(i8* %33, i32 0)
br label %35
35: ; preds = %25, %32
%36 = load i8*, i8** @optarg
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %6
br label %46
38: ; preds = %25
%39 = load i8*, i8** @optarg
store i8* %39, i8** %7
br label %46
40: ; preds = %25
%41 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%42 = load i8*, i8** %8
%43 = call i32 @print_usage(i8* %42, i32 1)
br label %44
44: ; preds = %25, %40
br label %46
45: ; preds = %25
br label %46
46: ; preds = %45, %44, %38, %35
br label %47
47: ; preds = %46
%48 = load i32, i32* %9
%49 = icmp ne i32 %48, -1
br i1 %49, label %25, label %50
50: ; preds = %47
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), i8** %12
%51 = load i32, i32* %6
%52 = load i8*, i8** %7
%53 = load i8*, i8** %12
%54 = load i8*, i8** %12
%55 = call i32 @strlen(i8* %54)
%56 = call i32 @inject_remote_process(i32 %51, i8* %52, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %53, i32 %55)
ret i32 0
}

LLVM IR (-O2):
%struct.option = type { i8*, i32, i8, i32* }
@.str = [6 x i8] c"hp:l:\00"
@.str.1 = [5 x i8] c"help\00"
@.str.2 = [4 x i8] c"pid\00"
@.str.3 = [8 x i8] c"libpath\00"
@__const.main.long_opts = [4 x %struct.option] [%struct.option { i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i32 0, i32 0), i32 0, i8 0, i32* inttoptr (i64 104 to i32*) }, %struct.option { i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i32 0, i32 0), i32 1, i8 0, i32* inttoptr (i64 112 to i32*) }, %struct.option { i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i32 0, i32 0), i32 1, i8 0, i32* inttoptr (i64 108 to i32*) }, %struct.option zeroinitializer]
@optarg = global i8* null
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [11 x i8] c"hook_entry\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [4 x %struct.option]
%4 = load i8*, i8** %1
%5 = tail call i8* @strrchr(i8* dereferenceable(1) %4, i8 signext 47)
%6 = getelementptr inbounds i8, i8* %5, i64 1
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @print_usage(i8* %6, i32 1)
br label %10
10: ; preds = %8, %2
%11 = bitcast [4 x %struct.option]* %3 to i8*
%12 = getelementptr inbounds [4 x %struct.option], [4 x %struct.option]* %3, i64 0, i64 0
br label %13
13: ; preds = %27, %10
%14 = phi i8* [ undef, %10 ], [ %28, %27 ]
%15 = phi i32 [ undef, %10 ], [ %29, %27 ]
%16 = call i32 @getopt_long(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.option* %12, i32* null)
switch i32 %16, label %27 [
i32 104, label %17
i32 112, label %19
i32 108, label %22
i32 63, label %24
]
17: ; preds = %13
%18 = call i32 @print_usage(i8* %6, i32 0)
br label %19
19: ; preds = %13, %17
%20 = load i8*, i8** @optarg
%21 = call i32 @atoi(i8* %20)
br label %27
22: ; preds = %13
%23 = load i8*, i8** @optarg
br label %27
24: ; preds = %13
%25 = call i32 @putchar(i32 10)
%26 = call i32 @print_usage(i8* %6, i32 1)
br label %27
27: ; preds = %19, %22, %24, %13
%28 = phi i8* [ %14, %13 ], [ %14, %24 ], [ %23, %22 ], [ %14, %19 ]
%29 = phi i32 [ %15, %13 ], [ %15, %24 ], [ %15, %22 ], [ %21, %19 ]
%30 = icmp eq i32 %16, -1
br i1 %30, label %31, label %13
31: ; preds = %27
%32 = call i32 @strlen(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @inject_remote_process(i32 %29, i8* %28, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0), i32 %32)
ret i32 0
}