#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 read(int, ...);
int net_receive(int fd, char *data, int max) {
  unsigned char msb, lsb;
  int n, len;
  data[0] = 0;
  if (read(fd, &msb, 1) <= 0)
    return (0);
  if (read(fd, &lsb, 1) <= 0)
    return (0);
  len = (msb * 256) + lsb;
  if (len >= max)
    return (0);
  n = read(fd, data, len);
  if (n != len)
    return (0);
  data[len] = 0;
  return (len);
}


x86 assembly (clang, -O0):
<net_receive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <read@plt>
cmp    $0x0,%eax
jg     1151 <net_receive+0x41>
movl   $0x0,-0x4(%rbp)
jmp    11df <net_receive+0xcf>
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <read@plt>
cmp    $0x0,%eax
jg     1179 <net_receive+0x69>
movl   $0x0,-0x4(%rbp)
jmp    11df <net_receive+0xcf>
movzbl -0x15(%rbp),%eax
shl    $0x8,%eax
movzbl -0x16(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11a1 <net_receive+0x91>
movl   $0x0,-0x4(%rbp)
jmp    11df <net_receive+0xcf>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     11cd <net_receive+0xbd>
movl   $0x0,-0x4(%rbp)
jmp    11df <net_receive+0xcf>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_receive>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebx
movb   $0x0,(%rsi)
xor    %r12d,%r12d
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
jle    118c <net_receive+0x7c>
xor    %r12d,%r12d
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
jle    118c <net_receive+0x7c>
movzbl 0xf(%rsp),%eax
shl    $0x8,%rax
movzbl 0xe(%rsp),%ebp
or     %rax,%rbp
cmp    %r15d,%ebp
jge    118c <net_receive+0x7c>
xor    %r12d,%r12d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <read@plt>
cmp    %ebp,%eax
jne    118c <net_receive+0x7c>
movb   $0x0,(%r14,%rbp,1)
mov    %ebp,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @net_receive(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = alloca i8
store i8 0, i8* %1
%6 = call i32 (i32, ...) @read(i32 %0, i8* %4, i32 1)
%7 = icmp slt i32 %6, 1
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = call i32 (i32, ...) @read(i32 %0, i8* %5, i32 1)
%10 = icmp slt i32 %9, 1
br i1 %10, label %25, label %11
11: ; preds = %8
%12 = load i8, i8* %4
%13 = zext i8 %12 to i32
%14 = shl nuw nsw i32 %13, 8
%15 = load i8, i8* %5
%16 = zext i8 %15 to i32
%17 = or i32 %14, %16
%18 = icmp slt i32 %17, %2
br i1 %18, label %19, label %25
19: ; preds = %11
%20 = call i32 (i32, ...) @read(i32 %0, i8* %1, i32 %17)
%21 = icmp eq i32 %20, %17
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = zext i32 %17 to i64
%24 = getelementptr inbounds i8, i8* %1, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %19, %11, %8, %3, %22
%26 = phi i32 [ %17, %22 ], [ 0, %3 ], [ 0, %8 ], [ 0, %11 ], [ 0, %19 ]
ret i32 %26
}