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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int ntohs(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, ...);
int read_pkt(int fd, char *buf) {
  uint16_t len;
  int n, pending;
  if ((read(fd, &len, sizeof(len))) < 0) {
    perror("read_pkt(size):");
    exit(1);
  }
  len = ntohs(len);
  pending = len;
  while (pending > 0) {
    if ((n = read(fd, buf, pending)) < 0) {
      perror("read_pkt(data):");
      return 0;
    }
    pending -= n;
    buf += n;
  }
  return len;
}


x86 assembly (clang, -O0):
<read_pkt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <read@plt>
cmp    $0x0,%rax
jge    1182 <read_pkt+0x42>
lea    0xe8d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11f3 <read_pkt+0xb3>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11d2 <read_pkt+0x92>
lea    0xe4f(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <read_pkt+0xb9>
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1193 <read_pkt+0x53>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pkt>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %rax,%rax
js     11bc <read_pkt+0x7c>
mov    0xc(%rsp),%edi
call   1040 <ntohs@plt>
mov    %eax,%ebp
mov    %eax,0xc(%rsp)
test   %eax,%eax
jle    11a1 <read_pkt+0x61>
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <read@plt>
test   %eax,%eax
js     11ac <read_pkt+0x6c>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1180 <read_pkt+0x40>
mov    0xc(%rsp),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
xor    %ebp,%ebp
jmp    11a1 <read_pkt+0x61>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"read_pkt(size):\00"
@.str.1 = [16 x i8] c"read_pkt(data):\00"
define i32 @read_pkt(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = call i64 (i32, ...) @read(i32 %9, i32* %6, i64 4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @ntohs(i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
store i32 %18, i32* %8
br label %19
19: ; preds = %31, %15
%20 = load i32, i32* %8
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %39
22: ; preds = %19
%23 = load i32, i32* %4
%24 = load i8*, i8** %5
%25 = load i32, i32* %8
%26 = call i64 (i32, ...) @read(i32 %23, i8* %24, i32 %25)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %41
31: ; preds = %22
%32 = load i32, i32* %7
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, %32
store i32 %34, i32* %8
%35 = load i32, i32* %7
%36 = load i8*, i8** %5
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %5
br label %19
39: ; preds = %19
%40 = load i32, i32* %6
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %29
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@.str = [16 x i8] c"read_pkt(size):\00"
@.str.1 = [16 x i8] c"read_pkt(data):\00"
define i32 @read_pkt(i32 %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i64 (i32, ...) @read(i32 %0, i32* %3, i64 4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* %3
%12 = call i32 @ntohs(i32 %11)
store i32 %12, i32* %3
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %10, %22
%15 = phi i32 [ %23, %22 ], [ %12, %10 ]
%16 = phi i8* [ %26, %22 ], [ %1, %10 ]
%17 = call i64 (i32, ...) @read(i32 %0, i8* %16, i32 %15)
%18 = trunc i64 %17 to i32
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %30
22: ; preds = %14
%23 = sub nsw i32 %15, %18
%24 = shl i64 %17, 32
%25 = ashr exact i64 %24, 32
%26 = getelementptr inbounds i8, i8* %16, i64 %25
%27 = icmp sgt i32 %23, 0
br i1 %27, label %14, label %28
28: ; preds = %22
%29 = load i32, i32* %3
br label %30
30: ; preds = %10, %28, %20
%31 = phi i32 [ 0, %20 ], [ %29, %28 ], [ %12, %10 ]
ret i32 %31
}