#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 /*<<< orphan*/ cmd;

/* Variables and functions */
int accept_client(int, unsigned long long);
int acquire_lock(char *, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ close_attach_socket(int, int);
int create_attach_socket(int *);
int /*<<< orphan*/ detach(int);
int lock_notification_files();
scalar_t__ notify_semaphore(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
unsigned long long random_key();
int read_response(int, char *);
int /*<<< orphan*/ release_lock(int);
int /*<<< orphan*/ translate_command(char *, int, int, char **);
int /*<<< orphan*/ unlock_notification_files(int);
scalar_t__ write_command(int, char *);
scalar_t__ write_reply_info(int, int, unsigned long long);
int jattach_openj9(int pid, int nspid, int argc, char **argv) {
  int attach_lock = acquire_lock("", "_attachlock");
  if (attach_lock < 0) {
    perror("Could not acquire attach lock");
    return 1;
  }
  int notif_count = 0;
  int port;
  int s = create_attach_socket(&port);
  if (s < 0) {
    perror("Failed to listen to attach socket");
    goto error;
  }
  unsigned long long key = random_key();
  if (write_reply_info(nspid, port, key) != 0) {
    perror("Could not write replyInfo");
    goto error;
  }
  notif_count = lock_notification_files();
  if (notify_semaphore(1, notif_count) != 0) {
    perror("Could not notify semaphore");
    goto error;
  }
  int fd = accept_client(s, key);
  if (fd < 0) {
    goto error;
  }
  close_attach_socket(s, nspid);
  unlock_notification_files(notif_count);
  notify_semaphore(-1, notif_count);
  release_lock(attach_lock);
  printf("Connected to remote JVM\n");
  char cmd[8192];
  translate_command(cmd, sizeof(cmd), argc, argv);
  if (write_command(fd, cmd) != 0) {
    perror("Error writing to socket");
    close(fd);
    return 1;
  }
  int result = read_response(fd, cmd);
  if (result != 1) {
    detach(fd);
  }
  close(fd);
  return result;
error:
  if (s >= 0) {
    close_attach_socket(s, nspid);
  }
  if (notif_count > 0) {
    unlock_notification_files(notif_count);
    notify_semaphore(-1, notif_count);
  }
  release_lock(attach_lock);
  return 1;
}


x86 assembly (clang, -O0):
<jattach_openj9>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xdca(%rip),%rsi
call   1100 <acquire_lock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1260 <jattach_openj9+0x50>
lea    0xdbd(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <jattach_openj9+0x20b>
movl   $0x0,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1060 <create_attach_socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    128e <jattach_openj9+0x7e>
lea    0xda6(%rip),%rdi
call   10e0 <perror@plt>
jmp    13d8 <jattach_openj9+0x1c8>
mov    $0x0,%al
call   10c0 <random_key@plt>
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1070 <write_reply_info@plt>
cmp    $0x0,%rax
je     12c3 <jattach_openj9+0xb3>
lea    0xd93(%rip),%rdi
call   10e0 <perror@plt>
jmp    13d8 <jattach_openj9+0x1c8>
mov    $0x0,%al
call   1030 <lock_notification_files@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    $0x1,%edi
call   1110 <notify_semaphore@plt>
cmp    $0x0,%rax
je     12f5 <jattach_openj9+0xe5>
lea    0xd7b(%rip),%rdi
call   10e0 <perror@plt>
jmp    13d8 <jattach_openj9+0x1c8>
mov    -0x28(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1080 <accept_client@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1313 <jattach_openj9+0x103>
jmp    13d8 <jattach_openj9+0x1c8>
mov    -0x28(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10f0 <close_attach_socket@plt>
mov    -0x20(%rbp),%edi
call   10a0 <unlock_notification_files@plt>
mov    -0x20(%rbp),%esi
mov    $0xffffffff,%edi
call   1110 <notify_semaphore@plt>
mov    -0x1c(%rbp),%edi
call   1050 <release_lock@plt>
lea    0xd3f(%rip),%rdi
call   1090 <printf@plt>
lea    -0x2040(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x2000,%esi
call   1130 <translate_command@plt>
mov    -0x34(%rbp),%edi
lea    -0x2040(%rbp),%rsi
call   1040 <write_command@plt>
cmp    $0x0,%rax
je     1398 <jattach_openj9+0x188>
lea    0xd1b(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x34(%rbp),%edi
call   10b0 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <jattach_openj9+0x20b>
mov    -0x34(%rbp),%edi
lea    -0x2040(%rbp),%rsi
call   1120 <read_response@plt>
mov    %eax,-0x2044(%rbp)
cmpl   $0x1,-0x2044(%rbp)
je     13c2 <jattach_openj9+0x1b2>
mov    -0x34(%rbp),%edi
call   10d0 <detach@plt>
mov    -0x34(%rbp),%edi
call   10b0 <close@plt>
mov    -0x2044(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    141b <jattach_openj9+0x20b>
cmpl   $0x0,-0x28(%rbp)
jl     13ed <jattach_openj9+0x1dd>
mov    -0x28(%rbp),%edi
mov    -0xc(%rbp),%esi
call   10f0 <close_attach_socket@plt>
cmpl   $0x0,-0x20(%rbp)
jle    140c <jattach_openj9+0x1fc>
mov    -0x20(%rbp),%edi
call   10a0 <unlock_notification_files@plt>
mov    -0x20(%rbp),%esi
mov    $0xffffffff,%edi
call   1110 <notify_semaphore@plt>
mov    -0x1c(%rbp),%edi
call   1050 <release_lock@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jattach_openj9>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
mov    %rcx,%r15
mov    %edx,%r14d
mov    %esi,%r13d
lea    0xe7f(%rip),%rdi
lea    0xdc8(%rip),%rsi
call   1100 <acquire_lock@plt>
test   %eax,%eax
js     1338 <jattach_openj9+0x128>
mov    %eax,%r12d
lea    0xc(%rsp),%rdi
call   1060 <create_attach_socket@plt>
test   %eax,%eax
js     1346 <jattach_openj9+0x136>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <random_key@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%esi
mov    %r13d,%edi
mov    %rax,%rdx
call   1080 <write_reply_info@plt>
test   %rax,%rax
jne    1354 <jattach_openj9+0x144>
mov    %r14d,0x8(%rsp)
xor    %eax,%eax
call   1030 <lock_notification_files@plt>
mov    %eax,%r14d
mov    $0x1,%edi
mov    %eax,%esi
call   1110 <notify_semaphore@plt>
test   %rax,%rax
jne    136c <jattach_openj9+0x15c>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1090 <accept_client@plt>
test   %eax,%eax
js     1378 <jattach_openj9+0x168>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r13d,%esi
call   10f0 <close_attach_socket@plt>
mov    %r14d,%edi
call   10a0 <unlock_notification_files@plt>
mov    $0xffffffff,%edi
mov    %r14d,%esi
call   1110 <notify_semaphore@plt>
mov    %r12d,%edi
call   1050 <release_lock@plt>
lea    0xdb5(%rip),%rdi
call   1070 <puts@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x2000,%esi
mov    0x8(%rsp),%edx
mov    %r15,%rcx
call   1130 <translate_command@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <write_command@plt>
test   %rax,%rax
jne    13bd <jattach_openj9+0x1ad>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1120 <read_response@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
je     132f <jattach_openj9+0x11f>
mov    %ebx,%edi
call   10d0 <detach@plt>
mov    %ebx,%edi
call   10b0 <close@plt>
jmp    13a9 <jattach_openj9+0x199>
lea    0xccd(%rip),%rdi
call   10e0 <perror@plt>
jmp    13a4 <jattach_openj9+0x194>
lea    0xcdd(%rip),%rdi
call   10e0 <perror@plt>
jmp    139c <jattach_openj9+0x18c>
lea    0xcf1(%rip),%rdi
call   10e0 <perror@plt>
mov    %ebp,%edi
mov    %r13d,%esi
call   10f0 <close_attach_socket@plt>
jmp    139c <jattach_openj9+0x18c>
lea    0xcf3(%rip),%rdi
call   10e0 <perror@plt>
mov    %ebp,%edi
mov    %r13d,%esi
call   10f0 <close_attach_socket@plt>
test   %r14d,%r14d
jle    139c <jattach_openj9+0x18c>
mov    %r14d,%edi
call   10a0 <unlock_notification_files@plt>
mov    $0xffffffff,%edi
mov    %r14d,%esi
call   1110 <notify_semaphore@plt>
mov    %r12d,%edi
call   1050 <release_lock@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcbd(%rip),%rdi
call   10e0 <perror@plt>
mov    %ebx,%edi
call   10b0 <close@plt>
jmp    13a4 <jattach_openj9+0x194>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [12 x i8] c"_attachlock\00"
@.str.2 = [30 x i8] c"Could not acquire attach lock\00"
@.str.3 = [34 x i8] c"Failed to listen to attach socket\00"
@.str.4 = [26 x i8] c"Could not write replyInfo\00"
@.str.5 = [27 x i8] c"Could not notify semaphore\00"
@.str.6 = [25 x i8] c"Connected to remote JVM\0A\00"
@.str.7 = [24 x i8] c"Error writing to socket\00"
define i32 @jattach_openj9(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
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca [8192 x i8]
%17 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8** %3, i8*** %9
%18 = call i32 @acquire_lock(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %5
br label %106
23: ; preds = %4
store i32 0, i32* %11
%24 = call i32 @create_attach_socket(i32* %12)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %88
29: ; preds = %23
%30 = call i64 (...) @random_key()
store i64 %30, i64* %14
%31 = load i32, i32* %7
%32 = load i32, i32* %12
%33 = load i64, i64* %14
%34 = call i64 @write_reply_info(i32 %31, i32 %32, i64 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %88
38: ; preds = %29
%39 = call i32 (...) @lock_notification_files()
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = call i64 @notify_semaphore(i32 1, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %88
45: ; preds = %38
%46 = load i32, i32* %13
%47 = load i64, i64* %14
%48 = call i32 @accept_client(i32 %46, i64 %47)
store i32 %48, i32* %15
%49 = load i32, i32* %15
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %45
br label %88
52: ; preds = %45
%53 = load i32, i32* %13
%54 = load i32, i32* %7
%55 = call i32 @close_attach_socket(i32 %53, i32 %54)
%56 = load i32, i32* %11
%57 = call i32 @unlock_notification_files(i32 %56)
%58 = load i32, i32* %11
%59 = call i64 @notify_semaphore(i32 -1, i32 %58)
%60 = load i32, i32* %10
%61 = call i32 @release_lock(i32 %60)
%62 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%63 = getelementptr inbounds [8192 x i8], [8192 x i8]* %16, i64 0, i64 0
%64 = load i32, i32* %8
%65 = load i8**, i8*** %9
%66 = call i32 @translate_command(i8* %63, i32 8192, i32 %64, i8** %65)
%67 = load i32, i32* %15
%68 = getelementptr inbounds [8192 x i8], [8192 x i8]* %16, i64 0, i64 0
%69 = call i64 @write_command(i32 %67, i8* %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %52
%72 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%73 = load i32, i32* %15
%74 = call i32 @close(i32 %73)
store i32 1, i32* %5
br label %106
75: ; preds = %52
%76 = load i32, i32* %15
%77 = getelementptr inbounds [8192 x i8], [8192 x i8]* %16, i64 0, i64 0
%78 = call i32 @read_response(i32 %76, i8* %77)
store i32 %78, i32* %17
%79 = load i32, i32* %17
%80 = icmp ne i32 %79, 1
br i1 %80, label %81, label %84
81: ; preds = %75
%82 = load i32, i32* %15
%83 = call i32 @detach(i32 %82)
br label %84
84: ; preds = %81, %75
%85 = load i32, i32* %15
%86 = call i32 @close(i32 %85)
%87 = load i32, i32* %17
store i32 %87, i32* %5
br label %106
88: ; preds = %51, %43, %36, %27
%89 = load i32, i32* %13
%90 = icmp sge i32 %89, 0
br i1 %90, label %91, label %95
91: ; preds = %88
%92 = load i32, i32* %13
%93 = load i32, i32* %7
%94 = call i32 @close_attach_socket(i32 %92, i32 %93)
br label %95
95: ; preds = %91, %88
%96 = load i32, i32* %11
%97 = icmp sgt i32 %96, 0
br i1 %97, label %98, label %103
98: ; preds = %95
%99 = load i32, i32* %11
%100 = call i32 @unlock_notification_files(i32 %99)
%101 = load i32, i32* %11
%102 = call i64 @notify_semaphore(i32 -1, i32 %101)
br label %103
103: ; preds = %98, %95
%104 = load i32, i32* %10
%105 = call i32 @release_lock(i32 %104)
store i32 1, i32* %5
br label %106
106: ; preds = %103, %84, %71, %21
%107 = load i32, i32* %5
ret i32 %107
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [12 x i8] c"_attachlock\00"
@.str.2 = [30 x i8] c"Could not acquire attach lock\00"
@.str.3 = [34 x i8] c"Failed to listen to attach socket\00"
@.str.4 = [26 x i8] c"Could not write replyInfo\00"
@.str.5 = [27 x i8] c"Could not notify semaphore\00"
@.str.7 = [24 x i8] c"Error writing to socket\00"
@str = [24 x i8] c"Connected to remote JVM\00"
define i32 @jattach_openj9(i32 %0, i32 %1, i32 %2, i8** %3) {
%5 = alloca i32
%6 = alloca [8192 x i8]
%7 = tail call i32 @acquire_lock(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %64
11: ; preds = %4
%12 = bitcast i32* %5 to i8*
%13 = call i32 @create_attach_socket(i32* %5)
%14 = icmp slt i32 %13, 0
br i1 %14, label %52, label %15
15: ; preds = %11
%16 = call i64 (...) @random_key()
%17 = load i32, i32* %5
%18 = call i64 @write_reply_info(i32 %1, i32 %17, i64 %16)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @close_attach_socket(i32 %13, i32 %1)
br label %60
23: ; preds = %15
%24 = call i32 (...) @lock_notification_files()
%25 = call i64 @notify_semaphore(i32 1, i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %54
29: ; preds = %23
%30 = call i32 @accept_client(i32 %13, i64 %16)
%31 = icmp slt i32 %30, 0
br i1 %31, label %54, label %32
32: ; preds = %29
%33 = call i32 @close_attach_socket(i32 %13, i32 %1)
%34 = call i32 @unlock_notification_files(i32 %24)
%35 = call i64 @notify_semaphore(i32 -1, i32 %24)
%36 = call i32 @release_lock(i32 %7)
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%38 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%39 = call i32 @translate_command(i8* %38, i32 8192, i32 %2, i8** %3)
%40 = call i64 @write_command(i32 %30, i8* %38)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%44 = call i32 @close(i32 %30)
br label %62
45: ; preds = %32
%46 = call i32 @read_response(i32 %30, i8* %38)
%47 = icmp eq i32 %46, 1
br i1 %47, label %50, label %48
48: ; preds = %45
%49 = call i32 @detach(i32 %30)
br label %50
50: ; preds = %48, %45
%51 = call i32 @close(i32 %30)
br label %62
52: ; preds = %11
%53 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %60
54: ; preds = %27, %29
%55 = call i32 @close_attach_socket(i32 %13, i32 %1)
%56 = icmp sgt i32 %24, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @unlock_notification_files(i32 %24)
%59 = call i64 @notify_semaphore(i32 -1, i32 %24)
br label %60
60: ; preds = %20, %52, %57, %54
%61 = call i32 @release_lock(i32 %7)
br label %62
62: ; preds = %60, %50, %42
%63 = phi i32 [ 1, %60 ], [ 1, %42 ], [ %46, %50 ]
br label %64
64: ; preds = %62, %9
%65 = phi i32 [ 1, %9 ], [ %63, %62 ]
ret i32 %65
}