#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);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int ntohl(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *conn_read(int conn) {
  int readlen = 0, result = 0;
  char lenchars[sizeof(int)];
  unsigned int count = 0;
  while (count < sizeof(int)) {
    result = read(conn, &lenchars[count], sizeof(int) - count);
    if (result < 0) {
      perror("Failure in conn_read");
      return NULL;
    }
    count += result;
  }
  int strlen = ntohl(*((int *)lenchars));
  printf("Should read %d bytes", strlen);
  printf("\n");
  ;
  assert(strlen < (1024 * 1024 * 1024));
  char *buf = (char *)malloc(strlen + 1);
  assert(buf != NULL);
  char *cur = buf;
  while (readlen < strlen) {
    result = read(conn, cur, strlen - readlen);
    if (result < 0) {
      printf("Error reading from socket: %d", result);
      printf("\n");
      ;
      free(buf);
      return NULL;
    }
    cur += result;
    readlen += result;
  }
  buf[readlen] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<conn_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    $0x4,%rax
jae    11f2 <conn_read+0x82>
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%eax
lea    -0x18(%rbp),%rsi
add    %rax,%rsi
mov    -0x1c(%rbp),%eax
mov    %eax,%ecx
mov    $0x4,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e4 <conn_read+0x74>
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <conn_read+0x17c>
mov    -0x14(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1190 <conn_read+0x20>
mov    -0x18(%rbp),%edi
call   1090 <ntohl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x40000000,-0x20(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12d8 <conn_read+0x168>
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
sub    -0x10(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12b9 <conn_read+0x149>
mov    -0x14(%rbp),%esi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <conn_read+0x17c>
mov    -0x14(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1259 <conn_read+0xe9>
mov    -0x28(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conn_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r12d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
lea    (%rsp,%rax,1),%rsi
add    $0xc,%rsi
mov    $0x4,%edx
sub    %ebx,%edx
mov    %r12d,%edi
call   1060 <read@plt>
test   %eax,%eax
js     128f <conn_read+0x10f>
add    %eax,%ebx
cmp    $0x3,%ebx
jbe    11a0 <conn_read+0x20>
mov    0xc(%rsp),%edi
call   10a0 <ntohl@plt>
mov    %eax,%r15d
lea    0xe3a(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
xor    %edi,%edi
cmp    $0x40000000,%r15d
setl   %dil
call   1070 <assert@plt>
lea    0x1(%r15),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
test   %r15d,%r15d
jle    1253 <conn_read+0xd3>
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edx
sub    %ebp,%edx
mov    %r12d,%edi
mov    %rbx,%rsi
call   1060 <read@plt>
test   %eax,%eax
js     125a <conn_read+0xda>
mov    %eax,%ecx
add    %rcx,%rbx
add    %eax,%ebp
cmp    %ebp,%r15d
jg     1230 <conn_read+0xb0>
movslq %ebp,%rbp
movb   $0x0,(%r14,%rbp,1)
jmp    127f <conn_read+0xff>
lea    0xdc9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
jmp    127c <conn_read+0xfc>

LLVM IR (-O0):
@.str = [21 x i8] c"Failure in conn_read\00"
@.str.1 = [21 x i8] c"Should read %d bytes\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [30 x i8] c"Error reading from socket: %d\00"
define i8* @conn_read(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %7
br label %11
11: ; preds = %29, %1
%12 = load i32, i32* %7
%13 = zext i32 %12 to i64
%14 = icmp ult i64 %13, 4
br i1 %14, label %15, label %33
15: ; preds = %11
%16 = load i32, i32* %3
%17 = load i32, i32* %7
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 %18
%20 = load i32, i32* %7
%21 = zext i32 %20 to i64
%22 = sub i64 4, %21
%23 = trunc i64 %22 to i32
%24 = call i32 @read(i32 %16, i8* %19, i32 %23)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %87
29: ; preds = %15
%30 = load i32, i32* %5
%31 = load i32, i32* %7
%32 = add i32 %31, %30
store i32 %32, i32* %7
br label %11
33: ; preds = %11
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%35 = bitcast i8* %34 to i32*
%36 = load i32, i32* %35
%37 = call i32 @ntohl(i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %8
%42 = icmp slt i32 %41, 1073741824
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
%47 = call i64 @malloc(i32 %46)
%48 = inttoptr i64 %47 to i8*
store i8* %48, i8** %9
%49 = load i8*, i8** %9
%50 = icmp ne i8* %49, null
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
%53 = load i8*, i8** %9
store i8* %53, i8** %10
br label %54
54: ; preds = %73, %33
%55 = load i32, i32* %4
%56 = load i32, i32* %8
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %81
58: ; preds = %54
%59 = load i32, i32* %3
%60 = load i8*, i8** %10
%61 = load i32, i32* %8
%62 = load i32, i32* %4
%63 = sub nsw i32 %61, %62
%64 = call i32 @read(i32 %59, i8* %60, i32 %63)
store i32 %64, i32* %5
%65 = load i32, i32* %5
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %58
%68 = load i32, i32* %5
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %68)
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%71 = load i8*, i8** %9
%72 = call i32 @free(i8* %71)
store i8* null, i8** %2
br label %87
73: ; preds = %58
%74 = load i32, i32* %5
%75 = load i8*, i8** %10
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i8, i8* %75, i64 %76
store i8* %77, i8** %10
%78 = load i32, i32* %5
%79 = load i32, i32* %4
%80 = add nsw i32 %79, %78
store i32 %80, i32* %4
br label %54
81: ; preds = %54
%82 = load i8*, i8** %9
%83 = load i32, i32* %4
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
store i8 0, i8* %85
%86 = load i8*, i8** %9
store i8* %86, i8** %2
br label %87
87: ; preds = %81, %67, %27
%88 = load i8*, i8** %2
ret i8* %88
}

LLVM IR (-O2):
@.str = [21 x i8] c"Failure in conn_read\00"
@.str.1 = [21 x i8] c"Should read %d bytes\00"
@.str.3 = [30 x i8] c"Error reading from socket: %d\00"
define i8* @conn_read(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to [4 x i8]*
%4 = bitcast i32* %2 to i8*
br label %7
5: ; preds = %7
%6 = icmp ult i32 %14, 4
br i1 %6, label %7, label %17
7: ; preds = %1, %5
%8 = phi i32 [ 0, %1 ], [ %14, %5 ]
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 %9
%11 = sub nuw nsw i32 4, %8
%12 = call i32 @read(i32 %0, i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
%14 = add nuw i32 %12, %8
br i1 %13, label %15, label %5
15: ; preds = %7
%16 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %52
17: ; preds = %5
%18 = load i32, i32* %2
%19 = tail call i32 @ntohl(i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = tail call i32 @putchar(i32 10)
%22 = icmp slt i32 %19, 1073741824
%23 = zext i1 %22 to i32
%24 = tail call i32 @assert(i32 %23)
%25 = add nsw i32 %19, 1
%26 = tail call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
%28 = icmp ne i64 %26, 0
%29 = zext i1 %28 to i32
%30 = tail call i32 @assert(i32 %29)
%31 = icmp sgt i32 %19, 0
br i1 %31, label %32, label %49
32: ; preds = %17, %42
%33 = phi i8* [ %44, %42 ], [ %27, %17 ]
%34 = phi i32 [ %45, %42 ], [ 0, %17 ]
%35 = sub nsw i32 %19, %34
%36 = tail call i32 @read(i32 %0, i8* %33, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%40 = tail call i32 @putchar(i32 10)
%41 = tail call i32 @free(i8* %27)
br label %52
42: ; preds = %32
%43 = zext i32 %36 to i64
%44 = getelementptr inbounds i8, i8* %33, i64 %43
%45 = add nsw i32 %36, %34
%46 = icmp sgt i32 %19, %45
br i1 %46, label %32, label %47
47: ; preds = %42
%48 = sext i32 %45 to i64
br label %49
49: ; preds = %47, %17
%50 = phi i64 [ 0, %17 ], [ %48, %47 ]
%51 = getelementptr inbounds i8, i8* %27, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %38, %49, %15
%53 = phi i8* [ null, %15 ], [ null, %38 ], [ %27, %49 ]
ret i8* %53
}