#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*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ bmp_write(int /*<<< orphan*/ *, int /*<<< orphan*/ *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char);
scalar_t__ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ *stdin;
int main(int argc, char **argv) {
  FILE *fpread;
  FILE *fpwrite;
  unsigned char *data;
  int maxdata;
  int datasize;
  char s_datasize[7];
  unsigned char c;
  int i;
  int n_last_bits;
  if (argc != 4) {
    printf("usage: echo \"data\" | %s <in file> <out file> <n last bits>\n",
           argv[0]);
    printf("       cat datafile | %s <in file> <out file> <n last bits>\n",
           argv[0]);
    return 1;
  }
  if ((fpread = fopen(argv[1], "r")) == NULL) {
    printf("error opening file: %s\n", argv[1]);
    return 2;
  }
  if ((fpwrite = fopen(argv[2], "w")) == NULL) {
    printf("error opening file: %s\n", argv[2]);
    fclose(fpread);
    return 2;
  }
  n_last_bits = atoi(argv[3]);
  if (n_last_bits != 1 && n_last_bits != 2 && n_last_bits != 4) {
    printf("error: number of last bits can only be 1, 2 or 4\n");
    fclose(fpread);
    fclose(fpwrite);
    return 3;
  }
  i = 0;
  fseek(fpread, 54, SEEK_SET);
  fread(&c, sizeof(c), 1, fpread);
  while (!feof(fpread)) {
    i++;
    fread(&c, sizeof(c), 1, fpread);
  }
  maxdata = ((i * n_last_bits) / 8) - 7;
  fprintf(stderr, "max data size: %d bytes\n", maxdata);
  fseek(fpread, 0, SEEK_SET);
  data = (char *)malloc(maxdata * sizeof(char));
  datasize = 0;
  while (fread(&c, sizeof(c), 1, stdin)) {
    if (datasize == maxdata) {
      printf("warning: data truncated at byte %d\n", datasize);
      break;
    }
    data[datasize] = c;
    datasize++;
  }
  snprintf(s_datasize, 7, "%d", datasize);
  for (i = 0; i < 54; i++) {
    fread(&c, sizeof(c), 1, fpread);
    fprintf(fpwrite, "%c", c);
  }
  bmp_write(fpread, fpwrite, s_datasize, 7, n_last_bits);
  bmp_write(fpread, fpwrite, data, datasize, n_last_bits);
  while (fread(&c, sizeof(c), 1, fpread)) {
    fprintf(fpwrite, "%c", c);
  }
  free(data);
  fclose(fpread);
  fclose(fpwrite);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1216 <main+0x56>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1522 <main+0x362>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe54(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    125a <main+0x9a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1522 <main+0x362>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe2a(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    12a7 <main+0xe7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x2,-0x4(%rbp)
jmp    1522 <main+0x362>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x1,-0x40(%rbp)
je     1301 <main+0x141>
cmpl   $0x2,-0x40(%rbp)
je     1301 <main+0x141>
cmpl   $0x4,-0x40(%rbp)
je     1301 <main+0x141>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x3,-0x4(%rbp)
jmp    1522 <main+0x362>
movl   $0x0,-0x3c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2cbd(%rip),%rax
mov    (%rax),%edx
mov    $0x36,%esi
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1351 <main+0x191>
jmp    1373 <main+0x1b3>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
jmp    1333 <main+0x173>
mov    -0x3c(%rbp),%eax
imul   -0x40(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
sub    $0x7,%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c49(%rip),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%eax
lea    0xd2b(%rip),%rsi
movzbl %al,%edx
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2c21(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
movslq -0x2c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2c0f(%rip),%rax
mov    (%rax),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
je     1434 <main+0x274>
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    1418 <main+0x258>
mov    -0x30(%rbp),%esi
lea    0xcd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1434 <main+0x274>
mov    -0x38(%rbp),%dl
mov    -0x28(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    13d2 <main+0x212>
lea    -0x37(%rbp),%rdi
mov    -0x30(%rbp),%ecx
mov    $0x7,%esi
lea    0xcbd(%rip),%rdx
call   1070 <snprintf@plt>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x36,-0x3c(%rbp)
jge    1493 <main+0x2d3>
mov    -0x18(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
lea    0xc8b(%rip),%rsi
movzbl -0x38(%rbp),%edx
call   1080 <fprintf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1453 <main+0x293>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x37(%rbp),%rdx
mov    -0x40(%rbp),%r8d
mov    $0x7,%ecx
mov    $0x0,%al
call   10e0 <bmp_write@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    -0x40(%rbp),%r8d
mov    $0x0,%al
call   10e0 <bmp_write@plt>
mov    -0x18(%rbp),%rcx
lea    -0x38(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fread@plt>
cmp    $0x0,%rax
je     1500 <main+0x340>
mov    -0x20(%rbp),%rdi
lea    0xc15(%rip),%rsi
movzbl -0x38(%rbp),%edx
call   1080 <fprintf@plt>
jmp    14c9 <main+0x309>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
cmp    $0x4,%edi
jne    1390 <main+0x1b0>
mov    0x8(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13bf <main+0x1df>
mov    %rax,%r12
mov    0x10(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13dc <main+0x1fc>
mov    %rax,%r14
mov    0x18(%rbp),%rdi
call   10f0 <atoi@plt>
cmp    $0x4,%eax
ja     1401 <main+0x221>
mov    %eax,%r13d
mov    $0x16,%eax
bt     %r13d,%eax
jae    1401 <main+0x221>
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x36,%esi
call   10d0 <fseek@plt>
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %r12,%rdi
call   10b0 <feof@plt>
xor    %ebp,%ebp
mov    $0x0,%ebx
test   %eax,%eax
jne    12c4 <main+0xe4>
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
nopl   (%rax)
add    $0x1,%ebx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %r12,%rdi
call   10b0 <feof@plt>
test   %eax,%eax
je     12a0 <main+0xc0>
mov    %r13d,0x1c(%rsp)
imul   %r13d,%ebx
lea    0x7(%rbx),%r13d
test   %ebx,%ebx
cmovns %ebx,%r13d
sar    $0x3,%r13d
add    $0xfffffff9,%r13d
mov    0x2cf2(%rip),%rax
mov    (%rax),%rdi
movzbl %r13b,%edx
lea    0xda1(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %r13d,%edi
call   10c0 <malloc@plt>
mov    %rax,0x10(%rsp)
mov    0x2cc8(%rip),%rax
mov    (%rax),%rcx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
test   %rax,%rax
je     143c <main+0x25c>
mov    %r13d,%ebx
xor    %ebp,%ebp
lea    0xf(%rsp),%r15
nopw   0x0(%rax,%rax,1)
cmp    %rbp,%rbx
je     1428 <main+0x248>
movzbl 0xf(%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %al,(%rcx,%rbp,1)
add    $0x1,%rbp
mov    0x2c77(%rip),%rax
mov    (%rax),%rcx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
test   %rax,%rax
jne    1350 <main+0x170>
jmp    143c <main+0x25c>
mov    0x0(%rbp),%rsi
lea    0xc65(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x0(%rbp),%rsi
lea    0xc8f(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%r13d
jmp    1521 <main+0x341>
mov    0x8(%rbp),%rsi
lea    0xcb1(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%r13d
jmp    1521 <main+0x341>
mov    0x10(%rbp),%rsi
lea    0xc94(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    $0x2,%r13d
jmp    1521 <main+0x341>
lea    0xccd(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    $0x3,%r13d
jmp    1521 <main+0x341>
lea    0xc7f(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13d,%ebp
lea    0xc8f(%rip),%rdx
lea    0x21(%rsp),%rdi
mov    $0x7,%esi
mov    %ebp,%ecx
call   1080 <snprintf@plt>
mov    $0x36,%ebx
lea    0xf(%rsp),%r15
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
movsbl 0xf(%rsp),%edi
mov    %r14,%rsi
call   1090 <fputc@plt>
add    $0xffffffff,%ebx
jne    1460 <main+0x280>
xor    %r13d,%r13d
lea    0x21(%rsp),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x7,%ecx
mov    0x1c(%rsp),%ebx
mov    %ebx,%r8d
xor    %eax,%eax
call   1100 <bmp_write@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    0x10(%rsp),%rdx
mov    %ebp,%ecx
mov    %ebx,%r8d
xor    %eax,%eax
call   1100 <bmp_write@plt>
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
test   %rax,%rax
je     1507 <main+0x327>
lea    0xf(%rsp),%rbx
movsbl 0xf(%rsp),%edi
mov    %r14,%rsi
call   1090 <fputc@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
test   %rax,%rax
jne    14e0 <main+0x300>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [60 x i8] c"usage: echo \22data\22 | %s <in file> <out file> <n last bits>\0A\00"
@.str.1 = [61 x i8] c" cat datafile | %s <in file> <out file> <n last bits>\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"error opening file: %s\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [50 x i8] c"error: number of last bits can only be 1, 2 or 4\0A\00"
@SEEK_SET = global i32 0
@stderr = global i32* null
@.str.6 = [25 x i8] c"max data size: %d bytes\0A\00"
@stdin = global i32* null
@.str.7 = [36 x i8] c"warning: data truncated at byte %d\0A\00"
@.str.8 = [3 x i8] c"%d\00"
@.str.9 = [3 x i8] c"%c\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [7 x i8]
%12 = alloca i8
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %26
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i32 1, i32* %3
br label %166
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %6
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %35)
store i32 2, i32* %3
br label %166
37: ; preds = %26
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 2
%40 = load i8*, i8** %39
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %41, i32** %7
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %50
43: ; preds = %37
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
store i32 2, i32* %3
br label %166
50: ; preds = %37
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 3
%53 = load i8*, i8** %52
%54 = call i32 @atoi(i8* %53)
store i32 %54, i32* %14
%55 = load i32, i32* %14
%56 = icmp ne i32 %55, 1
br i1 %56, label %57, label %69
57: ; preds = %50
%58 = load i32, i32* %14
%59 = icmp ne i32 %58, 2
br i1 %59, label %60, label %69
60: ; preds = %57
%61 = load i32, i32* %14
%62 = icmp ne i32 %61, 4
br i1 %62, label %63, label %69
63: ; preds = %60
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0))
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
%67 = load i32*, i32** %7
%68 = call i32 @fclose(i32* %67)
store i32 3, i32* %3
br label %166
69: ; preds = %60, %57, %50
store i32 0, i32* %13
%70 = load i32*, i32** %6
%71 = load i32, i32* @SEEK_SET
%72 = call i32 @fseek(i32* %70, i32 54, i32 %71)
%73 = load i32*, i32** %6
%74 = call i64 @fread(i8* %12, i32 1, i32 1, i32* %73)
br label %75
75: ; preds = %80, %69
%76 = load i32*, i32** %6
%77 = call i32 @feof(i32* %76)
%78 = icmp ne i32 %77, 0
%79 = xor i1 %78, true
br i1 %79, label %80, label %85
80: ; preds = %75
%81 = load i32, i32* %13
%82 = add nsw i32 %81, 1
store i32 %82, i32* %13
%83 = load i32*, i32** %6
%84 = call i64 @fread(i8* %12, i32 1, i32 1, i32* %83)
br label %75
85: ; preds = %75
%86 = load i32, i32* %13
%87 = load i32, i32* %14
%88 = mul nsw i32 %86, %87
%89 = sdiv i32 %88, 8
%90 = sub nsw i32 %89, 7
store i32 %90, i32* %9
%91 = load i32*, i32** @stderr
%92 = load i32, i32* %9
%93 = trunc i32 %92 to i8
%94 = call i32 @fprintf(i32* %91, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8 zeroext %93)
%95 = load i32*, i32** %6
%96 = load i32, i32* @SEEK_SET
%97 = call i32 @fseek(i32* %95, i32 0, i32 %96)
%98 = load i32, i32* %9
%99 = sext i32 %98 to i64
%100 = mul i64 %99, 1
%101 = trunc i64 %100 to i32
%102 = call i64 @malloc(i32 %101)
%103 = inttoptr i64 %102 to i8*
store i8* %103, i8** %8
store i32 0, i32* %10
br label %104
104: ; preds = %115, %85
%105 = load i32*, i32** @stdin
%106 = call i64 @fread(i8* %12, i32 1, i32 1, i32* %105)
%107 = icmp ne i64 %106, 0
br i1 %107, label %108, label %123
108: ; preds = %104
%109 = load i32, i32* %10
%110 = load i32, i32* %9
%111 = icmp eq i32 %109, %110
br i1 %111, label %112, label %115
112: ; preds = %108
%113 = load i32, i32* %10
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %113)
br label %123
115: ; preds = %108
%116 = load i8, i8* %12
%117 = load i8*, i8** %8
%118 = load i32, i32* %10
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds i8, i8* %117, i64 %119
store i8 %116, i8* %120
%121 = load i32, i32* %10
%122 = add nsw i32 %121, 1
store i32 %122, i32* %10
br label %104
123: ; preds = %112, %104
%124 = getelementptr inbounds [7 x i8], [7 x i8]* %11, i64 0, i64 0
%125 = load i32, i32* %10
%126 = call i32 @snprintf(i8* %124, i32 7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %125)
store i32 0, i32* %13
br label %127
127: ; preds = %136, %123
%128 = load i32, i32* %13
%129 = icmp slt i32 %128, 54
br i1 %129, label %130, label %139
130: ; preds = %127
%131 = load i32*, i32** %6
%132 = call i64 @fread(i8* %12, i32 1, i32 1, i32* %131)
%133 = load i32*, i32** %7
%134 = load i8, i8* %12
%135 = call i32 @fprintf(i32* %133, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8 zeroext %134)
br label %136
136: ; preds = %130
%137 = load i32, i32* %13
%138 = add nsw i32 %137, 1
store i32 %138, i32* %13
br label %127
139: ; preds = %127
%140 = load i32*, i32** %6
%141 = load i32*, i32** %7
%142 = getelementptr inbounds [7 x i8], [7 x i8]* %11, i64 0, i64 0
%143 = load i32, i32* %14
%144 = call i32 (i32*, i32*, ...) @bmp_write(i32* %140, i32* %141, i8* %142, i32 7, i32 %143)
%145 = load i32*, i32** %6
%146 = load i32*, i32** %7
%147 = load i8*, i8** %8
%148 = load i32, i32* %10
%149 = load i32, i32* %14
%150 = call i32 (i32*, i32*, ...) @bmp_write(i32* %145, i32* %146, i8* %147, i32 %148, i32 %149)
br label %151
151: ; preds = %155, %139
%152 = load i32*, i32** %6
%153 = call i64 @fread(i8* %12, i32 1, i32 1, i32* %152)
%154 = icmp ne i64 %153, 0
br i1 %154, label %155, label %159
155: ; preds = %151
%156 = load i32*, i32** %7
%157 = load i8, i8* %12
%158 = call i32 @fprintf(i32* %156, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8 zeroext %157)
br label %151
159: ; preds = %151
%160 = load i8*, i8** %8
%161 = call i32 @free(i8* %160)
%162 = load i32*, i32** %6
%163 = call i32 @fclose(i32* %162)
%164 = load i32*, i32** %7
%165 = call i32 @fclose(i32* %164)
store i32 0, i32* %3
br label %166
166: ; preds = %159, %63, %43, %32, %17
%167 = load i32, i32* %3
ret i32 %167
}

LLVM IR (-O2):
@.str = [60 x i8] c"usage: echo \22data\22 | %s <in file> <out file> <n last bits>\0A\00"
@.str.1 = [61 x i8] c" cat datafile | %s <in file> <out file> <n last bits>\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"error opening file: %s\0A\00"
@.str.4 = [2 x i8] c"w\00"
@SEEK_SET = global i32 0
@stderr = global i32* null
@.str.6 = [25 x i8] c"max data size: %d bytes\0A\00"
@stdin = global i32* null
@.str.7 = [36 x i8] c"warning: data truncated at byte %d\0A\00"
@.str.8 = [3 x i8] c"%d\00"
@str = [49 x i8] c"error: number of last bits can only be 1, 2 or 4\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [7 x i8]
%4 = alloca i8
%5 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%6 = icmp eq i32 %0, 4
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i8*, i8** %1
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %106
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %13
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %18)
br label %106
20: ; preds = %12
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i8*, i8** %21
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = tail call i32 @fclose(i32* %15)
br label %106
29: ; preds = %20
%30 = getelementptr inbounds i8*, i8** %1, i64 3
%31 = load i8*, i8** %30
%32 = tail call i32 @atoi(i8* %31)
switch i32 %32, label %33 [
i32 4, label %37
i32 2, label %37
i32 1, label %37
]
33: ; preds = %29
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%35 = tail call i32 @fclose(i32* %15)
%36 = tail call i32 @fclose(i32* %23)
br label %106
37: ; preds = %29, %29, %29
%38 = load i32, i32* @SEEK_SET
%39 = tail call i32 @fseek(i32* %15, i32 54, i32 %38)
%40 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %15)
%41 = tail call i32 @feof(i32* %15)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %37, %43
%44 = phi i32 [ %45, %43 ], [ 0, %37 ]
%45 = add nuw nsw i32 %44, 1
%46 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %15)
%47 = tail call i32 @feof(i32* %15)
%48 = icmp eq i32 %47, 0
br i1 %48, label %43, label %49
49: ; preds = %43, %37
%50 = phi i32 [ 0, %37 ], [ %45, %43 ]
%51 = mul nsw i32 %50, %32
%52 = sdiv i32 %51, 8
%53 = add nsw i32 %52, -7
%54 = load i32*, i32** @stderr
%55 = trunc i32 %53 to i8
%56 = tail call i32 @fprintf(i32* %54, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8 zeroext %55)
%57 = load i32, i32* @SEEK_SET
%58 = tail call i32 @fseek(i32* %15, i32 0, i32 %57)
%59 = tail call i64 @malloc(i32 %53)
%60 = inttoptr i64 %59 to i8*
%61 = load i32*, i32** @stdin
%62 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %80, label %64
64: ; preds = %49
%65 = zext i32 %53 to i64
br label %66
66: ; preds = %64, %71
%67 = phi i64 [ 0, %64 ], [ %74, %71 ]
%68 = icmp eq i64 %67, %65
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %53)
br label %80
71: ; preds = %66
%72 = load i8, i8* %4
%73 = getelementptr inbounds i8, i8* %60, i64 %67
store i8 %72, i8* %73
%74 = add nuw nsw i64 %67, 1
%75 = load i32*, i32** @stdin
%76 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %75)
%77 = icmp eq i64 %76, 0
br i1 %77, label %78, label %66
78: ; preds = %71
%79 = trunc i64 %74 to i32
br label %80
80: ; preds = %78, %49, %69
%81 = phi i32 [ %53, %69 ], [ 0, %49 ], [ %79, %78 ]
%82 = call i32 @snprintf(i8* %5, i32 7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 %81)
br label %83
83: ; preds = %80, %83
%84 = phi i32 [ 0, %80 ], [ %89, %83 ]
%85 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %15)
%86 = load i8, i8* %4
%87 = sext i8 %86 to i32
%88 = call i32 @fputc(i32 %87, i32* %23)
%89 = add nuw nsw i32 %84, 1
%90 = icmp eq i32 %89, 54
br i1 %90, label %91, label %83
91: ; preds = %83
%92 = call i32 (i32*, i32*, ...) @bmp_write(i32* %15, i32* %23, i8* %5, i32 7, i32 %32)
%93 = call i32 (i32*, i32*, ...) @bmp_write(i32* %15, i32* %23, i8* %60, i32 %81, i32 %32)
%94 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %15)
%95 = icmp eq i64 %94, 0
br i1 %95, label %102, label %96
96: ; preds = %91, %96
%97 = load i8, i8* %4
%98 = sext i8 %97 to i32
%99 = call i32 @fputc(i32 %98, i32* %23)
%100 = call i64 @fread(i8* %4, i32 1, i32 1, i32* %15)
%101 = icmp eq i64 %100, 0
br i1 %101, label %102, label %96
102: ; preds = %96, %91
%103 = call i32 @free(i8* %60)
%104 = call i32 @fclose(i32* %15)
%105 = call i32 @fclose(i32* %23)
br label %106
106: ; preds = %102, %33, %25, %17, %7
%107 = phi i32 [ 1, %7 ], [ 2, %17 ], [ 2, %25 ], [ 3, %33 ], [ 0, %102 ]
ret i32 %107
}