#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 uint32_t;

/* Variables and functions */
int recv_full(int, char *, unsigned int);
uint32_t pkt_recv_header(int fd, char *buf) {
  uint32_t rcvd = 0;
  if ((rcvd = recv_full(fd, buf, 2)) == 0)
    return 0;
  unsigned c = (unsigned char)buf[1];
  if (c > 0x80) {
    if ((rcvd = recv_full(fd, buf + 2, c - 0x80)) == 0)
      return 0;
    rcvd += 2;
  }
  return rcvd;
}


x86 assembly (clang, -O0):
<pkt_recv_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <recv_full@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    114f <pkt_recv_header+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <pkt_recv_header+0x97>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x80,-0x18(%rbp)
jbe    11a1 <pkt_recv_header+0x91>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x2,%rsi
mov    -0x18(%rbp),%edx
sub    $0x80,%edx
call   1030 <recv_full@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jne    1198 <pkt_recv_header+0x88>
movl   $0x0,-0x4(%rbp)
jmp    11a7 <pkt_recv_header+0x97>
mov    -0x14(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkt_recv_header>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x2,%edx
call   1030 <recv_full@plt>
test   %eax,%eax
je     114f <pkt_recv_header+0x3f>
movzbl 0x1(%rbx),%edx
cmp    $0x81,%edx
jb     1151 <pkt_recv_header+0x41>
add    $0x2,%rbx
add    $0xffffff80,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <recv_full@plt>
mov    %eax,%ecx
lea    0x2(%rcx),%eax
test   %ecx,%ecx
cmove  %ecx,%eax
jmp    1151 <pkt_recv_header+0x41>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pkt_recv_header(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = call i32 @recv_full(i32 %8, i8* %9, i32 2)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %34
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp ugt i32 %18, 128
br i1 %19, label %20, label %32
20: ; preds = %13
%21 = load i32, i32* %4
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 2
%24 = load i32, i32* %7
%25 = sub i32 %24, 128
%26 = call i32 @recv_full(i32 %21, i8* %23, i32 %25)
store i32 %26, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
store i32 0, i32* %3
br label %34
29: ; preds = %20
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 2
store i32 %31, i32* %6
br label %32
32: ; preds = %29, %13
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %28, %12
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @pkt_recv_header(i32 %0, i8* %1) {
%3 = tail call i32 @recv_full(i32 %0, i8* %1, i32 2)
%4 = icmp eq i32 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %1, i64 1
%7 = load i8, i8* %6
%8 = icmp ugt i8 %7, -128
br i1 %8, label %9, label %17
9: ; preds = %5
%10 = zext i8 %7 to i32
%11 = getelementptr inbounds i8, i8* %1, i64 2
%12 = add nsw i32 %10, -128
%13 = tail call i32 @recv_full(i32 %0, i8* %11, i32 %12)
%14 = icmp eq i32 %13, 0
%15 = add nsw i32 %13, 2
%16 = select i1 %14, i32 0, i32 %15
br label %17
17: ; preds = %9, %5, %2
%18 = phi i32 [ 0, %2 ], [ %3, %5 ], [ %16, %9 ]
ret i32 %18
}