#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 */
typedef int /*<<< orphan*/ len;

/* Variables and functions */
int ReceiveData(int, char *, int);
int /*<<< orphan*/ assert(int);
int read(int, int *, int);
int ReceiveString(int socket, char *str, int maxLen) {
  int len, received, b;
  b = read(socket, &len, sizeof(len));
  if (b <= 0)
    return -1;
  assert(len <= maxLen);
  assert(len >= 0);
  received = ReceiveData(socket, str, len);
  assert(received != -1);
  assert(received == len);
  str[len] = 0;
  return received;
}


x86 assembly (clang, -O0):
<ReceiveString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     116c <ReceiveString+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <ReceiveString+0xa8>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpl   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <ReceiveData@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReceiveString>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11a1 <ReceiveString+0x71>
movslq 0x4(%rsp),%rbx
xor    %edi,%edi
cmp    %r15d,%ebx
setle  %dil
call   1050 <assert@plt>
mov    %ebx,%edi
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <ReceiveData@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
cmp    %ebx,%ebp
sete   %dil
call   1050 <assert@plt>
movb   $0x0,(%r14,%rbx,1)
jmp    11a6 <ReceiveString+0x76>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ReceiveString(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %5
%12 = call i32 @read(i32 %11, i32* %8, i32 4)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %44
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i32, i32* %7
%19 = icmp sle i32 %17, %18
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* %8
%23 = icmp sge i32 %22, 0
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = load i32, i32* %5
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = call i32 @ReceiveData(i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, -1
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %9
%35 = load i32, i32* %8
%36 = icmp eq i32 %34, %35
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i8*, i8** %6
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i32, i32* %9
store i32 %43, i32* %4
br label %44
44: ; preds = %16, %15
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
define i32 @ReceiveString(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 @read(i32 %0, i32* %4, i32 4)
%7 = icmp slt i32 %6, 1
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, %2
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = xor i32 %9, -1
%14 = lshr i32 %13, 31
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @ReceiveData(i32 %0, i8* %1, i32 %9)
%17 = icmp ne i32 %16, -1
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = icmp eq i32 %16, %9
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = sext i32 %9 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %3, %8
%26 = phi i32 [ %16, %8 ], [ -1, %3 ]
ret i32 %26
}