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

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


x86 assembly (clang, -O0):
<bmp_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x1,-0x20(%rbp)
je     11e1 <bmp_write+0x71>
cmpl   $0x2,-0x20(%rbp)
je     11e1 <bmp_write+0x71>
cmpl   $0x4,-0x20(%rbp)
je     11e1 <bmp_write+0x71>
lea    0xe49(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12e4 <bmp_write+0x174>
movl   $0x0,-0x28(%rbp)
cmpl   $0x8,-0x28(%rbp)
jge    12cd <bmp_write+0x15d>
mov    -0x8(%rbp),%rcx
lea    -0x29(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1050 <fread@plt>
cmp    $0x0,%eax
jne    1253 <bmp_write+0xe3>
lea    0xdfb(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
movzbl -0x29(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <sum_last_bits@plt>
mov    %eax,%edx
mov    -0x34(%rbp),%eax
mov    $0xff,%ecx
sub    %edx,%ecx
and    %ecx,%eax
mov    %al,-0x29(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x2a(%rbp)
movzbl -0x2a(%rbp),%eax
mov    -0x28(%rbp),%ecx
shl    %cl,%eax
mov    %al,-0x2a(%rbp)
movzbl -0x2a(%rbp),%eax
mov    $0x8,%ecx
sub    -0x20(%rbp),%ecx
sar    %cl,%eax
mov    %al,-0x2a(%rbp)
movzbl -0x29(%rbp),%eax
movzbl -0x2a(%rbp),%ecx
or     %ecx,%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd7a(%rip),%rsi
movzbl -0x29(%rbp),%edx
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11fb <bmp_write+0x8b>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11e8 <bmp_write+0x78>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmp_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%ebx
mov    %rdx,%r12
mov    %rsi,0x10(%rsp)
mov    %rdi,0x18(%rsp)
lea    -0x1(%rbx),%eax
cmp    $0x2,%eax
jb     119f <bmp_write+0x2f>
cmp    $0x4,%ebx
jne    127a <bmp_write+0x10a>
test   %ecx,%ecx
jle    1237 <bmp_write+0xc7>
mov    $0x8,%ebp
sub    %ebx,%ebp
mov    %ecx,%eax
mov    %rax,0x20(%rsp)
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    0x18(%rsp),%rcx
call   1060 <fread@plt>
test   %eax,%eax
je     1248 <bmp_write+0xd8>
movzbl 0xf(%rsp),%r15d
mov    %ebx,%edi
call   1040 <sum_last_bits@plt>
not    %al
movzbl (%r12,%r14,1),%edx
mov    %r13d,%ecx
shl    %cl,%edx
movzbl %dl,%edx
mov    %ebp,%ecx
shr    %cl,%edx
and    %r15b,%al
or     %al,%dl
mov    %dl,0xf(%rsp)
movsbl %dl,%edi
mov    0x10(%rsp),%rsi
call   1080 <fputc@plt>
add    %ebx,%r13d
cmp    $0x8,%r13d
jl     11d0 <bmp_write+0x60>
add    $0x1,%r14
cmp    0x20(%rsp),%r14
jne    11c0 <bmp_write+0x50>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb1(%rip),%rdi
call   1050 <puts@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
call   1070 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1070 <fclose@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
lea    0xd8a(%rip),%rdi
call   1050 <puts@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    0x18(%rsp),%rdi
call   1070 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1070 <fclose@plt>
mov    $0x4,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"unexpected error calling bmp_write\0A\00"
@.str.1 = [12 x i8] c"EOF error.\0A\00"
@.str.2 = [3 x i8] c"%c\00"
define i32 @bmp_write(i32* %0, i32* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8
%14 = alloca i8
%15 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %15
%16 = load i32, i32* %10
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %33
18: ; preds = %5
%19 = load i32, i32* %10
%20 = icmp ne i32 %19, 2
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i32, i32* %10
%23 = icmp ne i32 %22, 4
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%26 = load i8*, i8** %8
%27 = call i32 @free(i8* %26)
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load i32*, i32** %7
%31 = call i32 @fclose(i32* %30)
%32 = call i32 @exit(i32 4)
unreachable
33: ; preds = %21, %18, %5
store i32 0, i32* %11
br label %34
34: ; preds = %95, %33
%35 = load i32, i32* %11
%36 = load i32, i32* %9
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %98
38: ; preds = %34
store i32 0, i32* %12
br label %39
39: ; preds = %88, %38
%40 = load i32, i32* %12
%41 = icmp slt i32 %40, 8
br i1 %41, label %42, label %92
42: ; preds = %39
%43 = load i32*, i32** %6
%44 = call i32 @fread(i8* %13, i32 1, i32 1, i32* %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %55, label %46
46: ; preds = %42
%47 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%48 = load i8*, i8** %8
%49 = call i32 @free(i8* %48)
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
%52 = load i32*, i32** %7
%53 = call i32 @fclose(i32* %52)
%54 = call i32 @exit(i32 2)
unreachable
55: ; preds = %42
%56 = load i8, i8* %13
%57 = zext i8 %56 to i32
%58 = load i32, i32* %10
%59 = call i32 @sum_last_bits(i32 %58)
%60 = sub nsw i32 255, %59
%61 = and i32 %57, %60
%62 = trunc i32 %61 to i8
store i8 %62, i8* %13
%63 = load i8*, i8** %8
%64 = load i32, i32* %15
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8, i8* %66
store i8 %67, i8* %14
%68 = load i8, i8* %14
%69 = zext i8 %68 to i32
%70 = load i32, i32* %12
%71 = shl i32 %69, %70
%72 = trunc i32 %71 to i8
store i8 %72, i8* %14
%73 = load i8, i8* %14
%74 = zext i8 %73 to i32
%75 = load i32, i32* %10
%76 = sub nsw i32 8, %75
%77 = ashr i32 %74, %76
%78 = trunc i32 %77 to i8
store i8 %78, i8* %14
%79 = load i8, i8* %13
%80 = zext i8 %79 to i32
%81 = load i8, i8* %14
%82 = zext i8 %81 to i32
%83 = or i32 %80, %82
%84 = trunc i32 %83 to i8
store i8 %84, i8* %13
%85 = load i32*, i32** %7
%86 = load i8, i8* %13
%87 = call i32 @fprintf(i32* %85, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %86)
br label %88
88: ; preds = %55
%89 = load i32, i32* %10
%90 = load i32, i32* %12
%91 = add nsw i32 %90, %89
store i32 %91, i32* %12
br label %39
92: ; preds = %39
%93 = load i32, i32* %15
%94 = add nsw i32 %93, 1
store i32 %94, i32* %15
br label %95
95: ; preds = %92
%96 = load i32, i32* %11
%97 = add nsw i32 %96, 1
store i32 %97, i32* %11
br label %34
98: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@str = [11 x i8] c"EOF error.\00"
@str.3 = [35 x i8] c"unexpected error calling bmp_write\00"
define i32 @bmp_write(i32* nocapture %0, i32* nocapture %1, i8* nocapture %2, i32 %3, i32 %4) {
%6 = alloca i8
switch i32 %4, label %7 [
i32 4, label %13
i32 2, label %13
i32 1, label %13
]
7: ; preds = %5
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.3, i64 0, i64 0))
%9 = tail call i32 @free(i8* %2)
%10 = tail call i32 @fclose(i32* %0)
%11 = tail call i32 @fclose(i32* %1)
%12 = tail call i32 @exit(i32 4)
unreachable
13: ; preds = %5, %5, %5
%14 = icmp sgt i32 %3, 0
br i1 %14, label %15, label %51
15: ; preds = %13
%16 = sub nuw nsw i32 8, %4
%17 = zext i32 %3 to i64
br label %18
18: ; preds = %15, %48
%19 = phi i64 [ 0, %15 ], [ %49, %48 ]
%20 = getelementptr inbounds i8, i8* %2, i64 %19
br label %21
21: ; preds = %18, %31
%22 = phi i32 [ 0, %18 ], [ %46, %31 ]
%23 = call i32 @fread(i8* %6, i32 1, i32 1, i32* %0)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%27 = tail call i32 @free(i8* %2)
%28 = tail call i32 @fclose(i32* %0)
%29 = tail call i32 @fclose(i32* %1)
%30 = tail call i32 @exit(i32 2)
unreachable
31: ; preds = %21
%32 = load i8, i8* %6
%33 = tail call i32 @sum_last_bits(i32 %4)
%34 = trunc i32 %33 to i8
%35 = xor i8 %34, -1
%36 = and i8 %32, %35
%37 = load i8, i8* %20
%38 = zext i8 %37 to i32
%39 = shl nuw nsw i32 %38, %22
%40 = and i32 %39, 255
%41 = lshr i32 %40, %16
%42 = trunc i32 %41 to i8
%43 = or i8 %36, %42
store i8 %43, i8* %6
%44 = sext i8 %43 to i32
%45 = tail call i32 @fputc(i32 %44, i32* %1)
%46 = add nsw i32 %22, %4
%47 = icmp slt i32 %46, 8
br i1 %47, label %21, label %48
48: ; preds = %31
%49 = add nuw nsw i64 %19, 1
%50 = icmp eq i64 %49, %17
br i1 %50, label %51, label %18
51: ; preds = %48, %13
ret i32 0
}