#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ bmp_read(int /*<<< orphan*/ *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned char);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  FILE *fpread;
  int i;
  int msgsize;
  char s_msgsize[7 + 1];
  int n_last_bits;
  unsigned char *data;
  if (argc != 3) {
    printf("usage: %s <in file> <n last bits> > <out file>\n", argv[0]);
    return 1;
  }
  if ((fpread = fopen(argv[1], "r")) == NULL) {
    printf("error opening file: %s\n", argv[1]);
    return 2;
  }
  n_last_bits = atoi(argv[2]);
  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);
    return 3;
  }
  fseek(fpread, 54, SEEK_SET);
  bmp_read(fpread, s_msgsize, 7, n_last_bits);
  s_msgsize[7 + 1] = '\0';
  msgsize = atoi(s_msgsize);
  fprintf(stderr, "msg size: %d bytes\n", msgsize);
  data = (unsigned char *)malloc(msgsize * sizeof(unsigned char));
  bmp_read(fpread, data, msgsize, n_last_bits);
  for (i = 0; i < msgsize; ++i) {
    fprintf(stdout, "%c", data[i]);
  }
  fclose(fpread);
  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   $0x3,-0x8(%rbp)
je     11c1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1329 <main+0x1a9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe60(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1205 <main+0x85>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1329 <main+0x1a9>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
je     1256 <main+0xd6>
cmpl   $0x2,-0x2c(%rbp)
je     1256 <main+0xd6>
cmpl   $0x4,-0x2c(%rbp)
je     1256 <main+0xd6>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x3,-0x4(%rbp)
jmp    1329 <main+0x1a9>
mov    -0x18(%rbp),%rdi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
mov    $0x36,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%ecx
mov    $0x7,%edx
mov    $0x0,%al
call   1050 <bmp_read@plt>
movb   $0x0,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%eax
lea    0xdd5(%rip),%rsi
movzbl %al,%edx
call   1060 <fprintf@plt>
movslq -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    $0x0,%al
call   1050 <bmp_read@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1319 <main+0x199>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rax
movslq -0x1c(%rbp),%rcx
lea    0xd8e(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12de <main+0x15e>
mov    -0x18(%rbp),%rdi
call   1030 <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    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    12a6 <main+0x116>
mov    0x8(%rbx),%rdi
lea    0xe7b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12bf <main+0x12f>
mov    %rax,%r14
mov    0x10(%rbx),%rdi
call   10b0 <atoi@plt>
cmp    $0x4,%eax
ja     12d9 <main+0x149>
mov    %eax,%r15d
mov    $0x16,%eax
bt     %r15d,%eax
jae    12d9 <main+0x149>
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x36,%esi
call   1090 <fseek@plt>
xor    %r12d,%r12d
lea    0x10(%rsp),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x7,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <bmp_read@plt>
movb   $0x0,0x18(%rsp)
mov    %rbx,%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
movzbl %bpl,%edx
lea    0xe0a(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %r14,0x8(%rsp)
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebp,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <bmp_read@plt>
test   %ebp,%ebp
jle    129a <main+0x10a>
mov    %ebp,%ebp
xor    %ebx,%ebx
mov    0x2d5b(%rip),%r14
lea    0xde2(%rip),%r15
nopl   0x0(%rax)
mov    (%r14),%edi
movzbl 0x0(%r13,%rbx,1),%edx
mov    %r15,%rsi
call   1070 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1280 <main+0xf0>
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
jmp    12f3 <main+0x163>
mov    (%rbx),%rsi
lea    0xd50(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r12d
jmp    12f3 <main+0x163>
mov    0x8(%rbx),%rsi
lea    0xd68(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%r12d
jmp    12f3 <main+0x163>
lea    0xd81(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0x3,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [48 x i8] c"usage: %s <in file> <n last bits> > <out file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"error opening file: %s\0A\00"
@.str.3 = [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 0
@.str.4 = [20 x i8] c"msg size: %d bytes\0A\00"
@stdout = global i32 0
@.str.5 = [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 i32
%9 = alloca [8 x i8]
%10 = alloca i32
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %91
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 2, i32* %3
br label %91
30: ; preds = %19
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %47
37: ; preds = %30
%38 = load i32, i32* %10
%39 = icmp ne i32 %38, 2
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i32, i32* %10
%42 = icmp ne i32 %41, 4
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32*, i32** %6
%46 = call i32 @fclose(i32* %45)
store i32 3, i32* %3
br label %91
47: ; preds = %40, %37, %30
%48 = load i32*, i32** %6
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @fseek(i32* %48, i32 54, i32 %49)
%51 = load i32*, i32** %6
%52 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%53 = load i32, i32* %10
%54 = call i32 (i32*, ...) @bmp_read(i32* %51, i8* %52, i32 7, i32 %53)
%55 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 8
store i8 0, i8* %55
%56 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%57 = call i32 @atoi(i8* %56)
store i32 %57, i32* %8
%58 = load i32, i32* @stderr
%59 = load i32, i32* %8
%60 = trunc i32 %59 to i8
%61 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %60)
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = mul i64 %63, 1
%65 = trunc i64 %64 to i32
%66 = call i64 @malloc(i32 %65)
%67 = inttoptr i64 %66 to i8*
store i8* %67, i8** %11
%68 = load i32*, i32** %6
%69 = load i8*, i8** %11
%70 = load i32, i32* %8
%71 = load i32, i32* %10
%72 = call i32 (i32*, ...) @bmp_read(i32* %68, i8* %69, i32 %70, i32 %71)
store i32 0, i32* %7
br label %73
73: ; preds = %85, %47
%74 = load i32, i32* %7
%75 = load i32, i32* %8
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %88
77: ; preds = %73
%78 = load i32, i32* @stdout
%79 = load i8*, i8** %11
%80 = load i32, i32* %7
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %79, i64 %81
%83 = load i8, i8* %82
%84 = call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8 zeroext %83)
br label %85
85: ; preds = %77
%86 = load i32, i32* %7
%87 = add nsw i32 %86, 1
store i32 %87, i32* %7
br label %73
88: ; preds = %73
%89 = load i32*, i32** %6
%90 = call i32 @fclose(i32* %89)
store i32 0, i32* %3
br label %91
91: ; preds = %88, %43, %25, %14
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [48 x i8] c"usage: %s <in file> <n last bits> > <out file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"error opening file: %s\0A\00"
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.4 = [20 x i8] c"msg size: %d bytes\0A\00"
@stdout = global i32 0
@.str.5 = [3 x i8] c"%c\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 [8 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 3
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %49
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i8*, i8** %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %15)
br label %49
17: ; preds = %9
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
switch i32 %20, label %21 [
i32 4, label %24
i32 2, label %24
i32 1, label %24
]
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @fclose(i32* %12)
br label %49
24: ; preds = %17, %17, %17
%25 = load i32, i32* @SEEK_SET
%26 = tail call i32 @fseek(i32* %12, i32 54, i32 %25)
%27 = call i32 (i32*, ...) @bmp_read(i32* %12, i8* %4, i32 7, i32 %20)
%28 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 8
store i8 0, i8* %28
%29 = call i32 @atoi(i8* %4)
%30 = load i32, i32* @stderr
%31 = trunc i32 %29 to i8
%32 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %31)
%33 = call i64 @malloc(i32 %29)
%34 = inttoptr i64 %33 to i8*
%35 = call i32 (i32*, ...) @bmp_read(i32* %12, i8* %34, i32 %29, i32 %20)
%36 = icmp sgt i32 %29, 0
br i1 %36, label %37, label %47
37: ; preds = %24
%38 = zext i32 %29 to i64
br label %39
39: ; preds = %37, %39
%40 = phi i64 [ 0, %37 ], [ %45, %39 ]
%41 = load i32, i32* @stdout
%42 = getelementptr inbounds i8, i8* %34, i64 %40
%43 = load i8, i8* %42
%44 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8 zeroext %43)
%45 = add nuw nsw i64 %40, 1
%46 = icmp eq i64 %45, %38
br i1 %46, label %47, label %39
47: ; preds = %39, %24
%48 = call i32 @fclose(i32* %12)
br label %49
49: ; preds = %47, %21, %14, %6
%50 = phi i32 [ 1, %6 ], [ 2, %14 ], [ 3, %21 ], [ 0, %47 ]
ret i32 %50
}