#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 get_tbh_size(char *);
int read_at_least(int, char *, int);
int tbh_read(int fd, char buf[], int buffsize) {
  int remaining, msgsize;
  if (read_at_least(fd, buf, 2) != 2)
    return 0;
  remaining = get_tbh_size(buf);
  ;
  msgsize = read_at_least(
      fd, &buf[2],
      (((remaining) < ((buffsize - 2))) ? (remaining) : ((buffsize - 2))));
  if (msgsize == 0)
    return 0;
  else
    return msgsize + 2;
}


x86 assembly (clang, -O0):
<tbh_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read_at_least@plt>
cmp    $0x2,%eax
je     1158 <tbh_read+0x38>
movl   $0x0,-0x4(%rbp)
jmp    11cc <tbh_read+0xac>
mov    -0x10(%rbp),%rdi
call   1030 <get_tbh_size@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jge    1192 <tbh_read+0x72>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    119b <tbh_read+0x7b>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%edx
call   1040 <read_at_least@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11c3 <tbh_read+0xa3>
movl   $0x0,-0x4(%rbp)
jmp    11cc <tbh_read+0xac>
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tbh_read>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    $0x2,%edx
call   1040 <read_at_least@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x2,%ecx
jne    116a <tbh_read+0x4a>
mov    %rbx,%rdi
call   1030 <get_tbh_size@plt>
add    $0x2,%rbx
add    $0xfffffffe,%ebp
cmp    %ebp,%eax
cmovl  %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <read_at_least@plt>
mov    %eax,%ecx
lea    0x2(%rcx),%eax
test   %ecx,%ecx
cmove  %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @tbh_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = call i32 @read_at_least(i32 %10, i8* %11, i32 2)
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 0, i32* %4
br label %39
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i32 @get_tbh_size(i8* %16)
store i32 %17, i32* %8
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 2
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = sub nsw i32 %22, 2
%24 = icmp slt i32 %21, %23
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = load i32, i32* %8
br label %30
27: ; preds = %15
%28 = load i32, i32* %7
%29 = sub nsw i32 %28, 2
br label %30
30: ; preds = %27, %25
%31 = phi i32 [ %26, %25 ], [ %29, %27 ]
%32 = call i32 @read_at_least(i32 %18, i8* %20, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 0, i32* %4
br label %39
36: ; preds = %30
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 2
store i32 %38, i32* %4
br label %39
39: ; preds = %36, %35, %14
%40 = load i32, i32* %4
ret i32 %40
}

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