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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int bmp_read(FILE *fpread, unsigned char *data, int datasize, int n_last_bits) {
  int i;
  int j;
  unsigned char c;
  unsigned char c2;
  if (n_last_bits != 1 && n_last_bits != 2 && n_last_bits != 4) {
    printf("unexpected error calling bmp_read\n");
    fclose(fpread);
    exit(4);
  }
  for (i = 0; i < datasize; ++i) {
    c = 0x00;
    for (j = 0; j < 8; j += n_last_bits) {
      fread(&c2, sizeof(c2), 1, fpread);
      c2 = c2 << (8 - n_last_bits);
      c2 = c2 >> j;
      c = c | c2;
    }
    data[i] = c;
  }
  return 0;
}


x86 assembly (clang, -O0):
<bmp_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
je     1193 <bmp_read+0x53>
cmpl   $0x2,-0x18(%rbp)
je     1193 <bmp_read+0x53>
cmpl   $0x4,-0x18(%rbp)
je     1193 <bmp_read+0x53>
lea    0xe85(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x4,%edi
call   1060 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1223 <bmp_read+0xe3>
movb   $0x0,-0x21(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x8,-0x20(%rbp)
jge    1207 <bmp_read+0xc7>
mov    -0x8(%rbp),%rcx
lea    -0x22(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
movzbl -0x22(%rbp),%eax
mov    $0x8,%ecx
sub    -0x18(%rbp),%ecx
shl    %cl,%eax
mov    %al,-0x22(%rbp)
movzbl -0x22(%rbp),%eax
mov    -0x20(%rbp),%ecx
sar    %cl,%eax
mov    %al,-0x22(%rbp)
movzbl -0x21(%rbp),%eax
movzbl -0x22(%rbp),%ecx
or     %ecx,%eax
mov    %al,-0x21(%rbp)
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11b1 <bmp_read+0x71>
mov    -0x21(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119a <bmp_read+0x5a>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmp_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r13d
mov    %rsi,0x18(%rsp)
mov    %rdi,%r14
lea    -0x1(%r13),%eax
cmp    $0x2,%eax
jb     116c <bmp_read+0x2c>
cmp    $0x4,%r13d
jne    1201 <bmp_read+0xc1>
test   %edx,%edx
jle    11f0 <bmp_read+0xb0>
mov    $0x8,%ebp
sub    %r13d,%ebp
mov    %edx,%eax
mov    %rax,0x10(%rsp)
xor    %eax,%eax
lea    0xf(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %rax,0x20(%rsp)
xor    %r15d,%r15d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
movzbl 0xf(%rsp),%eax
mov    %ebp,%ecx
shl    %cl,%eax
movzbl %al,%eax
mov    %ebx,%ecx
shr    %cl,%eax
mov    %al,0xf(%rsp)
or     %al,%r15b
add    %r13d,%ebx
cmp    $0x8,%ebx
jl     11a0 <bmp_read+0x60>
mov    0x18(%rsp),%rax
mov    0x20(%rsp),%rcx
mov    %r15b,(%rax,%rcx,1)
mov    %rcx,%rax
add    $0x1,%rax
cmp    0x10(%rsp),%rax
jne    1190 <bmp_read+0x50>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    $0x4,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"unexpected error calling bmp_read\0A\00"
define i32 @bmp_read(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i8
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, 1
br i1 %14, label %15, label %26
15: ; preds = %4
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %26
18: ; preds = %15
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 4
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%23 = load i32*, i32** %5
%24 = call i32 @fclose(i32* %23)
%25 = call i32 @exit(i32 4)
unreachable
26: ; preds = %18, %15, %4
store i32 0, i32* %9
br label %27
27: ; preds = %65, %26
%28 = load i32, i32* %9
%29 = load i32, i32* %7
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %68
31: ; preds = %27
store i8 0, i8* %11
store i32 0, i32* %10
br label %32
32: ; preds = %55, %31
%33 = load i32, i32* %10
%34 = icmp slt i32 %33, 8
br i1 %34, label %35, label %59
35: ; preds = %32
%36 = load i32*, i32** %5
%37 = call i32 @fread(i8* %12, i32 1, i32 1, i32* %36)
%38 = load i8, i8* %12
%39 = zext i8 %38 to i32
%40 = load i32, i32* %8
%41 = sub nsw i32 8, %40
%42 = shl i32 %39, %41
%43 = trunc i32 %42 to i8
store i8 %43, i8* %12
%44 = load i8, i8* %12
%45 = zext i8 %44 to i32
%46 = load i32, i32* %10
%47 = ashr i32 %45, %46
%48 = trunc i32 %47 to i8
store i8 %48, i8* %12
%49 = load i8, i8* %11
%50 = zext i8 %49 to i32
%51 = load i8, i8* %12
%52 = zext i8 %51 to i32
%53 = or i32 %50, %52
%54 = trunc i32 %53 to i8
store i8 %54, i8* %11
br label %55
55: ; preds = %35
%56 = load i32, i32* %8
%57 = load i32, i32* %10
%58 = add nsw i32 %57, %56
store i32 %58, i32* %10
br label %32
59: ; preds = %32
%60 = load i8, i8* %11
%61 = load i8*, i8** %6
%62 = load i32, i32* %9
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8 %60, i8* %64
br label %65
65: ; preds = %59
%66 = load i32, i32* %9
%67 = add nsw i32 %66, 1
store i32 %67, i32* %9
br label %27
68: ; preds = %27
ret i32 0
}

LLVM IR (-O2):
@str = [34 x i8] c"unexpected error calling bmp_read\00"
define i32 @bmp_read(i32* nocapture %0, i8* nocapture writeonly %1, i32 %2, i32 %3) {
%5 = alloca i8
switch i32 %3, label %6 [
i32 4, label %10
i32 2, label %10
i32 1, label %10
]
6: ; preds = %4
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @fclose(i32* %0)
%9 = tail call i32 @exit(i32 4)
unreachable
10: ; preds = %4, %4, %4
%11 = icmp sgt i32 %2, 0
br i1 %11, label %12, label %34
12: ; preds = %10
%13 = sub nuw nsw i32 8, %3
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %12, %30
%16 = phi i64 [ 0, %12 ], [ %32, %30 ]
br label %17
17: ; preds = %15, %17
%18 = phi i8 [ 0, %15 ], [ %27, %17 ]
%19 = phi i32 [ 0, %15 ], [ %28, %17 ]
%20 = call i32 @fread(i8* %5, i32 1, i32 1, i32* %0)
%21 = load i8, i8* %5
%22 = zext i8 %21 to i32
%23 = shl i32 %22, %13
%24 = and i32 %23, 255
%25 = lshr i32 %24, %19
%26 = trunc i32 %25 to i8
store i8 %26, i8* %5
%27 = or i8 %18, %26
%28 = add nsw i32 %19, %3
%29 = icmp slt i32 %28, 8
br i1 %29, label %17, label %30
30: ; preds = %17
%31 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 %27, i8* %31
%32 = add nuw nsw i64 %16, 1
%33 = icmp eq i64 %32, %14
br i1 %33, label %34, label %15
34: ; preds = %30, %10
ret i32 0
}