#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*/ uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/ *, char *, int);
int fread_wrapper(uint8_t *buf, int bytes_to_read, FILE *f, int *add_len,
                  uint32_t fsize, char *version) {
  int bytes_read = 0;
  if (*add_len == 1) {
    *(uint32_t *)buf = (uint32_t)fsize;
    *add_len = 0;
    bytes_to_read -= 16;
    bytes_read = 16;
    buf += 4;
    memset(buf, 0, 12);
    strncpy(buf, version, 11);
    buf += 12;
  } else if (*add_len == 2) {
    *(buf++) = (fsize >> 24);
    *(buf++) = (fsize >> 16);
    *(buf++) = (fsize >> 8);
    *(buf++) = (fsize >> 0);
    *add_len = 0;
    bytes_to_read -= 16;
    bytes_read = 16;
    memset(buf, 0, 12);
    strncpy(buf, version, 11);
    buf += 12;
  }
  bytes_read += fread(buf, 1, bytes_to_read, f);
  return bytes_read;
}


x86 assembly (clang, -O0):
<fread_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%rax
cmpl   $0x1,(%rax)
jne    11c5 <fread_wrapper+0x95>
mov    -0x24(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0xc(%rbp)
movl   $0x10,-0x34(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xb,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rax
mov    %rax,-0x8(%rbp)
jmp    127b <fread_wrapper+0x14b>
mov    -0x20(%rbp),%rax
cmpl   $0x2,(%rax)
jne    1276 <fread_wrapper+0x146>
mov    -0x24(%rbp),%ecx
sar    $0x18,%ecx
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x8(%rbp)
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
sar    $0x10,%ecx
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x8(%rbp)
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
sar    $0x8,%ecx
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x8(%rbp)
mov    %ecx,(%rax)
mov    -0x24(%rbp),%ecx
sar    $0x0,%ecx
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x8(%rbp)
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0xc(%rbp),%eax
sub    $0x10,%eax
mov    %eax,-0xc(%rbp)
movl   $0x10,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0xb,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rax
mov    %rax,-0x8(%rbp)
jmp    127b <fread_wrapper+0x14b>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_wrapper>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r9,%r15
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbx
mov    (%rcx),%edx
mov    %rdi,%rax
cmp    $0x1,%edx
je     1171 <fread_wrapper+0x41>
cmp    $0x2,%edx
jne    11ac <fread_wrapper+0x7c>
mov    %r8d,%eax
sar    $0x18,%eax
mov    %eax,(%rbx)
mov    %r8d,%eax
sar    $0x10,%eax
mov    %eax,0x4(%rbx)
mov    %r8d,%edx
sar    $0x8,%edx
lea    0xc(%rbx),%rax
mov    %edx,0x8(%rbx)
mov    %r8d,(%rax)
movl   $0x0,(%rcx)
add    $0xfffffff0,%r12d
lea    0x10(%rbx),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0xb,%edx
call   1030 <strncpy@plt>
add    $0x40,%rbx
mov    $0x10,%ebp
jmp    11ae <fread_wrapper+0x7e>
xor    %ebp,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
add    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fread_wrapper(i32* %0, i32 %1, i32* %2, i32* %3, i32 %4, i8* %5) {
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i32 %4, i32* %11
store i8* %5, i8** %12
store i32 0, i32* %13
%14 = load i32*, i32** %10
%15 = load i32, i32* %14
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %32
17: ; preds = %6
%18 = load i32, i32* %11
%19 = load i32*, i32** %7
store i32 %18, i32* %19
%20 = load i32*, i32** %10
store i32 0, i32* %20
%21 = load i32, i32* %8
%22 = sub nsw i32 %21, 16
store i32 %22, i32* %8
store i32 16, i32* %13
%23 = load i32*, i32** %7
%24 = getelementptr inbounds i32, i32* %23, i64 4
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = call i32 @memset(i32* %25, i32 0, i32 12)
%27 = load i32*, i32** %7
%28 = load i8*, i8** %12
%29 = call i32 @strncpy(i32* %27, i8* %28, i32 11)
%30 = load i32*, i32** %7
%31 = getelementptr inbounds i32, i32* %30, i64 12
store i32* %31, i32** %7
br label %64
32: ; preds = %6
%33 = load i32*, i32** %10
%34 = load i32, i32* %33
%35 = icmp eq i32 %34, 2
br i1 %35, label %36, label %63
36: ; preds = %32
%37 = load i32, i32* %11
%38 = ashr i32 %37, 24
%39 = load i32*, i32** %7
%40 = getelementptr inbounds i32, i32* %39, i32 1
store i32* %40, i32** %7
store i32 %38, i32* %39
%41 = load i32, i32* %11
%42 = ashr i32 %41, 16
%43 = load i32*, i32** %7
%44 = getelementptr inbounds i32, i32* %43, i32 1
store i32* %44, i32** %7
store i32 %42, i32* %43
%45 = load i32, i32* %11
%46 = ashr i32 %45, 8
%47 = load i32*, i32** %7
%48 = getelementptr inbounds i32, i32* %47, i32 1
store i32* %48, i32** %7
store i32 %46, i32* %47
%49 = load i32, i32* %11
%50 = ashr i32 %49, 0
%51 = load i32*, i32** %7
%52 = getelementptr inbounds i32, i32* %51, i32 1
store i32* %52, i32** %7
store i32 %50, i32* %51
%53 = load i32*, i32** %10
store i32 0, i32* %53
%54 = load i32, i32* %8
%55 = sub nsw i32 %54, 16
store i32 %55, i32* %8
store i32 16, i32* %13
%56 = load i32*, i32** %7
%57 = call i32 @memset(i32* %56, i32 0, i32 12)
%58 = load i32*, i32** %7
%59 = load i8*, i8** %12
%60 = call i32 @strncpy(i32* %58, i8* %59, i32 11)
%61 = load i32*, i32** %7
%62 = getelementptr inbounds i32, i32* %61, i64 12
store i32* %62, i32** %7
br label %63
63: ; preds = %36, %32
br label %64
64: ; preds = %63, %17
%65 = load i32*, i32** %7
%66 = load i32, i32* %8
%67 = load i32*, i32** %9
%68 = call i64 @fread(i32* %65, i32 1, i32 %66, i32* %67)
%69 = load i32, i32* %13
%70 = sext i32 %69 to i64
%71 = add nsw i64 %70, %68
%72 = trunc i64 %71 to i32
store i32 %72, i32* %13
%73 = load i32, i32* %13
ret i32 %73
}

LLVM IR (-O2):
define i32 @fread_wrapper(i32* %0, i32 %1, i32* nocapture %2, i32* nocapture %3, i32 %4, i8* %5) {
%7 = load i32, i32* %3
switch i32 %7, label %22 [
i32 1, label %15
i32 2, label %8
]
8: ; preds = %6
%9 = ashr i32 %4, 24
%10 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %9, i32* %0
%11 = ashr i32 %4, 16
%12 = getelementptr inbounds i32, i32* %0, i64 2
store i32 %11, i32* %10
%13 = ashr i32 %4, 8
%14 = getelementptr inbounds i32, i32* %0, i64 3
store i32 %13, i32* %12
br label %15
15: ; preds = %6, %8
%16 = phi i32* [ %14, %8 ], [ %0, %6 ]
store i32 %4, i32* %16
store i32 0, i32* %3
%17 = add nsw i32 %1, -16
%18 = getelementptr inbounds i32, i32* %0, i64 4
%19 = tail call i32 @memset(i32* %18, i32 0, i32 12)
%20 = tail call i32 @strncpy(i32* %18, i8* %5, i32 11)
%21 = getelementptr inbounds i32, i32* %0, i64 16
br label %22
22: ; preds = %15, %6
%23 = phi i32 [ %1, %6 ], [ %17, %15 ]
%24 = phi i32* [ %0, %6 ], [ %21, %15 ]
%25 = phi i64 [ 0, %6 ], [ 16, %15 ]
%26 = tail call i64 @fread(i32* %24, i32 1, i32 %23, i32* %2)
%27 = add nsw i64 %26, %25
%28 = trunc i64 %27 to i32
ret i32 %28
}