#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 size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
scalar_t__ strncmp(char *, char *, int);
int read_response(int fd, int argc, char **argv) {
  char buf[8192];
  ssize_t bytes = read(fd, buf, sizeof(buf) - 1);
  if (bytes == 0) {
    fprintf(stderr, "Unexpected EOF reading response\n");
    return 1;
  } else if (bytes < 0) {
    perror("Error reading response");
    return 1;
  }
  buf[bytes] = 0;
  int result = atoi(buf);
  if (result == 0 && argc > 0 && strcmp(argv[0], "load") == 0) {
    size_t total = bytes;
    while (total < sizeof(buf) - 1 &&
           (bytes = read(fd, buf + total, sizeof(buf) - 1 - total)) > 0) {
      total += (size_t)bytes;
    }
    bytes = total;
    buf[bytes] = 0;
    result =
        atoi(strncmp(buf + 2, "return code: ", 13) == 0 ? buf + 15 : buf + 2);
  }
  printf("JVM response code = ");
  do {
    fwrite(buf, 1, bytes, stdout);
    bytes = read(fd, buf, sizeof(buf));
  } while (bytes > 0);
  printf("\n");
  return result;
}


x86 assembly (clang, -O0):
<read_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x1fff,%edx
call   1050 <read@plt>
mov    %rax,-0x2028(%rbp)
cmpq   $0x0,-0x2028(%rbp)
jne    11df <read_response+0x5f>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <read_response+0x254>
cmpq   $0x0,-0x2028(%rbp)
jae    1205 <read_response+0x85>
lea    0xe2d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <read_response+0x254>
jmp    120a <read_response+0x8a>
mov    -0x2028(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
lea    -0x2020(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x202c(%rbp)
cmpl   $0x0,-0x202c(%rbp)
jne    1369 <read_response+0x1e9>
cmpl   $0x0,-0xc(%rbp)
jle    1369 <read_response+0x1e9>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xde8(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1369 <read_response+0x1e9>
mov    -0x2028(%rbp),%rax
mov    %rax,-0x2038(%rbp)
xor    %eax,%eax
cmpq   $0x1fff,-0x2038(%rbp)
mov    %al,-0x2039(%rbp)
jae    12be <read_response+0x13e>
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rsi
add    -0x2038(%rbp),%rsi
mov    $0x1fff,%eax
sub    -0x2038(%rbp),%rax
mov    %eax,%edx
call   1050 <read@plt>
mov    %rax,-0x2028(%rbp)
cmp    $0x0,%rax
seta   %al
mov    %al,-0x2039(%rbp)
mov    -0x2039(%rbp),%al
test   $0x1,%al
jne    12d1 <read_response+0x151>
jmp    12eb <read_response+0x16b>
mov    -0x2028(%rbp),%rax
add    -0x2038(%rbp),%rax
mov    %rax,-0x2038(%rbp)
jmp    126d <read_response+0xed>
mov    -0x2038(%rbp),%rax
mov    %rax,-0x2028(%rbp)
mov    -0x2028(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
lea    -0x2020(%rbp),%rdi
add    $0x2,%rdi
lea    0xd23(%rip),%rsi
mov    $0xd,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1345 <read_response+0x1c5>
lea    -0x2020(%rbp),%rax
add    $0xf,%rax
mov    %rax,-0x2048(%rbp)
jmp    1357 <read_response+0x1d7>
lea    -0x2020(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x2048(%rbp)
mov    -0x2048(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x202c(%rbp)
lea    0xcdb(%rip),%rdi
call   1040 <printf@plt>
lea    -0x2020(%rbp),%rdi
mov    -0x2028(%rbp),%rdx
mov    0x2c4e(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    -0x8(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   1050 <read@plt>
mov    %rax,-0x2028(%rbp)
cmpq   $0x0,-0x2028(%rbp)
ja     1375 <read_response+0x1f5>
lea    0xc59(%rip),%rdi
call   1040 <printf@plt>
mov    -0x202c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_response>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,%r12d
mov    %rsp,%rsi
mov    $0x1fff,%edx
call   1060 <read@plt>
test   %rax,%rax
je     1297 <read_response+0x117>
mov    %rax,%rbx
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
test   %ebp,%ebp
jle    1245 <read_response+0xc5>
test   %r14d,%r14d
jne    1245 <read_response+0xc5>
mov    (%r15),%rdi
lea    0xe4b(%rip),%rsi
call   1070 <strcmp@plt>
xor    %r14d,%r14d
test   %rax,%rax
jne    1245 <read_response+0xc5>
jmp    120b <read_response+0x8b>
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%rsp,%rbx,1),%rsi
mov    $0x1fff,%edx
sub    %ebx,%edx
mov    %r12d,%edi
call   1060 <read@plt>
test   %rax,%rax
je     1214 <read_response+0x94>
add    %rax,%rbx
cmp    $0x1ffe,%rbx
jbe    11f0 <read_response+0x70>
movb   $0x0,(%rsp,%rbx,1)
lea    0x2(%rsp),%rbp
lea    0xe02(%rip),%rsi
mov    %rbp,%rdi
mov    $0xd,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
lea    0xf(%rsp),%rdi
cmovne %rbp,%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
lea    0xde8(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d80(%rip),%r15
mov    %rsp,%rbp
nopl   0x0(%rax,%rax,1)
mov    (%r15),%ecx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   10a0 <fwrite@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x2000,%edx
call   1060 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1260 <read_response+0xe0>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    12b2 <read_response+0x132>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [33 x i8] c"Unexpected EOF reading response\0A\00"
@.str.1 = [23 x i8] c"Error reading response\00"
@.str.2 = [5 x i8] c"load\00"
@.str.3 = [14 x i8] c"return code: \00"
@.str.4 = [21 x i8] c"JVM response code = \00"
@stdout = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
define i32 @read_response(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca [8192 x i8]
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%12 = load i32, i32* %5
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%14 = call i64 @read(i32 %12, i8* %13, i32 8191)
store i64 %14, i64* %9
%15 = load i64, i64* %9
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %96
20: ; preds = %3
%21 = load i64, i64* %9
%22 = icmp ult i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %96
25: ; preds = %20
br label %26
26: ; preds = %25
%27 = load i64, i64* %9
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %80
33: ; preds = %26
%34 = load i32, i32* %6
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %80
36: ; preds = %33
%37 = load i8**, i8*** %7
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %80
42: ; preds = %36
%43 = load i64, i64* %9
store i64 %43, i64* %11
br label %44
44: ; preds = %59, %42
%45 = load i64, i64* %11
%46 = icmp ult i64 %45, 8191
br i1 %46, label %47, label %57
47: ; preds = %44
%48 = load i32, i32* %5
%49 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%50 = load i64, i64* %11
%51 = getelementptr inbounds i8, i8* %49, i64 %50
%52 = load i64, i64* %11
%53 = sub i64 8191, %52
%54 = trunc i64 %53 to i32
%55 = call i64 @read(i32 %48, i8* %51, i32 %54)
store i64 %55, i64* %9
%56 = icmp ugt i64 %55, 0
br label %57
57: ; preds = %47, %44
%58 = phi i1 [ false, %44 ], [ %56, %47 ]
br i1 %58, label %59, label %63
59: ; preds = %57
%60 = load i64, i64* %9
%61 = load i64, i64* %11
%62 = add i64 %61, %60
store i64 %62, i64* %11
br label %44
63: ; preds = %57
%64 = load i64, i64* %11
store i64 %64, i64* %9
%65 = load i64, i64* %9
%66 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 %65
store i8 0, i8* %66
%67 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%68 = getelementptr inbounds i8, i8* %67, i64 2
%69 = call i64 @strncmp(i8* %68, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 13)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %63
%72 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%73 = getelementptr inbounds i8, i8* %72, i64 15
br label %77
74: ; preds = %63
%75 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%76 = getelementptr inbounds i8, i8* %75, i64 2
br label %77
77: ; preds = %74, %71
%78 = phi i8* [ %73, %71 ], [ %76, %74 ]
%79 = call i32 @atoi(i8* %78)
store i32 %79, i32* %10
br label %80
80: ; preds = %77, %36, %33, %26
%81 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %82
82: ; preds = %90, %80
%83 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%84 = load i64, i64* %9
%85 = load i32, i32* @stdout
%86 = call i32 @fwrite(i8* %83, i32 1, i64 %84, i32 %85)
%87 = load i32, i32* %5
%88 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%89 = call i64 @read(i32 %87, i8* %88, i32 8192)
store i64 %89, i64* %9
br label %90
90: ; preds = %82
%91 = load i64, i64* %9
%92 = icmp ugt i64 %91, 0
br i1 %92, label %82, label %93
93: ; preds = %90
%94 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%95 = load i32, i32* %10
store i32 %95, i32* %4
br label %96
96: ; preds = %93, %23, %17
%97 = load i32, i32* %4
ret i32 %97
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [33 x i8] c"Unexpected EOF reading response\0A\00"
@.str.2 = [5 x i8] c"load\00"
@.str.3 = [14 x i8] c"return code: \00"
@.str.4 = [21 x i8] c"JVM response code = \00"
@stdout = global i32 0
define i32 @read_response(i32 %0, i32 %1, i8** nocapture %2) {
%4 = alloca [8192 x i8]
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%6 = call i64 @read(i32 %0, i8* %5, i32 8191)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %54
11: ; preds = %3
%12 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 %6
store i8 0, i8* %12
%13 = call i32 @atoi(i8* %5)
%14 = icmp eq i32 %13, 0
%15 = icmp sgt i32 %1, 0
%16 = and i1 %15, %14
br i1 %16, label %17, label %42
17: ; preds = %11
%18 = load i8*, i8** %2
%19 = tail call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %42
21: ; preds = %17
%22 = icmp ult i64 %6, 8191
br i1 %22, label %25, label %33
23: ; preds = %25
%24 = icmp ult i64 %32, 8191
br i1 %24, label %25, label %33
25: ; preds = %21, %23
%26 = phi i64 [ %32, %23 ], [ %6, %21 ]
%27 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 %26
%28 = trunc i64 %26 to i32
%29 = sub nuw nsw i32 8191, %28
%30 = call i64 @read(i32 %0, i8* %27, i32 %29)
%31 = icmp eq i64 %30, 0
%32 = add i64 %30, %26
br i1 %31, label %33, label %23
33: ; preds = %25, %23, %21
%34 = phi i64 [ %6, %21 ], [ %32, %23 ], [ %26, %25 ]
%35 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 2
%37 = call i64 @strncmp(i8* %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 13)
%38 = icmp eq i64 %37, 0
%39 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 15
%40 = select i1 %38, i8* %39, i8* %36
%41 = call i32 @atoi(i8* %40)
br label %42
42: ; preds = %33, %17, %11
%43 = phi i64 [ %34, %33 ], [ %6, %17 ], [ %6, %11 ]
%44 = phi i32 [ %41, %33 ], [ 0, %17 ], [ %13, %11 ]
%45 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %46, %42
%47 = phi i64 [ %43, %42 ], [ %50, %46 ]
%48 = load i32, i32* @stdout
%49 = call i32 @fwrite(i8* %5, i32 1, i64 %47, i32 %48)
%50 = call i64 @read(i32 %0, i8* %5, i32 8192)
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %46
52: ; preds = %46
%53 = call i32 @putchar(i32 10)
br label %54
54: ; preds = %52, %8
%55 = phi i32 [ 1, %8 ], [ %44, %52 ]
ret i32 %55
}