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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ free(char *);
char *memrchr(char *, char, size_t);
char *realloc(char *, size_t);
size_t recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strncmp(char *, char *, int);
void _get_response(int fd, char **bufp, size_t *size) {
  size_t len = 0;
  char buf[BUFSIZ];
  for (;;) {
    ssize_t count;
    if (len + BUFSIZ > *size) {
      *size *= 2;
      char *new_buf = realloc(*bufp, *size);
      if (!new_buf)
        goto error;
      *bufp = new_buf;
    }
    count = recv(fd, buf, BUFSIZ - 1, 0);
    if (count < 0)
      goto error;
    buf[count] = '\0';
    strcpy(*bufp + len, buf);
    len += count;
    if (count > 1) {
      char *s = memrchr(buf, '\n', count - 1);
      s = s ? s + 1 : buf;
      if (strncmp(s, "OK", 2) == 0)
        return;
    }
  }
error:
  free(*bufp);
  *bufp = NULL;
}


x86 assembly (clang, -O0):
<_get_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e2e(%rip),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jbe    1205 <_get_response+0xa5>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
shl    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1070 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11fa <_get_response+0x9a>
jmp    12e6 <_get_response+0x186>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x58(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jae    1233 <_get_response+0xd3>
jmp    12e6 <_get_response+0x186>
mov    -0x58(%rbp),%rsi
mov    -0x38(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    -0x20(%rbp),%rdi
call   1060 <strcpy@plt>
mov    -0x38(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x1,-0x38(%rbp)
jbe    12e1 <_get_response+0x181>
mov    -0x58(%rbp),%rdi
mov    -0x38(%rbp),%rdx
sub    $0x1,%rdx
mov    $0xa,%esi
call   1080 <memrchr@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     129c <_get_response+0x13c>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
jmp    12a9 <_get_response+0x149>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    12a9 <_get_response+0x149>
mov    -0x60(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
lea    0xd44(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    12dc <_get_response+0x17c>
movl   $0x1,-0x4c(%rbp)
jmp    1304 <_get_response+0x1a4>
jmp    12e1 <_get_response+0x181>
jmp    11a7 <_get_response+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_get_response>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %edi,-0x2c(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r15d,%r15d
jmp    11ac <_get_response+0x4c>
nopl   0x0(%rax)
add    %r13,%r15
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
movslq %edx,%rax
add    %r15,%rax
mov    (%r14),%rsi
cmp    %rsi,%rax
jbe    11db <_get_response+0x7b>
add    %rsi,%rsi
mov    %rsi,(%r14)
mov    (%r12),%rdi
call   1070 <realloc@plt>
test   %rax,%rax
je     123f <_get_response+0xdf>
mov    %rax,(%r12)
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
add    $0xffffffff,%edx
mov    -0x2c(%rbp),%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %rax,%r13
movb   $0x0,(%rbx,%rax,1)
mov    (%r12),%rdi
add    %r15,%rdi
mov    %rbx,%rsi
call   1060 <strcpy@plt>
cmp    $0x2,%r13
jb     11a0 <_get_response+0x40>
lea    -0x1(%r13),%rdx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1080 <memrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %rbx,%rdi
lea    0xdd6(%rip),%rsi
mov    $0x2,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    11a0 <_get_response+0x40>
jmp    1250 <_get_response+0xf0>
mov    (%r12),%rdi
call   1030 <free@plt>
movq   $0x0,(%r12)
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [3 x i8] c"OK\00"
define void @_get_response(i32 %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
store i64 0, i64* %7
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
br label %18
18: ; preds = %79, %3
%19 = load i64, i64* %7
%20 = load i32, i32* @BUFSIZ
%21 = sext i32 %20 to i64
%22 = add i64 %19, %21
%23 = load i64*, i64** %6
%24 = load i64, i64* %23
%25 = icmp ugt i64 %22, %24
br i1 %25, label %26, label %41
26: ; preds = %18
%27 = load i64*, i64** %6
%28 = load i64, i64* %27
%29 = mul i64 %28, 2
store i64 %29, i64* %27
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = load i64*, i64** %6
%33 = load i64, i64* %32
%34 = call i8* @realloc(i8* %31, i64 %33)
store i8* %34, i8** %11
%35 = load i8*, i8** %11
%36 = icmp ne i8* %35, null
br i1 %36, label %38, label %37
37: ; preds = %26
br label %80
38: ; preds = %26
%39 = load i8*, i8** %11
%40 = load i8**, i8*** %5
store i8* %39, i8** %40
br label %41
41: ; preds = %38, %18
%42 = load i32, i32* %4
%43 = load i32, i32* @BUFSIZ
%44 = sub nsw i32 %43, 1
%45 = call i64 @recv(i32 %42, i8* %17, i32 %44, i32 0)
store i64 %45, i64* %10
%46 = load i64, i64* %10
%47 = icmp ult i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %41
br label %80
49: ; preds = %41
%50 = load i64, i64* %10
%51 = getelementptr inbounds i8, i8* %17, i64 %50
store i8 0, i8* %51
%52 = load i8**, i8*** %5
%53 = load i8*, i8** %52
%54 = load i64, i64* %7
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = call i32 @strcpy(i8* %55, i8* %17)
%57 = load i64, i64* %10
%58 = load i64, i64* %7
%59 = add i64 %58, %57
store i64 %59, i64* %7
%60 = load i64, i64* %10
%61 = icmp ugt i64 %60, 1
br i1 %61, label %62, label %79
62: ; preds = %49
%63 = load i64, i64* %10
%64 = sub i64 %63, 1
%65 = call i8* @memrchr(i8* %17, i8 signext 10, i64 %64)
store i8* %65, i8** %12
%66 = load i8*, i8** %12
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = load i8*, i8** %12
%70 = getelementptr inbounds i8, i8* %69, i64 1
br label %72
71: ; preds = %62
br label %72
72: ; preds = %71, %68
%73 = phi i8* [ %70, %68 ], [ %17, %71 ]
store i8* %73, i8** %12
%74 = load i8*, i8** %12
%75 = call i64 @strncmp(i8* %74, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %72
store i32 1, i32* %13
br label %85
78: ; preds = %72
br label %79
79: ; preds = %78, %49
br label %18
80: ; preds = %48, %37
%81 = load i8**, i8*** %5
%82 = load i8*, i8** %81
%83 = call i32 @free(i8* %82)
%84 = load i8**, i8*** %5
store i8* null, i8** %84
store i32 0, i32* %13
br label %85
85: ; preds = %80, %77
%86 = load i8*, i8** %8
%87 = load i32, i32* %13
switch i32 %87, label %89 [
i32 0, label %88
i32 1, label %88
]
88: ; preds = %85, %85
ret void
89: ; preds = %85
unreachable
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [3 x i8] c"OK\00"
define void @_get_response(i32 %0, i8** nocapture %1, i64* nocapture %2) {
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
br label %7
7: ; preds = %39, %3
%8 = phi i32 [ %4, %3 ], [ %40, %39 ]
%9 = phi i64 [ 0, %3 ], [ %29, %39 ]
%10 = sext i32 %8 to i64
%11 = add i64 %9, %10
%12 = load i64, i64* %2
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %21
14: ; preds = %7
%15 = shl i64 %12, 1
store i64 %15, i64* %2
%16 = load i8*, i8** %1
%17 = call i8* @realloc(i8* %16, i64 %15)
%18 = icmp eq i8* %17, null
br i1 %18, label %41, label %19
19: ; preds = %14
store i8* %17, i8** %1
%20 = load i32, i32* @BUFSIZ
br label %21
21: ; preds = %19, %7
%22 = phi i32 [ %20, %19 ], [ %8, %7 ]
%23 = add nsw i32 %22, -1
%24 = call i64 @recv(i32 %0, i8* %6, i32 %23, i32 0)
%25 = getelementptr inbounds i8, i8* %6, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %1
%27 = getelementptr inbounds i8, i8* %26, i64 %9
%28 = call i32 @strcpy(i8* %27, i8* %6)
%29 = add i64 %24, %9
%30 = icmp ugt i64 %24, 1
br i1 %30, label %31, label %39
31: ; preds = %21
%32 = add i64 %24, -1
%33 = call i8* @memrchr(i8* %6, i8 signext 10, i64 %32)
%34 = icmp eq i8* %33, null
%35 = getelementptr inbounds i8, i8* %33, i64 1
%36 = select i1 %34, i8* %6, i8* %35
%37 = call i64 @strncmp(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %21, %31
%40 = load i32, i32* @BUFSIZ
br label %7
41: ; preds = %14
%42 = load i8*, i8** %1
%43 = call i32 @free(i8* %42)
store i8* null, i8** %1
br label %44
44: ; preds = %31, %41
ret void
}