#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 netReadAll(int, ...);
int /*<<< orphan*/ warn(char *);
char *netGetString(int sd, char buf[256]) {
  static char sbuf[256];
  unsigned char len = 0;
  int length;
  int sz;
  if (buf == NULL)
    buf = sbuf;
  sz = netReadAll(sd, &len, 1);
  if (sz == 0)
    return NULL;
  if (sz < 0) {
    warn("Couldn't read string length");
    return NULL;
  }
  length = len;
  if (length > 0)
    if (netReadAll(sd, buf, length) < 0) {
      warn("Couldn't read string body");
      return NULL;
    }
  buf[length] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<netGetString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1149 <netGetString+0x29>
lea    0x2efb(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1040 <netReadAll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1176 <netGetString+0x56>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <netGetString+0xd6>
cmpl   $0x0,-0x24(%rbp)
jge    1199 <netGetString+0x79>
lea    0xe79(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <netGetString+0xd6>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11e2 <netGetString+0xc2>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1040 <netReadAll@plt>
cmp    $0x0,%eax
jge    11dd <netGetString+0xbd>
lea    0xe51(%rip),%rdi
call   1030 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f6 <netGetString+0xd6>
jmp    11e2 <netGetString+0xc2>
mov    -0x18(%rbp),%rax
movslq -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):
<netGetString>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
movb   $0x0,0xf(%rsp)
test   %rsi,%rsi
lea    0x2f07(%rip),%rbx
cmovne %rsi,%rbx
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1040 <netReadAll@plt>
test   %eax,%eax
je     1190 <netGetString+0x70>
js     117b <netGetString+0x5b>
movzbl 0xf(%rsp),%r14d
test   %r14d,%r14d
je     1172 <netGetString+0x52>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1040 <netReadAll@plt>
test   %eax,%eax
js     1184 <netGetString+0x64>
mov    %r14d,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    1192 <netGetString+0x72>
lea    0xe7e(%rip),%rdi
jmp    118b <netGetString+0x6b>
lea    0xe91(%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):
@netGetString.sbuf = internal global [256 x i8] zeroinitializer
@.str = [28 x i8] c"Couldn't read string length\00"
@.str.1 = [26 x i8] c"Couldn't read string body\00"
define i8* @netGetString(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i8 0, i8* %6
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @netGetString.sbuf, i64 0, i64 0), i8** %5
br label %12
12: ; preds = %11, %2
%13 = load i32, i32* %4
%14 = call i32 (i32, ...) @netReadAll(i32 %13, i8* %6, i32 1)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i8* null, i8** %3
br label %43
18: ; preds = %12
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @warn(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %43
23: ; preds = %18
%24 = load i8, i8* %6
%25 = zext i8 %24 to i32
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %23
%29 = load i32, i32* %4
%30 = load i8*, i8** %5
%31 = load i32, i32* %7
%32 = call i32 (i32, ...) @netReadAll(i32 %29, i8* %30, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @warn(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %43
36: ; preds = %28
br label %37
37: ; preds = %36, %23
%38 = load i8*, i8** %5
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %5
store i8* %42, i8** %3
br label %43
43: ; preds = %37, %34, %21, %17
%44 = load i8*, i8** %3
ret i8* %44
}

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