#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*/ tv;
struct timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ expected;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct timeval *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned long long);
int /*<<< orphan*/ stderr;
int accept_client(int s, unsigned long long key) {
  struct timeval tv = {5, 0};
  setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  int client = accept(s, NULL, NULL);
  if (client < 0) {
    perror("JVM did not respond");
    return -1;
  }
  char buf[35];
  size_t off = 0;
  while (off < sizeof(buf)) {
    ssize_t bytes = recv(client, buf + off, sizeof(buf) - off, 0);
    if (bytes <= 0) {
      fprintf(stderr, "The JVM connection was prematurely closed\n");
      close(client);
      return -1;
    }
    off += bytes;
  }
  char expected[35];
  snprintf(expected, sizeof(expected), "ATTACH_CONNECTED %016llx ", key);
  if (memcmp(buf, expected, sizeof(expected) - 1) != 0) {
    fprintf(stderr, "Unexpected JVM response\n");
    close(client);
    return -1;
  }
  return client;
}


x86 assembly (clang, -O0):
<accept_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f5 <accept_client+0x75>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <accept_client+0x152>
movq   $0x0,-0x48(%rbp)
cmpq   $0x23,-0x48(%rbp)
jae    126e <accept_client+0xee>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
add    -0x48(%rbp),%rsi
mov    $0x23,%eax
sub    -0x48(%rbp),%rax
mov    %eax,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jg     125d <accept_client+0xdd>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <accept_client+0x152>
mov    -0x50(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11fd <accept_client+0x7d>
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x23,%esi
lea    0xdc5(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x40(%rbp),%rdi
lea    -0x80(%rbp),%rsi
mov    $0x22,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     12cc <accept_client+0x14c>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <accept_client+0x152>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_client>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
movq   $0x5,0x8(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <accept@plt>
test   %eax,%eax
js     1279 <accept_client+0xf9>
mov    %eax,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%rsp,%rbx,1),%rsi
add    $0x10,%rsi
mov    $0x23,%edx
sub    %ebx,%edx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jle    124b <accept_client+0xcb>
add    %rax,%rbx
cmp    $0x22,%rbx
jbe    11e0 <accept_client+0x60>
lea    0xe32(%rip),%rdx
lea    0x40(%rsp),%r15
mov    %r15,%rdi
mov    $0x23,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
lea    0x10(%rsp),%rdi
mov    %r15,%rsi
mov    $0x22,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
je     126c <accept_client+0xec>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
jmp    125b <accept_client+0xdb>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd80(%rip),%rdi
call   1090 <perror@plt>
jmp    1267 <accept_client+0xe7>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@__const.accept_client.tv = %struct.timeval { i32 5, i32 0 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [20 x i8] c"JVM did not respond\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"The JVM connection was prematurely closed\0A\00"
@.str.2 = [26 x i8] c"ATTACH_CONNECTED %016llx \00"
@.str.3 = [25 x i8] c"Unexpected JVM response\0A\00"
define i32 @accept_client(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca [35 x i8]
%9 = alloca i64
%10 = alloca i64
%11 = alloca [35 x i8]
store i32 %0, i32* %4
store i64 %1, i64* %5
%12 = bitcast %struct.timeval* %6 to i8*
%13 = load i32, i32* %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_RCVTIMEO
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, %struct.timeval* %6, i32 8)
%17 = load i32, i32* %4
%18 = call i32 @accept(i32 %17, i32* null, i32* null)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
23: ; preds = %2
store i64 0, i64* %9
br label %24
24: ; preds = %43, %23
%25 = load i64, i64* %9
%26 = icmp ult i64 %25, 35
br i1 %26, label %27, label %47
27: ; preds = %24
%28 = load i32, i32* %7
%29 = getelementptr inbounds [35 x i8], [35 x i8]* %8, i64 0, i64 0
%30 = load i64, i64* %9
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = load i64, i64* %9
%33 = sub i64 35, %32
%34 = trunc i64 %33 to i32
%35 = call i64 @recv(i32 %28, i8* %31, i32 %34, i32 0)
store i64 %35, i64* %10
%36 = load i64, i64* %10
%37 = icmp sle i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %27
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %3
br label %62
43: ; preds = %27
%44 = load i64, i64* %10
%45 = load i64, i64* %9
%46 = add i64 %45, %44
store i64 %46, i64* %9
br label %24
47: ; preds = %24
%48 = getelementptr inbounds [35 x i8], [35 x i8]* %11, i64 0, i64 0
%49 = load i64, i64* %5
%50 = call i32 @snprintf(i8* %48, i32 35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 %49)
%51 = getelementptr inbounds [35 x i8], [35 x i8]* %8, i64 0, i64 0
%52 = getelementptr inbounds [35 x i8], [35 x i8]* %11, i64 0, i64 0
%53 = call i64 @memcmp(i8* %51, i8* %52, i32 34)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %47
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %3
br label %62
60: ; preds = %47
%61 = load i32, i32* %7
store i32 %61, i32* %3
br label %62
62: ; preds = %60, %55, %38, %21
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [20 x i8] c"JVM did not respond\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"The JVM connection was prematurely closed\0A\00"
@.str.2 = [26 x i8] c"ATTACH_CONNECTED %016llx \00"
@.str.3 = [25 x i8] c"Unexpected JVM response\0A\00"
define i32 @accept_client(i32 %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timeval*
%5 = alloca [35 x i8]
%6 = alloca [35 x i8]
%7 = bitcast i64* %3 to i8*
store i64 5, i64* %3
%8 = load i32, i32* @SOL_SOCKET
%9 = load i32, i32* @SO_RCVTIMEO
%10 = call i32 @setsockopt(i32 %0, i32 %8, i32 %9, %struct.timeval* %4, i32 8)
%11 = call i32 @accept(i32 %0, i32* null, i32* null)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %44
15: ; preds = %2
%16 = getelementptr inbounds [35 x i8], [35 x i8]* %5, i64 0, i64 0
br label %19
17: ; preds = %19
%18 = icmp ult i64 %26, 35
br i1 %18, label %19, label %31
19: ; preds = %15, %17
%20 = phi i64 [ 0, %15 ], [ %26, %17 ]
%21 = getelementptr inbounds [35 x i8], [35 x i8]* %5, i64 0, i64 %20
%22 = trunc i64 %20 to i32
%23 = sub nuw nsw i32 35, %22
%24 = call i64 @recv(i32 %11, i8* %21, i32 %23, i32 0)
%25 = icmp slt i64 %24, 1
%26 = add nuw i64 %24, %20
br i1 %25, label %27, label %17
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @close(i32 %11)
br label %42
31: ; preds = %17
%32 = getelementptr inbounds [35 x i8], [35 x i8]* %6, i64 0, i64 0
%33 = call i32 @snprintf(i8* %32, i32 35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i64 %1)
%34 = call i64 @memcmp(i8* %16, i8* %32, i32 34)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @close(i32 %11)
br label %40
40: ; preds = %31, %36
%41 = phi i32 [ -1, %36 ], [ %11, %31 ]
br label %42
42: ; preds = %27, %40
%43 = phi i32 [ %41, %40 ], [ -1, %27 ]
br label %44
44: ; preds = %42, %13
%45 = phi i32 [ -1, %13 ], [ %43, %42 ]
ret i32 %45
}