#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 */
char *needMem(unsigned long);
int netReadAll(int, ...);
int /*<<< orphan*/ warn(char *);
char *netGetHugeString(int sd) {
  unsigned char b[4];
  char *s = NULL;
  unsigned long length = 0;
  int sz, i;
  sz = netReadAll(sd, b, 4);
  if (sz == 0)
    return NULL;
  if (sz < 4) {
    warn("Couldn't read huge string length");
    return NULL;
  }
  for (i = 0; i < 4; ++i) {
    length <<= 8;
    length += b[i];
  }
  s = needMem(length + 1);
  if (length > 0) {
    if (netReadAll(sd, s, length) < 0) {
      warn("Couldn't read huge string body");
      return NULL;
    }
  }
  s[length] = 0;
  return s;
}


x86 assembly (clang, -O0):
<netGetHugeString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <netReadAll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1178 <netGetHugeString+0x48>
movq   $0x0,-0x8(%rbp)
jmp    1240 <netGetHugeString+0x110>
cmpl   $0x4,-0x24(%rbp)
jge    119b <netGetHugeString+0x6b>
lea    0xe77(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <netGetHugeString+0x110>
movl   $0x0,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
jge    11d7 <netGetHugeString+0xa7>
mov    -0x20(%rbp),%rax
shl    $0x8,%rax
mov    %rax,-0x20(%rbp)
movslq -0x28(%rbp),%rax
movzbl -0x10(%rbp,%rax,1),%eax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11a2 <netGetHugeString+0x72>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1040 <needMem@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    122c <netGetHugeString+0xfc>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1050 <netReadAll@plt>
cmp    $0x0,%eax
jge    1227 <netGetHugeString+0xf7>
lea    0xe0c(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    1240 <netGetHugeString+0x110>
jmp    122c <netGetHugeString+0xfc>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netGetHugeString>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xor    %ebx,%ebx
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <netReadAll@plt>
test   %eax,%eax
je     11a4 <netGetHugeString+0x74>
cmp    $0x4,%eax
jge    1160 <netGetHugeString+0x30>
lea    0xea2(%rip),%rdi
jmp    119d <netGetHugeString+0x6d>
mov    0xc(%rsp),%ebp
shl    $0x20,%rbp
bswap  %rbp
lea    0x1(%rbp),%rdi
call   1040 <needMem@plt>
mov    %rax,%rbx
test   %rbp,%rbp
je     1190 <netGetHugeString+0x60>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <netReadAll@plt>
test   %eax,%eax
js     1196 <netGetHugeString+0x66>
movb   $0x0,(%rbx,%rbp,1)
jmp    11a4 <netGetHugeString+0x74>
lea    0xe84(%rip),%rdi
call   1030 <warn@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Couldn't read huge string length\00"
@.str.1 = [31 x i8] c"Couldn't read huge string body\00"
define i8* @netGetHugeString(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [4 x i8]
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i8* null, i8** %5
store i64 0, i64* %6
%9 = load i32, i32* %3
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
%11 = call i32 (i32, ...) @netReadAll(i32 %9, i8* %10, i32 4)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %57
15: ; preds = %1
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 4
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @warn(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %57
20: ; preds = %15
store i32 0, i32* %8
br label %21
21: ; preds = %34, %20
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 4
br i1 %23, label %24, label %37
24: ; preds = %21
%25 = load i64, i64* %6
%26 = shl i64 %25, 8
store i64 %26, i64* %6
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = zext i8 %30 to i64
%32 = load i64, i64* %6
%33 = add i64 %32, %31
store i64 %33, i64* %6
br label %34
34: ; preds = %24
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %21
37: ; preds = %21
%38 = load i64, i64* %6
%39 = add i64 %38, 1
%40 = call i8* @needMem(i64 %39)
store i8* %40, i8** %5
%41 = load i64, i64* %6
%42 = icmp ugt i64 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %37
%44 = load i32, i32* %3
%45 = load i8*, i8** %5
%46 = load i64, i64* %6
%47 = call i32 (i32, ...) @netReadAll(i32 %44, i8* %45, i64 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @warn(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %57
51: ; preds = %43
br label %52
52: ; preds = %51, %37
%53 = load i8*, i8** %5
%54 = load i64, i64* %6
%55 = getelementptr inbounds i8, i8* %53, i64 %54
store i8 0, i8* %55
%56 = load i8*, i8** %5
store i8* %56, i8** %2
br label %57
57: ; preds = %52, %49, %18, %14
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@.str = [33 x i8] c"Couldn't read huge string length\00"
@.str.1 = [31 x i8] c"Couldn't read huge string body\00"
define i8* @netGetHugeString(i32 %0) {
%2 = alloca [4 x i8]
%3 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 0
%4 = call i32 (i32, ...) @netReadAll(i32 %0, i8* %3, i32 4)
%5 = icmp eq i32 %4, 0
br i1 %5, label %38, label %6
6: ; preds = %1
%7 = icmp slt i32 %4, 4
br i1 %7, label %29, label %8
8: ; preds = %6
%9 = load i8, i8* %3
%10 = zext i8 %9 to i64
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 1
%12 = load i8, i8* %11
%13 = zext i8 %12 to i64
%14 = shl nuw nsw i64 %10, 16
%15 = shl nuw nsw i64 %13, 8
%16 = or i64 %14, %15
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 2
%18 = load i8, i8* %17
%19 = zext i8 %18 to i64
%20 = or i64 %16, %19
%21 = shl nuw nsw i64 %20, 8
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %2, i64 0, i64 3
%23 = load i8, i8* %22
%24 = zext i8 %23 to i64
%25 = or i64 %21, %24
%26 = add nuw nsw i64 %25, 1
%27 = call i8* @needMem(i64 %26)
%28 = icmp eq i64 %25, 0
br i1 %28, label %36, label %31
29: ; preds = %6
%30 = call i32 @warn(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %38
31: ; preds = %8
%32 = call i32 (i32, ...) @netReadAll(i32 %0, i8* %27, i64 %25)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @warn(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %38
36: ; preds = %31, %8
%37 = getelementptr inbounds i8, i8* %27, i64 %25
store i8 0, i8* %37
br label %38
38: ; preds = %1, %36, %34, %29
%39 = phi i8* [ null, %29 ], [ null, %34 ], [ %27, %36 ], [ null, %1 ]
ret i8* %39
}