#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 */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *xrealloc(char *, int);
int transport_read(int sockfd, char **buff) {
  int n, sum = 0;
  char tmp[1024];
  assert(*buff == NULL);
  while (1) {
    n = read(sockfd, tmp, 1024);
    if (n < 0)
      return -1;
    if (n == 0)
      break;
    sum += n;
    *buff = xrealloc(*buff, sum + 1);
    strncpy(*buff + sum - n, tmp, n);
  }
  (*buff)[sum] = '\0';
  return sum;
}


x86 assembly (clang, -O0):
<transport_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    119b <transport_read+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <transport_read+0xd2>
cmpl   $0x0,-0x14(%rbp)
jne    11aa <transport_read+0x6a>
jmp    11fd <transport_read+0xbd>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%esi
add    $0x1,%esi
call   1040 <xrealloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x18(%rbp),%rax
add    %rax,%rdi
movslq -0x14(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
lea    -0x420(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
jmp    116e <transport_read+0x2e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transport_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r12
mov    %edi,%r14d
xor    %edi,%edi
cmpq   $0x0,(%rsi)
sete   %dil
call   1060 <assert@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11c7 <transport_read+0x87>
mov    %eax,%ebp
xor    %eax,%eax
mov    %rsp,%r15
nop
test   %ebp,%ebp
je     11ce <transport_read+0x8e>
lea    (%rax,%rbp,1),%ebx
mov    (%r12),%rdi
lea    (%rax,%rbp,1),%esi
add    $0x1,%esi
call   1040 <xrealloc@plt>
mov    %rax,(%r12)
movslq %ebx,%rdi
add    %rax,%rdi
mov    %ebp,%eax
sub    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %ebx,%eax
test   %ebp,%ebp
jns    1180 <transport_read+0x40>
mov    $0xffffffff,%eax
jmp    11d9 <transport_read+0x99>
mov    (%r12),%rcx
movslq %eax,%rdx
movb   $0x0,(%rcx,%rdx,1)
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @transport_read(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [1024 x i8]
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%9 = load i8**, i8*** %5
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
br label %14
14: ; preds = %2, %25
%15 = load i32, i32* %4
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 1024)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %3
br label %54
21: ; preds = %14
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %47
25: ; preds = %21
%26 = load i32, i32* %6
%27 = load i32, i32* %7
%28 = add nsw i32 %27, %26
store i32 %28, i32* %7
%29 = load i8**, i8*** %5
%30 = load i8*, i8** %29
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
%33 = call i8* @xrealloc(i8* %30, i32 %32)
%34 = load i8**, i8*** %5
store i8* %33, i8** %34
%35 = load i8**, i8*** %5
%36 = load i8*, i8** %35
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = sub i64 0, %41
%43 = getelementptr inbounds i8, i8* %39, i64 %42
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%45 = load i32, i32* %6
%46 = call i32 @strncpy(i8* %43, i8* %44, i32 %45)
br label %14
47: ; preds = %24
%48 = load i8**, i8*** %5
%49 = load i8*, i8** %48
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8 0, i8* %52
%53 = load i32, i32* %7
store i32 %53, i32* %3
br label %54
54: ; preds = %47, %20
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
define i32 @transport_read(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i8*, i8** %1
%6 = icmp eq i8* %5, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = call i32 @read(i32 %0, i8* %4, i32 1024)
%10 = icmp slt i32 %9, 0
br i1 %10, label %32, label %11
11: ; preds = %2, %15
%12 = phi i32 [ %26, %15 ], [ %9, %2 ]
%13 = phi i32 [ %16, %15 ], [ 0, %2 ]
%14 = icmp eq i32 %12, 0
br i1 %14, label %28, label %15
15: ; preds = %11
%16 = add nsw i32 %12, %13
%17 = load i8*, i8** %1
%18 = add nsw i32 %16, 1
%19 = call i8* @xrealloc(i8* %17, i32 %18)
store i8* %19, i8** %1
%20 = sext i32 %16 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
%22 = zext i32 %12 to i64
%23 = sub nsw i64 0, %22
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = call i32 @strncpy(i8* %24, i8* %4, i32 %12)
%26 = call i32 @read(i32 %0, i8* %4, i32 1024)
%27 = icmp slt i32 %26, 0
br i1 %27, label %32, label %11
28: ; preds = %11
%29 = load i8*, i8** %1
%30 = sext i32 %13 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %15, %2, %28
%33 = phi i32 [ %13, %28 ], [ -1, %2 ], [ -1, %15 ]
ret i32 %33
}