#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 */
scalar_t__ check_socket(int);
int /*<<< orphan*/ close(int);
int connect_socket(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read_response(int, int, char **);
scalar_t__ start_attach_mechanism(int, int);
scalar_t__ write_command(int, int, char **);
int jattach_hotspot(int pid, int nspid, int argc, char **argv) {
  if (check_socket(nspid) != 0 && start_attach_mechanism(pid, nspid) != 0) {
    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, argv) != 0) {
    perror("Error writing to socket");
    close(fd);
    return 1;
  }
  int result = read_response(fd, argc, argv);
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<jattach_hotspot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <check_socket@plt>
cmp    $0x0,%rax
je     11d4 <jattach_hotspot+0x54>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <start_attach_mechanism@plt>
cmp    $0x0,%rax
je     11d4 <jattach_hotspot+0x54>
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <jattach_hotspot+0xe6>
mov    -0xc(%rbp),%edi
call   1050 <connect_socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1201 <jattach_hotspot+0x81>
lea    0xe31(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <jattach_hotspot+0xe6>
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1030 <write_command@plt>
cmp    $0x0,%rax
je     1246 <jattach_hotspot+0xc6>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1266 <jattach_hotspot+0xe6>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   10a0 <read_response@plt>
mov    %eax,-0x20(%rbp)
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):
<jattach_hotspot>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%ebp
mov    %esi,%edi
call   1080 <check_socket@plt>
test   %rax,%rax
je     11ab <jattach_hotspot+0x2b>
mov    %ebp,%edi
mov    %ebx,%esi
call   1070 <start_attach_mechanism@plt>
test   %rax,%rax
jne    1218 <jattach_hotspot+0x98>
mov    %ebx,%edi
call   1050 <connect_socket@plt>
cmp    $0xffffffff,%eax
je     11fa <jattach_hotspot+0x7a>
mov    %eax,%ebx
lea    0xe95(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r14,%rdx
call   1030 <write_command@plt>
test   %rax,%rax
jne    1203 <jattach_hotspot+0x83>
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r14,%rdx
call   10a0 <read_response@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rdi
jmp    121f <jattach_hotspot+0x9f>
lea    0xe33(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1224 <jattach_hotspot+0xa4>
lea    0xde1(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    11ef <jattach_hotspot+0x6f>

LLVM IR (-O0):
@.str = [33 x i8] c"Could not start attach mechanism\00"
@.str.1 = [28 x i8] c"Could not connect to socket\00"
@.str.2 = [25 x i8] c"Connected to remote JVM\0A\00"
@.str.3 = [24 x i8] c"Error writing to socket\00"
define i32 @jattach_hotspot(i32 %0, i32 %1, i32 %2, i8** %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8** %3, i8*** %9
%12 = load i32, i32* %7
%13 = call i64 @check_socket(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = call i64 @start_attach_mechanism(i32 %16, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %48
22: ; preds = %15, %4
%23 = load i32, i32* %7
%24 = call i32 @connect_socket(i32 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %5
br label %48
29: ; preds = %22
%30 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %10
%32 = load i32, i32* %8
%33 = load i8**, i8*** %9
%34 = call i64 @write_command(i32 %31, i32 %32, i8** %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* %10
%39 = call i32 @close(i32 %38)
store i32 1, i32* %5
br label %48
40: ; preds = %29
%41 = load i32, i32* %10
%42 = load i32, i32* %8
%43 = load i8**, i8*** %9
%44 = call i32 @read_response(i32 %41, i32 %42, i8** %43)
store i32 %44, i32* %11
%45 = load i32, i32* %10
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %11
store i32 %47, i32* %5
br label %48
48: ; preds = %40, %36, %27, %20
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str = [33 x i8] c"Could not start attach mechanism\00"
@.str.1 = [28 x i8] c"Could not connect to socket\00"
@.str.3 = [24 x i8] c"Error writing to socket\00"
@str = [24 x i8] c"Connected to remote JVM\00"
define i32 @jattach_hotspot(i32 %0, i32 %1, i32 %2, i8** %3) {
%5 = tail call i64 @check_socket(i32 %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = tail call i64 @start_attach_mechanism(i32 %0, i32 %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %27
12: ; preds = %7, %4
%13 = tail call i32 @connect_socket(i32 %1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %27
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%19 = tail call i64 @write_command(i32 %13, i32 %2, i8** %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @close(i32 %13)
br label %27
24: ; preds = %17
%25 = tail call i32 @read_response(i32 %13, i32 %2, i8** %3)
%26 = tail call i32 @close(i32 %13)
br label %27
27: ; preds = %15, %21, %24, %10
%28 = phi i32 [ 1, %10 ], [ 1, %15 ], [ 1, %21 ], [ %25, %24 ]
ret i32 %28
}