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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
char *readMessage(int sock) {
  char buf[256];
  int size = 512;
  int copied = 0;
  char *big_buf = malloc(sizeof(char) * size);
  if (buf == NULL) {
    perror("malloc");
    return NULL;
  }
  int recvd;
  do {
    recvd = recv(sock, buf, sizeof(buf), 0);
    if (recvd == -1) {
      perror("recv");
      free(big_buf);
      return NULL;
    }
    if (size - copied - 1 < recvd) {
      size *= 2;
      char *tmp = malloc(sizeof(char) * size);
      if (tmp == NULL) {
        perror("malloc");
        free(big_buf);
        return NULL;
      }
      memcpy(tmp, big_buf, size / 2);
      free(big_buf);
      big_buf = tmp;
    }
    memcpy(big_buf + sizeof(char) * copied, buf, recvd);
    copied += recvd;
  } while (recvd >= 256);
  *(big_buf + copied) = '\0';
  return big_buf;
}


x86 assembly (clang, -O0):
<readMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x200,-0x114(%rbp)
movl   $0x0,-0x118(%rbp)
movslq -0x114(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x120(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    11d9 <readMessage+0x89>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    -0x120(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f2 <readMessage+0x1a2>
mov    -0x114(%rbp),%eax
sub    -0x118(%rbp),%eax
sub    $0x1,%eax
cmp    -0x124(%rbp),%eax
jge    128c <readMessage+0x13c>
mov    -0x114(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x114(%rbp)
movslq -0x114(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
jne    124f <readMessage+0xff>
lea    0xdd4(%rip),%rdi
call   1070 <perror@plt>
mov    -0x120(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f2 <readMessage+0x1a2>
mov    -0x130(%rbp),%rdi
mov    -0x120(%rbp),%rsi
mov    -0x114(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x120(%rbp),%rdi
call   1030 <free@plt>
mov    -0x130(%rbp),%rax
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rdi
movslq -0x118(%rbp),%rax
shl    $0x0,%rax
add    %rax,%rdi
lea    -0x110(%rbp),%rsi
mov    -0x124(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x124(%rbp),%eax
add    -0x118(%rbp),%eax
mov    %eax,-0x118(%rbp)
cmpl   $0x100,-0x124(%rbp)
jge    118b <readMessage+0x3b>
mov    -0x120(%rbp),%rax
movslq -0x118(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x120(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readMessage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r12d
mov    $0x200,%r13d
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
xor    %r15d,%r15d
lea    0x10(%rsp),%rbp
mov    %r12d,0xc(%rsp)
jmp    11ab <readMessage+0x5b>
cs nopw 0x0(%rax,%rax,1)
movslq %r15d,%rdi
add    %r14,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
add    %ebx,%r15d
cmp    $0xff,%ebx
jle    120c <readMessage+0xbc>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
cmp    $0xffffffff,%eax
je     1229 <readMessage+0xd9>
mov    %eax,%ebx
mov    %r15d,%eax
not    %eax
add    %r13d,%eax
cmp    %ebx,%eax
jge    1190 <readMessage+0x40>
lea    0x0(%r13,%r13,1),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1232 <readMessage+0xe2>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%r14
mov    %ebp,%r13d
mov    0xc(%rsp),%r12d
lea    0x10(%rsp),%rbp
jmp    1190 <readMessage+0x40>
movslq %r15d,%rax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd0(%rip),%rdi
jmp    1239 <readMessage+0xe9>
lea    0xdcc(%rip),%rdi
call   1070 <perror@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    1214 <readMessage+0xc4>

LLVM IR (-O0):
@.str = [5 x i8] c"recv\00"
@.str.1 = [7 x i8] c"malloc\00"
define i8* @readMessage(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %3
store i32 512, i32* %5
store i32 0, i32* %6
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = mul i64 1, %11
%13 = trunc i64 %12 to i32
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %7
br label %15
15: ; preds = %67, %1
%16 = load i32, i32* %3
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%18 = call i32 @recv(i32 %16, i8* %17, i32 256, i32 0)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%23 = load i8*, i8** %7
%24 = call i32 @free(i8* %23)
store i8* null, i8** %2
br label %76
25: ; preds = %15
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = sub nsw i32 %26, %27
%29 = sub nsw i32 %28, 1
%30 = load i32, i32* %8
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %55
32: ; preds = %25
%33 = load i32, i32* %5
%34 = mul nsw i32 %33, 2
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = sext i32 %35 to i64
%37 = mul i64 1, %36
%38 = trunc i64 %37 to i32
%39 = call i8* @malloc(i32 %38)
store i8* %39, i8** %9
%40 = load i8*, i8** %9
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %46
42: ; preds = %32
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%44 = load i8*, i8** %7
%45 = call i32 @free(i8* %44)
store i8* null, i8** %2
br label %76
46: ; preds = %32
%47 = load i8*, i8** %9
%48 = load i8*, i8** %7
%49 = load i32, i32* %5
%50 = sdiv i32 %49, 2
%51 = call i32 @memcpy(i8* %47, i8* %48, i32 %50)
%52 = load i8*, i8** %7
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %9
store i8* %54, i8** %7
br label %55
55: ; preds = %46, %25
%56 = load i8*, i8** %7
%57 = load i32, i32* %6
%58 = sext i32 %57 to i64
%59 = mul i64 1, %58
%60 = getelementptr inbounds i8, i8* %56, i64 %59
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%62 = load i32, i32* %8
%63 = call i32 @memcpy(i8* %60, i8* %61, i32 %62)
%64 = load i32, i32* %8
%65 = load i32, i32* %6
%66 = add nsw i32 %65, %64
store i32 %66, i32* %6
br label %67
67: ; preds = %55
%68 = load i32, i32* %8
%69 = icmp sge i32 %68, 256
br i1 %69, label %15, label %70
70: ; preds = %67
%71 = load i8*, i8** %7
%72 = load i32, i32* %6
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
store i8 0, i8* %74
%75 = load i8*, i8** %7
store i8* %75, i8** %2
br label %76
76: ; preds = %70, %42, %21
%77 = load i8*, i8** %2
ret i8* %77
}

LLVM IR (-O2):
@.str = [5 x i8] c"recv\00"
@.str.1 = [7 x i8] c"malloc\00"
define i8* @readMessage(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
br label %5
5: ; preds = %28, %1
%6 = phi i32 [ 0, %1 ], [ %34, %28 ]
%7 = phi i8* [ %4, %1 ], [ %29, %28 ]
%8 = phi i32 [ 512, %1 ], [ %30, %28 ]
%9 = call i32 @recv(i32 %0, i8* %3, i32 256, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @free(i8* %7)
br label %39
14: ; preds = %5
%15 = xor i32 %6, -1
%16 = add i32 %8, %15
%17 = icmp slt i32 %16, %9
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = shl nsw i32 %8, 1
%20 = call i8* @malloc(i32 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @free(i8* %7)
br label %39
25: ; preds = %18
%26 = call i32 @memcpy(i8* %20, i8* %7, i32 %8)
%27 = call i32 @free(i8* %7)
br label %28
28: ; preds = %25, %14
%29 = phi i8* [ %20, %25 ], [ %7, %14 ]
%30 = phi i32 [ %19, %25 ], [ %8, %14 ]
%31 = sext i32 %6 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = call i32 @memcpy(i8* %32, i8* %3, i32 %9)
%34 = add nsw i32 %9, %6
%35 = icmp sgt i32 %9, 255
br i1 %35, label %5, label %36
36: ; preds = %28
%37 = sext i32 %34 to i64
%38 = getelementptr inbounds i8, i8* %29, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %22, %36, %11
%40 = phi i8* [ null, %11 ], [ %29, %36 ], [ null, %22 ]
ret i8* %40
}