#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 errno;
unsigned int fread(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
bool read_varlen(FILE *f, int c, unsigned *value) {
  unsigned size;
  unsigned char buf[4];
  *value = 0;
  switch (c & 3) {
  case 3:
    size = 4;
    break;
  case 2:
    size = 2;
    break;
  case 1:
    size = 1;
    break;
  default:
    printf("INVALID SIZE\n");
    return false;
  }
  memset(buf, 0, sizeof buf);
  if (fread(buf, 1, size, f) != size)
    goto err;
  *value = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + (buf[0] << 0);
  return true;
err:
  printf("(ERROR %d - %s)\n", errno, strerror(errno));
  return false;
}


x86 assembly (clang, -O0):
<read_varlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x14(%rbp),%eax
and    $0x3,%eax
mov    %eax,-0x2c(%rbp)
sub    $0x1,%eax
je     11ae <read_varlen+0x6e>
jmp    1174 <read_varlen+0x34>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     11a2 <read_varlen+0x62>
jmp    1185 <read_varlen+0x45>
mov    -0x2c(%rbp),%eax
sub    $0x3,%eax
jne    11ba <read_varlen+0x7a>
jmp    1196 <read_varlen+0x56>
movl   $0x4,-0x24(%rbp)
jmp    11d4 <read_varlen+0x94>
movl   $0x2,-0x24(%rbp)
jmp    11d4 <read_varlen+0x94>
movl   $0x1,-0x24(%rbp)
jmp    11d4 <read_varlen+0x94>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1270 <read_varlen+0x130>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    -0x28(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x24(%rbp),%eax
je     1207 <read_varlen+0xc7>
jmp    123b <read_varlen+0xfb>
movzbl -0x25(%rbp),%ecx
shl    $0x18,%ecx
movzbl -0x26(%rbp),%eax
shl    $0x10,%eax
add    %eax,%ecx
movzbl -0x27(%rbp),%eax
shl    $0x8,%eax
add    %eax,%ecx
movzbl -0x28(%rbp),%eax
shl    $0x0,%eax
add    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1270 <read_varlen+0x130>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    %rax,%rdx
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_varlen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x0,(%rdx)
and    $0x3,%esi
add    $0xffffffff,%esi
cmp    $0x3,%esi
jae    11ba <read_varlen+0x6a>
mov    %rdx,%rbx
mov    %rdi,%r14
movslq %esi,%rax
lea    0xea3(%rip),%rcx
mov    (%rcx,%rax,4),%r15d
lea    0xc(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    $0x1,%ebp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %r15d,%eax
jne    11c8 <read_varlen+0x78>
mov    0xc(%rsp),%eax
mov    %eax,(%rbx)
jmp    11ed <read_varlen+0x9d>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
jmp    11eb <read_varlen+0x9b>
mov    0x2e11(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe21(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"INVALID SIZE\0A\00"
@.str.1 = [17 x i8] c"(ERROR %d - %s)\0A\00"
@errno = global i32 0
define i32 @read_varlen(i32* %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [4 x i8]
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32*, i32** %7
store i32 0, i32* %10
%11 = load i32, i32* %6
%12 = and i32 %11, 3
switch i32 %12, label %16 [
i32 3, label %13
i32 2, label %14
i32 1, label %15
]
13: ; preds = %3
store i32 4, i32* %8
br label %18
14: ; preds = %3
store i32 2, i32* %8
br label %18
15: ; preds = %3
store i32 1, i32* %8
br label %18
16: ; preds = %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %54
18: ; preds = %15, %14, %13
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 4)
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%22 = load i32, i32* %8
%23 = load i32*, i32** %5
%24 = call i32 @fread(i8* %21, i32 1, i32 %22, i32* %23)
%25 = load i32, i32* %8
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %18
br label %49
28: ; preds = %18
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = shl i32 %31, 24
%33 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = shl i32 %35, 16
%37 = add nsw i32 %32, %36
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = shl i32 %40, 8
%42 = add nsw i32 %37, %41
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = shl i32 %45, 0
%47 = add nsw i32 %42, %46
%48 = load i32*, i32** %7
store i32 %47, i32* %48
store i32 1, i32* %4
br label %54
49: ; preds = %27
%50 = load i32, i32* @errno
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %50, i8* %52)
store i32 0, i32* %4
br label %54
54: ; preds = %49, %28, %16
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"(ERROR %d - %s)\0A\00"
@errno = global i32 0
@str = [13 x i8] c"INVALID SIZE\00"
@switch.table.read_varlen = [3 x i32] [i32 1, i32 2, i32 4]
define i32 @read_varlen(i32* nocapture %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca [4 x i8]
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
store i32 0, i32* %2
%6 = and i32 %1, 3
%7 = add nsw i32 %6, -1
%8 = icmp ult i32 %7, 3
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %40
11: ; preds = %3
%12 = sext i32 %7 to i64
%13 = getelementptr inbounds [3 x i32], [3 x i32]* @switch.table.read_varlen, i64 0, i64 %12
%14 = load i32, i32* %13
%15 = call i32 @memset(i8* %5, i32 0, i32 4)
%16 = call i32 @fread(i8* %5, i32 1, i32 %14, i32* %0)
%17 = icmp eq i32 %16, %14
br i1 %17, label %18, label %36
18: ; preds = %11
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = shl nuw i32 %21, 24
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = shl nuw nsw i32 %25, 16
%27 = or i32 %26, %22
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = shl nuw nsw i32 %30, 8
%32 = or i32 %27, %31
%33 = load i8, i8* %5
%34 = zext i8 %33 to i32
%35 = or i32 %32, %34
store i32 %35, i32* %2
br label %40
36: ; preds = %11
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %37, i8* %38)
br label %40
40: ; preds = %36, %18, %9
%41 = phi i32 [ 0, %9 ], [ 0, %36 ], [ 1, %18 ]
ret i32 %41
}