#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 */

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ calloc(int, int);
scalar_t__ errno;
int /*<<< orphan*/ memcpy(unsigned char *, char *, int);
scalar_t__ realloc(unsigned char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *strstr(char *, char *);
int recv_comm_until(int socket, unsigned char **buffer, char *needle) {
  int total_bytes_read;
  int bytes_read;
  int buffer_length = 1024;
  char temp_buffer[1024];
  total_bytes_read = 0;
  *buffer = (unsigned char *)calloc(sizeof(unsigned char), buffer_length);
  while (strstr((char *)*buffer, needle) == NULL) {
    bytes_read = recv(socket, temp_buffer, 1024, 0);
    if (bytes_read < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        return -2;
      }
    } else if (bytes_read == 0) {
      return -1;
    }
    if ((total_bytes_read + bytes_read) > buffer_length) {
      *buffer = (unsigned char *)realloc(*buffer,
                                         (total_bytes_read + bytes_read) * 2);
    }
    memcpy(&(*buffer)[total_bytes_read], temp_buffer, bytes_read);
    total_bytes_read += bytes_read;
  }
  return 0;
}


x86 assembly (clang, -O0):
<recv_comm_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x400,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x24(%rbp),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
jne    1267 <recv_comm_until+0x117>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11f3 <recv_comm_until+0xa3>
mov    0x2e11(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <recv_comm_until+0x97>
jmp    118b <recv_comm_until+0x3b>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126e <recv_comm_until+0x11e>
cmpl   $0x0,-0x20(%rbp)
jne    1209 <recv_comm_until+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <recv_comm_until+0x11e>
jmp    120e <recv_comm_until+0xbe>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    123c <recv_comm_until+0xec>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
add    -0x20(%rbp),%esi
shl    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
lea    -0x430(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    118b <recv_comm_until+0x3b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_comm_until>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r12d
mov    $0x1,%edi
mov    $0x400,%esi
call   1040 <calloc@plt>
mov    %rax,(%rbx)
xor    %eax,%eax
mov    %rax,(%rsp)
lea    0x10(%rsp),%r15
mov    0x2e4a(%rip),%r13
xchg   %ax,%ax
mov    (%rbx),%rdi
mov    %r14,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
jne    120d <recv_comm_until+0xbd>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jns    11c8 <recv_comm_until+0x78>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1190 <recv_comm_until+0x40>
jmp    1211 <recv_comm_until+0xc1>
je     1228 <recv_comm_until+0xd8>
mov    %eax,%ebp
mov    (%rsp),%rax
lea    (%rax,%rbp,1),%ecx
mov    (%rbx),%rax
cmp    $0x401,%ecx
mov    %rcx,0x8(%rsp)
jl     11f1 <recv_comm_until+0xa1>
lea    (%rcx,%rcx,1),%esi
mov    %rax,%rdi
call   1060 <realloc@plt>
mov    %rax,(%rbx)
movslq (%rsp),%rdi
add    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
jmp    1190 <recv_comm_until+0x40>
xor    %eax,%eax
jmp    1216 <recv_comm_until+0xc6>
mov    $0xfffffffe,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1216 <recv_comm_until+0xc6>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @recv_comm_until(i32 %0, i8** %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i32 1024, i32* %10
store i32 0, i32* %8
%12 = load i32, i32* %10
%13 = call i64 @calloc(i32 1, i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i8**, i8*** %6
store i8* %14, i8** %15
br label %16
16: ; preds = %55, %32, %3
%17 = load i8**, i8*** %6
%18 = load i8*, i8** %17
%19 = load i8*, i8** %7
%20 = call i32* @strstr(i8* %18, i8* %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %67
22: ; preds = %16
%23 = load i32, i32* %5
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%25 = call i32 @recv(i32 %23, i8* %24, i32 1024, i32 0)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %16
33: ; preds = %28
store i32 -2, i32* %4
br label %68
34: ; preds = %22
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 -1, i32* %4
br label %68
38: ; preds = %34
br label %39
39: ; preds = %38
%40 = load i32, i32* %8
%41 = load i32, i32* %9
%42 = add nsw i32 %40, %41
%43 = load i32, i32* %10
%44 = icmp sgt i32 %42, %43
br i1 %44, label %45, label %55
45: ; preds = %39
%46 = load i8**, i8*** %6
%47 = load i8*, i8** %46
%48 = load i32, i32* %8
%49 = load i32, i32* %9
%50 = add nsw i32 %48, %49
%51 = mul nsw i32 %50, 2
%52 = call i64 @realloc(i8* %47, i32 %51)
%53 = inttoptr i64 %52 to i8*
%54 = load i8**, i8*** %6
store i8* %53, i8** %54
br label %55
55: ; preds = %45, %39
%56 = load i8**, i8*** %6
%57 = load i8*, i8** %56
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%62 = load i32, i32* %9
%63 = call i32 @memcpy(i8* %60, i8* %61, i32 %62)
%64 = load i32, i32* %9
%65 = load i32, i32* %8
%66 = add nsw i32 %65, %64
store i32 %66, i32* %8
br label %16
67: ; preds = %16
store i32 0, i32* %4
br label %68
68: ; preds = %67, %37, %33
%69 = load i32, i32* %4
ret i32 %69
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @recv_comm_until(i32 %0, i8** nocapture %1, i8* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = tail call i64 @calloc(i32 1, i32 1024)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %1
br label %8
8: ; preds = %31, %3
%9 = phi i32 [ %24, %31 ], [ 0, %3 ]
br label %10
10: ; preds = %8, %17
%11 = load i8*, i8** %1
%12 = call i32* @strstr(i8* dereferenceable(1) %11, i8* dereferenceable(1) %2)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %36
14: ; preds = %10
%15 = call i32 @recv(i32 %0, i8* %5, i32 1024, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %10, label %36
21: ; preds = %14
%22 = icmp eq i32 %15, 0
br i1 %22, label %36, label %23
23: ; preds = %21
%24 = add nsw i32 %15, %9
%25 = icmp sgt i32 %24, 1024
%26 = load i8*, i8** %1
br i1 %25, label %27, label %31
27: ; preds = %23
%28 = shl nuw nsw i32 %24, 1
%29 = call i64 @realloc(i8* %26, i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %1
br label %31
31: ; preds = %23, %27
%32 = phi i8* [ %30, %27 ], [ %26, %23 ]
%33 = sext i32 %9 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
%35 = call i32 @memcpy(i8* %34, i8* %5, i32 %15)
br label %8
36: ; preds = %21, %10, %17
%37 = phi i32 [ 0, %10 ], [ -2, %17 ], [ -1, %21 ]
ret i32 %37
}