#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 pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int atoi(char *);
int getopt(int, char **, char *);
scalar_t__ inject(int, char *, char);
scalar_t__ optind;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  pid_t pid;
  char all_thrs = 0;
  int ch, r = EXIT_FAILURE;
  while ((ch = getopt(argc, argv, "a")) != -1) {
    switch (ch) {
    case 'a':
      all_thrs = 1;
      break;
    default:
      usage();
      break;
    }
  }
  argc -= optind;
  argv += optind;
  if (argc != 2) {
    usage();
    goto ret;
  }
  if ((pid = atoi(argv[1])) <= 0)
    goto ret;
  if (all_thrs)
    printf("[*] Suspending all threads and injecting %s in PID %d\n", argv[0],
           pid);
  else
    printf("[*] Injecting %s in PID %d\n", argv[0], pid);
  if (inject(pid, argv[0], all_thrs) != 0)
    goto ret;
  printf("[*] Done\n");
  r = EXIT_SUCCESS;
ret:
  return r;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x15(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe7c(%rip),%rdx
call   1050 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     11bb <main+0x6b>
mov    -0x1c(%rbp),%eax
sub    $0x61,%eax
jne    11af <main+0x5f>
jmp    11a6 <main+0x56>
movb   $0x1,-0x15(%rbp)
jmp    11b6 <main+0x66>
mov    $0x0,%al
call   1040 <usage@plt>
jmp    1176 <main+0x26>
mov    0x2e26(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fb <main+0xab>
mov    $0x0,%al
call   1040 <usage@plt>
jmp    1294 <main+0x144>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jg     1219 <main+0xc9>
jmp    1294 <main+0x144>
cmpb   $0x0,-0x15(%rbp)
je     1240 <main+0xf0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1258 <main+0x108>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
movsbl -0x15(%rbp),%edx
call   1070 <inject@plt>
cmp    $0x0,%rax
je     127a <main+0x12a>
jmp    1294 <main+0x144>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e61(%rip),%rax
mov    (%rax),%r14d
lea    0xeb8(%rip),%r15
lea    0xe78(%rip),%r12
xor    %r13d,%r13d
jmp    1197 <main+0x37>
nopl   (%rax)
xor    %eax,%eax
call   1050 <usage@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1060 <getopt@plt>
cmp    $0xffffffff,%eax
je     11ba <main+0x5a>
cmp    $0x61,%eax
jne    1190 <main+0x30>
mov    $0x1,%r13b
lea    0xe4a(%rip),%r15
jmp    1197 <main+0x37>
mov    0x2e27(%rip),%rax
mov    (%rax),%r12
sub    %r12d,%ebp
cmp    $0x2,%ebp
jne    1218 <main+0xb8>
mov    0x8(%rbx,%r12,8),%rdi
call   1070 <atoi@plt>
test   %eax,%eax
jle    121f <main+0xbf>
mov    %eax,%ebp
mov    (%rbx,%r12,8),%rsi
mov    %r15,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rbx,%r12,8),%rsi
movsbl %r13b,%edx
mov    %ebp,%edi
call   1080 <inject@plt>
test   %rax,%rax
jne    121f <main+0xbf>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%r14d
jmp    121f <main+0xbf>
xor    %eax,%eax
call   1050 <usage@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"a\00"
@optind = global i64 0
@.str.1 = [55 x i8] c"[*] Suspending all threads and injecting %s in PID %d\0A\00"
@.str.2 = [28 x i8] c"[*] Injecting %s in PID %d\0A\00"
@.str.3 = [10 x i8] c"[*] Done\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 0, i8* %7
%10 = load i32, i32* @EXIT_FAILURE
store i32 %10, i32* %9
br label %11
11: ; preds = %21, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %8
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i32, i32* %8
switch i32 %17, label %19 [
i32 97, label %18
]
18: ; preds = %16
store i8 1, i8* %7
br label %21
19: ; preds = %16
%20 = call i32 (...) @usage()
br label %21
21: ; preds = %19, %18
br label %11
22: ; preds = %11
%23 = load i64, i64* @optind
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = sub nsw i64 %25, %23
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
%28 = load i64, i64* @optind
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 %28
store i8** %30, i8*** %5
%31 = load i32, i32* %4
%32 = icmp ne i32 %31, 2
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 (...) @usage()
br label %69
35: ; preds = %22
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %6
%40 = icmp sle i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
br label %69
42: ; preds = %35
%43 = load i8, i8* %7
%44 = icmp ne i8 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %42
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = load i32, i32* %6
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i8* %48, i32 %49)
br label %57
51: ; preds = %42
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 0
%54 = load i8*, i8** %53
%55 = load i32, i32* %6
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %54, i32 %55)
br label %57
57: ; preds = %51, %45
%58 = load i32, i32* %6
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 0
%61 = load i8*, i8** %60
%62 = load i8, i8* %7
%63 = call i64 @inject(i32 %58, i8* %61, i8 signext %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %57
br label %69
66: ; preds = %57
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%68 = load i32, i32* @EXIT_SUCCESS
store i32 %68, i32* %9
br label %69
69: ; preds = %66, %65, %41, %33
%70 = load i32, i32* %9
ret i32 %70
}

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"a\00"
@optind = global i64 0
@.str.1 = [55 x i8] c"[*] Suspending all threads and injecting %s in PID %d\0A\00"
@.str.2 = [28 x i8] c"[*] Injecting %s in PID %d\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [9 x i8] c"[*] Done\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @EXIT_FAILURE
br label %4
4: ; preds = %7, %2
%5 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), %2 ], [ getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), %7 ]
%6 = phi i8 [ 0, %2 ], [ 1, %7 ]
br label %7
7: ; preds = %4, %9
%8 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %8, label %9 [
i32 -1, label %11
i32 97, label %4
]
9: ; preds = %7
%10 = tail call i32 (...) @usage()
br label %7
11: ; preds = %7
%12 = load i64, i64* @optind
%13 = trunc i64 %12 to i32
%14 = sub i32 %0, %13
%15 = getelementptr inbounds i8*, i8** %1, i64 %12
%16 = icmp eq i32 %14, 2
br i1 %16, label %19, label %17
17: ; preds = %11
%18 = tail call i32 (...) @usage()
br label %33
19: ; preds = %11
%20 = getelementptr inbounds i8*, i8** %15, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = icmp slt i32 %22, 1
br i1 %23, label %33, label %24
24: ; preds = %19
%25 = load i8*, i8** %15
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %5, i8* %25, i32 %22)
%27 = load i8*, i8** %15
%28 = tail call i64 @inject(i32 %22, i8* %27, i8 signext %6)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%32 = load i32, i32* @EXIT_SUCCESS
br label %33
33: ; preds = %24, %19, %30, %17
%34 = phi i32 [ %3, %17 ], [ %3, %19 ], [ %3, %24 ], [ %32, %30 ]
ret i32 %34
}