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


x86 assembly (clang, -O0):
<netGetLongString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x0,-0xd(%rbp)
movb   $0x0,-0xe(%rbp)
mov    -0xc(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1050 <netReadAll@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    117f <netGetLongString+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    1217 <netGetLongString+0xe7>
cmpl   $0x0,-0x20(%rbp)
jge    11a2 <netGetLongString+0x72>
lea    0xe70(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    1217 <netGetLongString+0xe7>
movzbl -0xe(%rbp),%eax
shl    $0x8,%eax
movzbl -0xd(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <needMem@plt>
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1203 <netGetLongString+0xd3>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1050 <netReadAll@plt>
cmp    $0x0,%eax
jge    11fe <netGetLongString+0xce>
lea    0xe35(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    1217 <netGetLongString+0xe7>
jmp    1203 <netGetLongString+0xd3>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netGetLongString>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
movw   $0x0,0xe(%rsp)
xor    %ebx,%ebx
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1050 <netReadAll@plt>
test   %eax,%eax
je     11a7 <netGetLongString+0x77>
js     1190 <netGetLongString+0x60>
movzwl 0xe(%rsp),%ebp
shl    $0x30,%rbp
bswap  %rbp
lea    0x1(%rbp),%edi
call   1040 <needMem@plt>
mov    %rax,%rbx
test   %rbp,%rbp
je     118a <netGetLongString+0x5a>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <netReadAll@plt>
test   %eax,%eax
js     1199 <netGetLongString+0x69>
movb   $0x0,(%rbx,%rbp,1)
jmp    11a7 <netGetLongString+0x77>
lea    0xe69(%rip),%rdi
jmp    11a0 <netGetLongString+0x70>
lea    0xe81(%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 long string length\00"
@.str.1 = [31 x i8] c"Couldn't read long string body\00"
define i8* @netGetLongString(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [2 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8* null, i8** %5
store i32 0, i32* %6
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 0, i8* %8
%9 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
store i8 0, i8* %9
%10 = load i32, i32* %3
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%12 = call i32 (i32, ...) @netReadAll(i32 %10, i8* %11, i32 2)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i8* null, i8** %2
br label %50
16: ; preds = %1
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @warn(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %50
21: ; preds = %16
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = shl i32 %24, 8
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = add nsw i32 %25, %28
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = call i8* @needMem(i32 %31)
store i8* %32, i8** %5
%33 = load i32, i32* %6
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %21
%36 = load i32, i32* %3
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = call i32 (i32, ...) @netReadAll(i32 %36, i8* %37, i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = 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 %50
43: ; preds = %35
br label %44
44: ; preds = %43, %21
%45 = load i8*, i8** %5
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %5
store i8* %49, i8** %2
br label %50
50: ; preds = %44, %41, %19, %15
%51 = load i8*, i8** %2
ret i8* %51
}

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