#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__ malloc(int);
int read(int, unsigned char *, int);
scalar_t__ realloc(void *, int);
unsigned char *__dynamic_read_socket__(int fd) {
  int readn;
  unsigned char *body;
  int malloc_size = 1024;
  int block_size = 1024;
  int effiv_addr = 0;
  body = (unsigned char *)malloc(malloc_size);
  while (readn = read(fd, body + effiv_addr, block_size)) {
    effiv_addr += readn;
    malloc_size += block_size;
    body = (unsigned char *)realloc((void *)body, malloc_size);
  }
  return body;
}


x86 assembly (clang, -O0):
<__dynamic_read_socket__>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x400,-0x14(%rbp)
movl   $0x400,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     11a5 <__dynamic_read_socket__+0x75>
mov    -0x8(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    115c <__dynamic_read_socket__+0x2c>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__dynamic_read_socket__>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    $0x400,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %r14d,%edi
mov    %rax,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
test   %eax,%eax
je     119c <__dynamic_read_socket__+0x6c>
xor    %ebx,%ebx
mov    $0x800,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    %eax,%ebx
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
mov    %rax,%r15
movslq %ebx,%rsi
add    %rax,%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1030 <read@plt>
add    $0x400,%ebp
test   %eax,%eax
jne    1170 <__dynamic_read_socket__+0x40>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @__dynamic_read_socket__(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
store i32 1024, i32* %5
store i32 1024, i32* %6
store i32 0, i32* %7
%8 = load i32, i32* %5
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %4
br label %11
11: ; preds = %20, %1
%12 = load i32, i32* %2
%13 = load i8*, i8** %4
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i32, i32* %6
%18 = call i32 @read(i32 %12, i8* %16, i32 %17)
store i32 %18, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %11
%21 = load i32, i32* %3
%22 = load i32, i32* %7
%23 = add nsw i32 %22, %21
store i32 %23, i32* %7
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = add nsw i32 %25, %24
store i32 %26, i32* %5
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = call i64 @realloc(i8* %27, i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %4
br label %11
31: ; preds = %11
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
define i8* @__dynamic_read_socket__(i32 %0) {
%2 = tail call i64 @malloc(i32 1024)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i32 @read(i32 %0, i8* %3, i32 1024)
%5 = icmp eq i32 %4, 0
br i1 %5, label %21, label %6
6: ; preds = %1, %6
%7 = phi i32 [ %17, %6 ], [ %4, %1 ]
%8 = phi i8* [ %14, %6 ], [ %3, %1 ]
%9 = phi i32 [ %11, %6 ], [ 0, %1 ]
%10 = phi i32 [ %12, %6 ], [ 1024, %1 ]
%11 = add nsw i32 %7, %9
%12 = add nuw nsw i32 %10, 1024
%13 = tail call i64 @realloc(i8* %8, i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = sext i32 %11 to i64
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = tail call i32 @read(i32 %0, i8* %16, i32 1024)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %6
19: ; preds = %6
%20 = inttoptr i64 %13 to i8*
br label %21
21: ; preds = %19, %1
%22 = phi i8* [ %3, %1 ], [ %20, %19 ]
ret i8* %22
}