#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 unsigned char ulong;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned char endian2(unsigned char, unsigned char, int);
unsigned char endian4(unsigned char, unsigned char, unsigned char,
                      unsigned char, int);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int fread_endian(ulong *x, int bytes, FILE *fp, int endian) {
  unsigned char u1, u2, u3, u4;
  if (bytes == 1) {
    if (fread(&u1, 1, 1, fp) != 1)
      return -1;
    *x = u1;
    return 1;
  } else if (bytes == 2) {
    if (fread(&u1, 1, 1, fp) != 1)
      return -1;
    if (fread(&u2, 1, 1, fp) != 1)
      return -1;
    *x = endian2(u1, u2, endian);
    return 2;
  } else if (bytes == 4) {
    if (fread(&u1, 1, 1, fp) != 1)
      return -1;
    if (fread(&u2, 1, 1, fp) != 1)
      return -1;
    if (fread(&u3, 1, 1, fp) != 1)
      return -1;
    if (fread(&u4, 1, 1, fp) != 1)
      return -1;
    *x = endian4(u1, u2, u3, u4, endian);
    return 4;
  } else {
    *x = 0;
    return 0;
  }
}


x86 assembly (clang, -O0):
<fread_endian>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    118e <fread_endian+0x5e>
mov    -0x20(%rbp),%rcx
lea    -0x25(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1179 <fread_endian+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x25(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
cmpl   $0x2,-0x14(%rbp)
jne    1212 <fread_endian+0xe2>
mov    -0x20(%rbp),%rcx
lea    -0x25(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11c1 <fread_endian+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x20(%rbp),%rcx
lea    -0x26(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11ea <fread_endian+0xba>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x25(%rbp),%cl
mov    -0x26(%rbp),%al
mov    -0x24(%rbp),%edx
movzbl %cl,%edi
movzbl %al,%esi
call   1050 <endian2@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x2,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
cmpl   $0x4,-0x14(%rbp)
jne    12f7 <fread_endian+0x1c7>
mov    -0x20(%rbp),%rcx
lea    -0x25(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1245 <fread_endian+0x115>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x20(%rbp),%rcx
lea    -0x26(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     126e <fread_endian+0x13e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x20(%rbp),%rcx
lea    -0x27(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1297 <fread_endian+0x167>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     12c0 <fread_endian+0x190>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x25(%rbp),%sil
mov    -0x26(%rbp),%dl
mov    -0x27(%rbp),%cl
mov    -0x28(%rbp),%al
mov    -0x24(%rbp),%r8d
movzbl %sil,%edi
movzbl %dl,%esi
movzbl %cl,%edx
movzbl %al,%ecx
call   1040 <endian4@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x4,-0x4(%rbp)
jmp    1305 <fread_endian+0x1d5>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fread_endian>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r12
mov    %rdi,%r15
cmp    $0x4,%esi
je     11f7 <fread_endian+0xc7>
cmp    $0x2,%esi
je     1196 <fread_endian+0x66>
xor    %eax,%eax
mov    $0x0,%ebx
cmp    $0x1,%esi
jne    128d <fread_endian+0x15d>
lea    0xc(%rsp),%rdi
mov    $0x1,%ebx
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
jne    1292 <fread_endian+0x162>
mov    0xc(%rsp),%al
jmp    128d <fread_endian+0x15d>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
jne    1292 <fread_endian+0x162>
lea    0xd(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1292 <fread_endian+0x162>
movzbl 0xc(%rsp),%edi
movzbl 0xd(%rsp),%esi
mov    %r14d,%edx
call   1050 <endian2@plt>
mov    $0x2,%ebx
jmp    128d <fread_endian+0x15d>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
jne    1292 <fread_endian+0x162>
lea    0xd(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1292 <fread_endian+0x162>
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1292 <fread_endian+0x162>
lea    0xe(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1292 <fread_endian+0x162>
movzbl 0xc(%rsp),%edi
movzbl 0xd(%rsp),%esi
movzbl 0xf(%rsp),%edx
movzbl 0xe(%rsp),%ecx
mov    %r14d,%r8d
call   1040 <endian4@plt>
mov    $0x4,%ebx
mov    %al,(%r15)
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @fread_endian(i8* nocapture writeonly %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
switch i32 %1, label %42 [
i32 1, label %9
i32 2, label %14
i32 4, label %24
]
9: ; preds = %4
%10 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %2)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %45
12: ; preds = %9
%13 = load i8, i8* %5
br label %42
14: ; preds = %4
%15 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %2)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %45
17: ; preds = %14
%18 = call i32 @fread(i8* %6, i32 1, i32 1, i32* %2)
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %45
20: ; preds = %17
%21 = load i8, i8* %5
%22 = load i8, i8* %6
%23 = tail call zeroext i8 @endian2(i8 zeroext %21, i8 zeroext %22, i32 %3)
br label %42
24: ; preds = %4
%25 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %2)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %45
27: ; preds = %24
%28 = call i32 @fread(i8* %6, i32 1, i32 1, i32* %2)
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %45
30: ; preds = %27
%31 = call i32 @fread(i8* %7, i32 1, i32 1, i32* %2)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %45
33: ; preds = %30
%34 = call i32 @fread(i8* %8, i32 1, i32 1, i32* %2)
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %45
36: ; preds = %33
%37 = load i8, i8* %5
%38 = load i8, i8* %6
%39 = load i8, i8* %7
%40 = load i8, i8* %8
%41 = tail call zeroext i8 @endian4(i8 zeroext %37, i8 zeroext %38, i8 zeroext %39, i8 zeroext %40, i32 %3)
br label %42
42: ; preds = %4, %12, %20, %36
%43 = phi i8 [ %41, %36 ], [ %23, %20 ], [ %13, %12 ], [ 0, %4 ]
%44 = phi i32 [ 4, %36 ], [ 2, %20 ], [ 1, %12 ], [ 0, %4 ]
store i8 %43, i8* %0
br label %45
45: ; preds = %42, %33, %30, %27, %24, %17, %14, %9
%46 = phi i32 [ -1, %9 ], [ -1, %14 ], [ -1, %17 ], [ -1, %24 ], [ -1, %27 ], [ -1, %30 ], [ -1, %33 ], [ %44, %42 ]
ret i32 %46
}