#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int readlength(FILE *f) {
  char s = 0;
  unsigned char buf[4];
  int length;
  int t;
  t = fread(buf, 1, 4, f);
  if (t != 4) {
    return -1;
  }
  length = (((int)buf[0]) << 24) | (((int)buf[1]) << 16) |
           (((int)buf[2]) << 8) | buf[3];
  return length;
}


x86 assembly (clang, -O0):
<readlength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
lea    -0x15(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x4,-0x20(%rbp)
je     1150 <readlength+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1178 <readlength+0x68>
movzbl -0x15(%rbp),%eax
shl    $0x18,%eax
movzbl -0x14(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movzbl -0x13(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x12(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlength>:
push   %rax
mov    %rdi,%rcx
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x4,%ecx
jne    113a <readlength+0x2a>
mov    0x4(%rsp),%eax
bswap  %eax
pop    %rcx
ret

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

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