#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;
typedef int ssize_t;

/* Variables and functions */
int htonl(int);
int readn(int, ...);
ssize_t readvrec1(int s, char *p, int n) {
  uint32_t headlen;
  ssize_t rv;
  rv = readn(s, &headlen, sizeof(uint32_t));
  if (rv != sizeof(uint32_t))
    return rv < 0 ? -1 : 0;
  headlen = htonl(headlen);
  if (headlen > n) {
    headlen = n;
  }
  rv = readn(s, p, headlen);
  if (rv != headlen)
    return rv < 0 ? -1 : 0;
  return rv;
}


x86 assembly (clang, -O0):
<readvrec1>:
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
mov    $0x0,%al
call   1040 <readn@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
je     116e <readvrec1+0x4e>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11c9 <readvrec1+0xa9>
mov    -0x18(%rbp),%edi
call   1030 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    118b <readvrec1+0x6b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1040 <readn@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     11c3 <readvrec1+0xa3>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x0,%edx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    11c9 <readvrec1+0xa9>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readvrec1>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <readn@plt>
cmp    $0x4,%eax
jne    1173 <readvrec1+0x53>
mov    0xc(%rsp),%edi
call   1030 <htonl@plt>
cmp    %ebx,%eax
cmovle %eax,%ebx
mov    %ebx,0xc(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <readn@plt>
mov    %eax,%ecx
sar    $0x1f,%eax
cmp    0xc(%rsp),%ecx
cmove  %ecx,%eax
jmp    1176 <readvrec1+0x56>
sar    $0x1f,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readvrec1(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 = call i32 (i32, ...) @readn(i32 %10, i32* %8, i64 4)
store i32 %11, i32* %9
%12 = load i32, i32* %9
%13 = sext i32 %12 to i64
%14 = icmp ne i64 %13, 4
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 -1, i32 0
store i32 %19, i32* %4
br label %43
20: ; preds = %3
%21 = load i32, i32* %8
%22 = call i32 @htonl(i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = load i32, i32* %7
%25 = icmp sgt i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i32, i32* %7
store i32 %27, i32* %8
br label %28
28: ; preds = %26, %20
%29 = load i32, i32* %5
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = call i32 (i32, ...) @readn(i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = load i32, i32* %8
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 0
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 -1, i32 0
store i32 %40, i32* %4
br label %43
41: ; preds = %28
%42 = load i32, i32* %9
store i32 %42, i32* %4
br label %43
43: ; preds = %41, %36, %15
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
define i32 @readvrec1(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i32 (i32, ...) @readn(i32 %0, i32* %4, i64 4)
%7 = icmp eq i32 %6, 4
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = ashr i32 %6, 31
br label %20
10: ; preds = %3
%11 = load i32, i32* %4
%12 = call i32 @htonl(i32 %11)
%13 = icmp sgt i32 %12, %2
%14 = select i1 %13, i32 %2, i32 %12
store i32 %14, i32* %4
%15 = call i32 (i32, ...) @readn(i32 %0, i8* %1, i32 %14)
%16 = load i32, i32* %4
%17 = icmp eq i32 %15, %16
%18 = ashr i32 %15, 31
%19 = select i1 %17, i32 %15, i32 %18
br label %20
20: ; preds = %10, %8
%21 = phi i32 [ %9, %8 ], [ %19, %10 ]
ret i32 %21
}