#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 scalar_t__ ssize_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_unescaped(char *);
scalar_t__ read(int, char *, size_t);
char *realloc(char *, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strncmp(char const *, char *, int);
char *strstr(char *, char *);
int read_response(int fd, const char *cmd) {
  size_t size = 8192;
  char *buf = malloc(size);
  size_t off = 0;
  while (buf != NULL) {
    ssize_t bytes = read(fd, buf + off, size - off);
    if (bytes == 0) {
      fprintf(stderr, "Unexpected EOF reading response\n");
      return 1;
    } else if (bytes < 0) {
      perror("Error reading response");
      return 1;
    }
    off += bytes;
    if (buf[off - 1] == 0) {
      break;
    }
    if (off >= size) {
      buf = realloc(buf, size *= 2);
    }
  }
  if (buf == NULL) {
    fprintf(stderr, "Failed to allocate memory for response\n");
    return 1;
  }
  int result = 0;
  if (strncmp(cmd, "ATTACH_LOADAGENT", 16) == 0) {
    if (strncmp(buf, "ATTACH_ACK", 10) != 0) {
      result = strncmp(buf, "ATTACH_ERR AgentInitializationException", 39) == 0
                   ? atoi(buf + 39)
                   : -1;
    }
  } else if (strncmp(cmd, "ATTACH_DIAGNOSTICS:", 19) == 0) {
    char *p = strstr(buf, "openj9_diagnostics.string_result=");
    if (p != NULL) {
      print_unescaped(p + 33);
      free(buf);
      return result;
    }
  }
  buf[off - 1] = '\n';
  fwrite(buf, 1, off, stdout);
  free(buf);
  return result;
}


x86 assembly (clang, -O0):
<read_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x2000,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12ad <read_response+0xfd>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122f <read_response+0x7f>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    140d <read_response+0x25d>
cmpq   $0x0,-0x30(%rbp)
jge    1252 <read_response+0xa2>
lea    0xde0(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    140d <read_response+0x25d>
jmp    1257 <read_response+0xa7>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1281 <read_response+0xd1>
jmp    12ad <read_response+0xfd>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jb     12a8 <read_response+0xf8>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
mov    %rsi,-0x18(%rbp)
call   1090 <realloc@plt>
mov    %rax,-0x20(%rbp)
jmp    11dc <read_response+0x2c>
cmpq   $0x0,-0x20(%rbp)
jne    12d9 <read_response+0x129>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    140d <read_response+0x25d>
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd75(%rip),%rsi
mov    $0x10,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    136a <read_response+0x1ba>
mov    -0x20(%rbp),%rdi
lea    0xd67(%rip),%rsi
mov    $0xa,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     1365 <read_response+0x1b5>
mov    -0x20(%rbp),%rdi
lea    0xd53(%rip),%rsi
mov    $0x27,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1352 <read_response+0x1a2>
mov    -0x20(%rbp),%rdi
add    $0x27,%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x44(%rbp)
jmp    135f <read_response+0x1af>
mov    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
jmp    135f <read_response+0x1af>
mov    -0x44(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    13d3 <read_response+0x223>
mov    -0x10(%rbp),%rdi
lea    0xd2f(%rip),%rsi
mov    $0x13,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    13ce <read_response+0x21e>
mov    -0x20(%rbp),%rdi
lea    0xd24(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     13c9 <read_response+0x219>
mov    -0x40(%rbp),%rdi
add    $0x21,%rdi
call   1080 <print_unescaped@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    140d <read_response+0x25d>
jmp    13ce <read_response+0x21e>
jmp    13d3 <read_response+0x223>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    0x2be6(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_response>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%r12d
mov    $0x2000,%ebp
mov    $0x2000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1225 <read_response+0x75>
mov    %rax,%r14
xor    %ebx,%ebx
jmp    11e5 <read_response+0x35>
nopl   0x0(%rax)
test   %r14,%r14
je     1225 <read_response+0x75>
lea    (%r14,%rbx,1),%rsi
mov    %rbp,%rdx
sub    %rbx,%rdx
mov    %r12d,%edi
call   1050 <read@plt>
test   %rax,%rax
je     1237 <read_response+0x87>
js     132e <read_response+0x17e>
add    %rax,%rbx
cmpb   $0x0,-0x1(%r14,%rbx,1)
je     125c <read_response+0xac>
cmp    %rbp,%rbx
jb     11e0 <read_response+0x30>
add    %rbp,%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
call   1090 <realloc@plt>
mov    %rax,%r14
jmp    11e0 <read_response+0x30>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
jmp    1247 <read_response+0x97>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdb9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdfd(%rip),%rsi
mov    %r15,%rdi
mov    $0x10,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     12bc <read_response+0x10c>
lea    0xe28(%rip),%rsi
mov    %r15,%rdi
mov    $0x13,%edx
call   1040 <strncmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1302 <read_response+0x152>
lea    0xe21(%rip),%rsi
mov    %r14,%rdi
call   10d0 <strstr@plt>
test   %rax,%rax
je     1302 <read_response+0x152>
add    $0x21,%rax
mov    %rax,%rdi
call   1080 <print_unescaped@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    1251 <read_response+0xa1>
lea    0xdae(%rip),%rsi
mov    %r14,%rdi
mov    $0xa,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1300 <read_response+0x150>
lea    0xda0(%rip),%rsi
mov    %r14,%rdi
mov    $0x27,%edx
call   1040 <strncmp@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1302 <read_response+0x152>
lea    0x27(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
jmp    1302 <read_response+0x152>
xor    %ebp,%ebp
movb   $0xa,-0x1(%r14,%rbx,1)
mov    0x2cc9(%rip),%rax
mov    (%rax),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   10c0 <fwrite@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1251 <read_response+0xa1>
lea    0xcec(%rip),%rdi
call   10a0 <perror@plt>
jmp    124c <read_response+0x9c>

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 = [40 x i8] c"Failed to allocate memory for response\0A\00"
@.str.3 = [17 x i8] c"ATTACH_LOADAGENT\00"
@.str.4 = [11 x i8] c"ATTACH_ACK\00"
@.str.5 = [40 x i8] c"ATTACH_ERR AgentInitializationException\00"
@.str.6 = [20 x i8] c"ATTACH_DIAGNOSTICS:\00"
@.str.7 = [34 x i8] c"openj9_diagnostics.string_result=\00"
@stdout = global i32 0
define i32 @read_response(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 8192, i64* %6
%12 = load i64, i64* %6
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %7
store i64 0, i64* %8
br label %14
14: ; preds = %58, %2
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %59
17: ; preds = %14
%18 = load i32, i32* %4
%19 = load i8*, i8** %7
%20 = load i64, i64* %8
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = load i64, i64* %6
%23 = load i64, i64* %8
%24 = sub i64 %22, %23
%25 = call i64 @read(i32 %18, i8* %21, i64 %24)
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %115
31: ; preds = %17
%32 = load i64, i64* %9
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %115
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i64, i64* %9
%39 = load i64, i64* %8
%40 = add i64 %39, %38
store i64 %40, i64* %8
%41 = load i8*, i8** %7
%42 = load i64, i64* %8
%43 = sub i64 %42, 1
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %37
br label %59
49: ; preds = %37
%50 = load i64, i64* %8
%51 = load i64, i64* %6
%52 = icmp uge i64 %50, %51
br i1 %52, label %53, label %58
53: ; preds = %49
%54 = load i8*, i8** %7
%55 = load i64, i64* %6
%56 = mul i64 %55, 2
store i64 %56, i64* %6
%57 = call i8* @realloc(i8* %54, i64 %56)
store i8* %57, i8** %7
br label %58
58: ; preds = %53, %49
br label %14
59: ; preds = %48, %14
%60 = load i8*, i8** %7
%61 = icmp eq i8* %60, null
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %115
65: ; preds = %59
store i32 0, i32* %10
%66 = load i8*, i8** %5
%67 = call i64 @strncmp(i8* %66, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 16)
%68 = icmp eq i64 %67, 0
br i1 %68, label %69, label %85
69: ; preds = %65
%70 = load i8*, i8** %7
%71 = call i64 @strncmp(i8* %70, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 10)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %84
73: ; preds = %69
%74 = load i8*, i8** %7
%75 = call i64 @strncmp(i8* %74, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 39)
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %81
77: ; preds = %73
%78 = load i8*, i8** %7
%79 = getelementptr inbounds i8, i8* %78, i64 39
%80 = call i32 @atoi(i8* %79)
br label %82
81: ; preds = %73
br label %82
82: ; preds = %81, %77
%83 = phi i32 [ %80, %77 ], [ -1, %81 ]
store i32 %83, i32* %10
br label %84
84: ; preds = %82, %69
br label %103
85: ; preds = %65
%86 = load i8*, i8** %5
%87 = call i64 @strncmp(i8* %86, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 19)
%88 = icmp eq i64 %87, 0
br i1 %88, label %89, label %102
89: ; preds = %85
%90 = load i8*, i8** %7
%91 = call i8* @strstr(i8* %90, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
store i8* %91, i8** %11
%92 = load i8*, i8** %11
%93 = icmp ne i8* %92, null
br i1 %93, label %94, label %101
94: ; preds = %89
%95 = load i8*, i8** %11
%96 = getelementptr inbounds i8, i8* %95, i64 33
%97 = call i32 @print_unescaped(i8* %96)
%98 = load i8*, i8** %7
%99 = call i32 @free(i8* %98)
%100 = load i32, i32* %10
store i32 %100, i32* %3
br label %115
101: ; preds = %89
br label %102
102: ; preds = %101, %85
br label %103
103: ; preds = %102, %84
%104 = load i8*, i8** %7
%105 = load i64, i64* %8
%106 = sub i64 %105, 1
%107 = getelementptr inbounds i8, i8* %104, i64 %106
store i8 10, i8* %107
%108 = load i8*, i8** %7
%109 = load i64, i64* %8
%110 = load i32, i32* @stdout
%111 = call i32 @fwrite(i8* %108, i32 1, i64 %109, i32 %110)
%112 = load i8*, i8** %7
%113 = call i32 @free(i8* %112)
%114 = load i32, i32* %10
store i32 %114, i32* %3
br label %115
115: ; preds = %103, %94, %62, %34, %28
%116 = load i32, i32* %3
ret i32 %116
}

LLVM IR (-O2):
@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 = [40 x i8] c"Failed to allocate memory for response\0A\00"
@.str.3 = [17 x i8] c"ATTACH_LOADAGENT\00"
@.str.4 = [11 x i8] c"ATTACH_ACK\00"
@.str.5 = [40 x i8] c"ATTACH_ERR AgentInitializationException\00"
@.str.6 = [20 x i8] c"ATTACH_DIAGNOSTICS:\00"
@.str.7 = [34 x i8] c"openj9_diagnostics.string_result=\00"
@stdout = global i32 0
define i32 @read_response(i32 %0, i8* %1) {
%3 = tail call dereferenceable_or_null(8192) i8* @malloc(i64 8192)
%4 = icmp eq i8* %3, null
br i1 %4, label %35, label %5
5: ; preds = %2, %31
%6 = phi i64 [ %33, %31 ], [ 8192, %2 ]
%7 = phi i64 [ %21, %31 ], [ 0, %2 ]
%8 = phi i8* [ %32, %31 ], [ %3, %2 ]
%9 = getelementptr inbounds i8, i8* %8, i64 %7
%10 = sub i64 %6, %7
%11 = tail call i64 @read(i32 %0, i8* %9, i64 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %66
16: ; preds = %5
%17 = icmp slt i64 %11, 0
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %66
20: ; preds = %16
%21 = add i64 %11, %7
%22 = add i64 %21, -1
%23 = getelementptr inbounds i8, i8* %8, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %38, label %26
26: ; preds = %20
%27 = icmp ult i64 %21, %6
br i1 %27, label %31, label %28
28: ; preds = %26
%29 = shl i64 %6, 1
%30 = tail call i8* @realloc(i8* %8, i64 %29)
br label %31
31: ; preds = %26, %28
%32 = phi i8* [ %30, %28 ], [ %8, %26 ]
%33 = phi i64 [ %29, %28 ], [ %6, %26 ]
%34 = icmp eq i8* %32, null
br i1 %34, label %35, label %5
35: ; preds = %31, %2
%36 = load i32, i32* @stderr
%37 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %66
38: ; preds = %20
%39 = getelementptr inbounds i8, i8* %8, i64 %22
%40 = tail call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 16)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = tail call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 10)
%44 = icmp eq i64 %43, 0
br i1 %44, label %61, label %45
45: ; preds = %42
%46 = tail call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i32 39)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %61
48: ; preds = %45
%49 = getelementptr inbounds i8, i8* %8, i64 39
%50 = tail call i32 @atoi(i8* %49)
br label %61
51: ; preds = %38
%52 = tail call i64 @strncmp(i8* %1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i32 19)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %51
%55 = tail call i8* @strstr(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%56 = icmp eq i8* %55, null
br i1 %56, label %61, label %57
57: ; preds = %54
%58 = getelementptr inbounds i8, i8* %55, i64 33
%59 = tail call i32 @print_unescaped(i8* %58)
%60 = tail call i32 @free(i8* %8)
br label %66
61: ; preds = %54, %48, %45, %51, %42
%62 = phi i32 [ 0, %42 ], [ 0, %51 ], [ %50, %48 ], [ -1, %45 ], [ 0, %54 ]
store i8 10, i8* %39
%63 = load i32, i32* @stdout
%64 = tail call i32 @fwrite(i8* %8, i32 1, i64 %21, i32 %63)
%65 = tail call i32 @free(i8* %8)
br label %66
66: ; preds = %18, %13, %57, %61, %35
%67 = phi i32 [ 1, %35 ], [ %62, %61 ], [ 0, %57 ], [ 1, %13 ], [ 1, %18 ]
ret i32 %67
}