#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, char *, int);
int getkvlen(int fd, unsigned int *klen, unsigned int *vlen) {
  char buf[4];
  if (read(fd, buf, 4) != 4)
    return 0;
  *klen = (unsigned int)buf[0];
  *vlen = (unsigned int)buf[1] + (buf[2] << 8) + (buf[3] << 16);
  return 1;
}


x86 assembly (clang, -O0):
<getkvlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
cmp    $0x4,%eax
je     1149 <getkvlen+0x39>
movl   $0x0,-0x4(%rbp)
jmp    1176 <getkvlen+0x66>
movsbl -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movsbl -0x1b(%rbp),%ecx
movsbl -0x1a(%rbp),%eax
shl    $0x8,%eax
add    %eax,%ecx
movsbl -0x19(%rbp),%eax
shl    $0x10,%eax
add    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getkvlen>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4,%ecx
jne    115a <getkvlen+0x4a>
movsbl 0x4(%rsp),%eax
mov    %eax,(%rbx)
movsbl 0x5(%rsp),%eax
movsbl 0x6(%rsp),%ecx
shl    $0x8,%ecx
add    %eax,%ecx
movsbl 0x7(%rsp),%eax
shl    $0x10,%eax
add    %ecx,%eax
mov    %eax,(%r14)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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