#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 */

/* Variables and functions */
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int atoi(char *);
int /*<<< orphan*/ check_socket(int);
int /*<<< orphan*/ close(int);
int connect_socket(int);
scalar_t__ enter_mount_ns(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int nspid_for_pid(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read_response(int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ start_attach_mechanism(int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write_command(int, int, char **);
int main(int argc, char **argv) {
  if (argc < 3) {
    printf("Usage: jattach <pid> <cmd> <args> ...\n");
    return 1;
  }
  int pid = atoi(argv[1]);
  if (pid == 0) {
    perror("Invalid pid provided");
    return 1;
  }
  int nspid = nspid_for_pid(pid);
  if (enter_mount_ns(pid) < 0) {
    fprintf(stderr, "WARNING: couldn't enter target process mnt namespace\n");
  }
  signal(SIGPIPE, SIG_IGN);
  if (!check_socket(nspid) && !start_attach_mechanism(pid, nspid)) {
    perror("Could not start attach mechanism");
    return 1;
  }
  int fd = connect_socket(nspid);
  if (fd == -1) {
    perror("Could not connect to socket");
    return 1;
  }
  printf("Connected to remote JVM\n");
  if (!write_command(fd, argc - 2, argv + 2)) {
    perror("Error writing to socket");
    close(fd);
    return 1;
  }
  printf("Response code = ");
  fflush(stdout);
  int result = read_response(fd);
  printf("\n");
  close(fd);
  return result;
}


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)
cmpl   $0x3,-0x8(%rbp)
jge    1218 <main+0x38>
lea    0xdf9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1a7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    124a <main+0x6a>
lea    0xdee(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1a7>
mov    -0x14(%rbp),%edi
call   1100 <nspid_for_pid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   10a0 <enter_mount_ns@plt>
cmp    $0x0,%rax
jge    127c <main+0x9c>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    -0x18(%rbp),%edi
call   10c0 <check_socket@plt>
cmp    $0x0,%eax
jne    12d0 <main+0xf0>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <start_attach_mechanism@plt>
cmp    $0x0,%eax
jne    12d0 <main+0xf0>
lea    0xdb3(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1a7>
mov    -0x18(%rbp),%edi
call   1050 <connect_socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12fd <main+0x11d>
lea    0xda7(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1a7>
lea    0xdab(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
sub    $0x2,%esi
mov    -0x10(%rbp),%rdx
add    $0x10,%rdx
call   1030 <write_command@plt>
cmp    $0x0,%eax
jne    1348 <main+0x168>
lea    0xd99(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1a7>
lea    0xd91(%rip),%rdi
call   1040 <printf@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    -0x1c(%rbp),%edi
call   10f0 <read_response@plt>
mov    %eax,-0x20(%rbp)
lea    0xcb1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     1222 <main+0x22>
lea    0xeb6(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%eax
jmp    1302 <main+0x102>
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1100 <atoi@plt>
test   %eax,%eax
je     130d <main+0x10d>
mov    %eax,%ebx
mov    %eax,%edi
call   1120 <nspid_for_pid@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   10c0 <enter_mount_ns@plt>
test   %rax,%rax
jns    1265 <main+0x65>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   10b0 <fprintf@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
call   10a0 <signal@plt>
mov    %r15d,%edi
call   10e0 <check_socket@plt>
test   %eax,%eax
jne    129a <main+0x9a>
mov    %ebx,%edi
mov    %r15d,%esi
call   1090 <start_attach_mechanism@plt>
test   %eax,%eax
je     1347 <main+0x147>
mov    %r15d,%edi
call   1070 <connect_socket@plt>
cmp    $0xffffffff,%eax
je     131e <main+0x11e>
mov    %eax,%ebx
lea    0xe01(%rip),%rdi
call   1050 <puts@plt>
add    $0xfffffffe,%ebp
add    $0x10,%r14
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r14,%rdx
call   1040 <write_command@plt>
test   %eax,%eax
je     132f <main+0x12f>
lea    0xdcd(%rip),%rdi
call   1060 <printf@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
mov    %ebx,%edi
call   1110 <read_response@plt>
mov    %eax,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcec(%rip),%rdi
call   10f0 <perror@plt>
jmp    1218 <main+0x18>
lea    0xd47(%rip),%rdi
call   10f0 <perror@plt>
jmp    1218 <main+0x18>
lea    0xd52(%rip),%rdi
call   10f0 <perror@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    1218 <main+0x18>
lea    0xcfd(%rip),%rdi
call   10f0 <perror@plt>
jmp    1218 <main+0x18>

LLVM IR (-O0):
@.str = [39 x i8] c"Usage: jattach <pid> <cmd> <args> ...\0A\00"
@.str.1 = [21 x i8] c"Invalid pid provided\00"
@stderr = global i32 0
@.str.2 = [54 x i8] c"WARNING: couldn't enter target process mnt namespace\0A\00"
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str.3 = [33 x i8] c"Could not start attach mechanism\00"
@.str.4 = [28 x i8] c"Could not connect to socket\00"
@.str.5 = [25 x i8] c"Connected to remote JVM\0A\00"
@.str.6 = [24 x i8] c"Error writing to socket\00"
@.str.7 = [17 x i8] c"Response code = \00"
@stdout = global i32 0
@.str.8 = [2 x i8] c"\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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %76
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %76
23: ; preds = %14
%24 = load i32, i32* %6
%25 = call i32 @nspid_for_pid(i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %6
%27 = call i64 @enter_mount_ns(i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %29, %23
%33 = load i32, i32* @SIGPIPE
%34 = load i32, i32* @SIG_IGN
%35 = call i32 @signal(i32 %33, i32 %34)
%36 = load i32, i32* %7
%37 = call i32 @check_socket(i32 %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %32
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = call i32 @start_attach_mechanism(i32 %40, i32 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %76
46: ; preds = %39, %32
%47 = load i32, i32* %7
%48 = call i32 @connect_socket(i32 %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %76
53: ; preds = %46
%54 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32, i32* %8
%56 = load i32, i32* %4
%57 = sub nsw i32 %56, 2
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 2
%60 = call i32 @write_command(i32 %55, i32 %57, i8** %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %66, label %62
62: ; preds = %53
%63 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
store i32 1, i32* %3
br label %76
66: ; preds = %53
%67 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%68 = load i32, i32* @stdout
%69 = call i32 @fflush(i32 %68)
%70 = load i32, i32* %8
%71 = call i32 @read_response(i32 %70)
store i32 %71, i32* %9
%72 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%73 = load i32, i32* %8
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %9
store i32 %75, i32* %3
br label %76
76: ; preds = %66, %62, %51, %44, %21, %12
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"Invalid pid provided\00"
@stderr = global i32 0
@.str.2 = [54 x i8] c"WARNING: couldn't enter target process mnt namespace\0A\00"
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str.3 = [33 x i8] c"Could not start attach mechanism\00"
@.str.4 = [28 x i8] c"Could not connect to socket\00"
@.str.6 = [24 x i8] c"Error writing to socket\00"
@.str.7 = [17 x i8] c"Response code = \00"
@stdout = global i32 0
@str = [24 x i8] c"Connected to remote JVM\00"
@str.9 = [38 x i8] c"Usage: jattach <pid> <cmd> <args> ...\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.9, i64 0, i64 0))
br label %52
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %52
13: ; preds = %6
%14 = tail call i32 @nspid_for_pid(i32 %9)
%15 = tail call i64 @enter_mount_ns(i32 %9)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %17, %13
%21 = load i32, i32* @SIGPIPE
%22 = load i32, i32* @SIG_IGN
%23 = tail call i32 @signal(i32 %21, i32 %22)
%24 = tail call i32 @check_socket(i32 %14)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = tail call i32 @start_attach_mechanism(i32 %9, i32 %14)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %52
31: ; preds = %26, %20
%32 = tail call i32 @connect_socket(i32 %14)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %52
36: ; preds = %31
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%38 = add nsw i32 %0, -2
%39 = getelementptr inbounds i8*, i8** %1, i64 2
%40 = tail call i32 @write_command(i32 %32, i32 %38, i8** %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%44 = tail call i32 @close(i32 %32)
br label %52
45: ; preds = %36
%46 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%47 = load i32, i32* @stdout
%48 = tail call i32 @fflush(i32 %47)
%49 = tail call i32 @read_response(i32 %32)
%50 = tail call i32 @putchar(i32 10)
%51 = tail call i32 @close(i32 %32)
br label %52
52: ; preds = %11, %34, %42, %45, %29, %4
%53 = phi i32 [ 1, %4 ], [ 1, %11 ], [ 1, %29 ], [ 1, %34 ], [ %49, %45 ], [ 1, %42 ]
ret i32 %53
}