#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;
scalar_t__ bufferedReadWrite(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                             unsigned long);
int /*<<< orphan*/ convertNB2BMP(int /*<<< orphan*/ *, char *, int, int,
                                 unsigned long, unsigned long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fseek(int /*<<< orphan*/ *, unsigned long, int /*<<< orphan*/);
char *getSectionName(unsigned long);
scalar_t__ isSectionImage(unsigned long);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int, char *, unsigned long);
int /*<<< orphan*/ stderr;
int extractNB(FILE *input, int index, unsigned long type, unsigned long start,
              unsigned long len) {
  int retval = 0;
  int biWidth;
  int biHeight;
  unsigned long dataLen;
  char filename[1024];
  FILE *output;
  sprintf(filename, "%02d_%s_0x%03lx.nb", index, getSectionName(type), type);
  printf("[] Extracting: %s\n", filename);
  output = fopen(filename, "wb");
  if (output == NULL)
    fprintf(stderr, "[!!] Could not open '%s'\n", filename);
  if (fseek(input, start, SEEK_SET) != 0)
    fprintf(stderr, "[!!] Could not seek to position %08lX\n", start);
  if (bufferedReadWrite(input, output, len))
    retval = 1;
  else
    fprintf(stderr, "[!!] Could not read complete file\n");
  fclose(output);
  if (isSectionImage(type)) {
    switch (len) {
    case 262144:
    case 153600:
      biWidth = 240;
      biHeight = 320;
      break;
    case 655360:
    case 614912:
      biWidth = 480;
      biHeight = 640;
      break;
    default:
      fprintf(stderr, "[!!] Could not determine NB image Width and Height.\n");
      return 0;
      break;
    }
    dataLen = biWidth * biHeight * 2;
    convertNB2BMP(input, filename, biWidth, biHeight, dataLen, start);
  }
  return retval;
}


x86 assembly (clang, -O0):
<extractNB>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x450(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x45c(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <getSectionName@plt>
mov    -0x468(%rbp),%rdi
mov    -0x45c(%rbp),%edx
mov    %rax,%rcx
mov    -0x20(%rbp),%r8
lea    0xe00(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x450(%rbp),%rsi
lea    0xe00(%rip),%rdi
call   1060 <printf@plt>
lea    -0x450(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x458(%rbp)
cmpq   $0x0,-0x458(%rbp)
jne    125e <extractNB+0xbe>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    -0x450(%rbp),%rdx
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2d6b(%rip),%rax
mov    (%rax),%edx
call   1090 <fseek@plt>
cmp    $0x0,%rax
je     1299 <extractNB+0xf9>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdb1(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x458(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10c0 <bufferedReadWrite@plt>
cmp    $0x0,%rax
je     12c3 <extractNB+0x123>
movl   $0x1,-0x34(%rbp)
jmp    12da <extractNB+0x13a>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x458(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <isSectionImage@plt>
cmp    $0x0,%rax
je     13d4 <extractNB+0x234>
mov    -0x30(%rbp),%rax
mov    %rax,-0x470(%rbp)
sub    $0x25800,%rax
je     135d <extractNB+0x1bd>
jmp    1315 <extractNB+0x175>
mov    -0x470(%rbp),%rax
sub    $0x40000,%rax
je     135d <extractNB+0x1bd>
jmp    132d <extractNB+0x18d>
mov    -0x470(%rbp),%rax
sub    $0x96200,%rax
je     1370 <extractNB+0x1d0>
jmp    1345 <extractNB+0x1a5>
mov    -0x470(%rbp),%rax
sub    $0xa0000,%rax
je     1370 <extractNB+0x1d0>
jmp    1383 <extractNB+0x1e3>
movl   $0xf0,-0x38(%rbp)
movl   $0x140,-0x3c(%rbp)
jmp    13a6 <extractNB+0x206>
movl   $0x1e0,-0x38(%rbp)
movl   $0x280,-0x3c(%rbp)
jmp    13a6 <extractNB+0x206>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
lea    0xcfa(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13da <extractNB+0x23a>
mov    -0x38(%rbp),%eax
imul   -0x3c(%rbp),%eax
shl    $0x1,%eax
cltq
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x450(%rbp),%rsi
mov    -0x38(%rbp),%edx
mov    -0x3c(%rbp),%ecx
mov    -0x48(%rbp),%r8
mov    -0x28(%rbp),%r9
call   1070 <convertNB2BMP@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extractNB>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %r8,%r12
mov    %rcx,%r13
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
mov    %rdx,%rdi
call   1030 <getSectionName@plt>
lea    0xe32(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
mov    %rax,%rcx
mov    %r14,%r8
call   10b0 <sprintf@plt>
lea    0xe2b(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
lea    0xe2f(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1221 <extractNB+0x81>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r13,%rsi
call   1090 <fseek@plt>
test   %rax,%rax
je     1254 <extractNB+0xb4>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r13,%rbx
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r12,%rdx
call   10c0 <bufferedReadWrite@plt>
mov    $0x1,%r13d
test   %rax,%rax
jne    128a <extractNB+0xea>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
xor    %r13d,%r13d
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1040 <isSectionImage@plt>
test   %rax,%rax
je     1316 <extractNB+0x176>
cmp    $0x961ff,%r12
jg     12c6 <extractNB+0x126>
mov    $0x140,%ecx
mov    $0xf0,%edx
cmp    $0x25800,%r12
je     12e2 <extractNB+0x142>
cmp    $0x40000,%r12
je     12e2 <extractNB+0x142>
jmp    12fc <extractNB+0x15c>
cmp    $0xa0000,%r12
je     12d8 <extractNB+0x138>
cmp    $0x96200,%r12
jne    12fc <extractNB+0x15c>
mov    $0x280,%ecx
mov    $0x1e0,%edx
mov    %edx,%r8d
imul   %ecx,%r8d
add    %r8d,%r8d
mov    %rsp,%rsi
mov    %r15,%rdi
mov    %rbx,%r9
call   1070 <convertNB2BMP@plt>
jmp    1316 <extractNB+0x176>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
xor    %r13d,%r13d
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r13d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"%02d_%s_0x%03lx.nb\00"
@.str.1 = [19 x i8] c"[] Extracting: %s\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.3 = [26 x i8] c"[!!] Could not open '%s'\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [39 x i8] c"[!!] Could not seek to position %08lX\0A\00"
@.str.5 = [35 x i8] c"[!!] Could not read complete file\0A\00"
@.str.6 = [53 x i8] c"[!!] Could not determine NB image Width and Height.\0A\00"
define i32 @extractNB(i32* %0, i32 %1, i64 %2, i64 %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca [1024 x i8]
%17 = alloca i32*
store i32* %0, i32** %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i64 %4, i64* %11
store i32 0, i32* %12
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%19 = load i32, i32* %8
%20 = load i64, i64* %9
%21 = call i8* @getSectionName(i64 %20)
%22 = load i64, i64* %9
%23 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %19, i8* %21, i64 %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %27, i32** %17
%28 = load i32*, i32** %17
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %5
%31 = load i32, i32* @stderr
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %30, %5
%35 = load i32*, i32** %7
%36 = load i64, i64* %10
%37 = load i32, i32* @SEEK_SET
%38 = call i64 @fseek(i32* %35, i64 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* @stderr
%42 = load i64, i64* %10
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %42)
br label %44
44: ; preds = %40, %34
%45 = load i32*, i32** %7
%46 = load i32*, i32** %17
%47 = load i64, i64* %11
%48 = call i64 @bufferedReadWrite(i32* %45, i32* %46, i64 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %44
store i32 1, i32* %12
br label %54
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %51, %50
%55 = load i32*, i32** %17
%56 = call i32 @fclose(i32* %55)
%57 = load i64, i64* %9
%58 = call i64 @isSectionImage(i64 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %80
60: ; preds = %54
%61 = load i64, i64* %11
switch i64 %61, label %64 [
i64 262144, label %62
i64 153600, label %62
i64 655360, label %63
i64 614912, label %63
]
62: ; preds = %60, %60
store i32 240, i32* %13
store i32 320, i32* %14
br label %67
63: ; preds = %60, %60
store i32 480, i32* %13
store i32 640, i32* %14
br label %67
64: ; preds = %60
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %6
br label %82
67: ; preds = %63, %62
%68 = load i32, i32* %13
%69 = load i32, i32* %14
%70 = mul nsw i32 %68, %69
%71 = mul nsw i32 %70, 2
%72 = sext i32 %71 to i64
store i64 %72, i64* %15
%73 = load i32*, i32** %7
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%75 = load i32, i32* %13
%76 = load i32, i32* %14
%77 = load i64, i64* %15
%78 = load i64, i64* %10
%79 = call i32 @convertNB2BMP(i32* %73, i8* %74, i32 %75, i32 %76, i64 %77, i64 %78)
br label %80
80: ; preds = %67, %54
%81 = load i32, i32* %12
store i32 %81, i32* %6
br label %82
82: ; preds = %80, %64
%83 = load i32, i32* %6
ret i32 %83
}

LLVM IR (-O2):
@.str = [19 x i8] c"%02d_%s_0x%03lx.nb\00"
@.str.1 = [19 x i8] c"[] Extracting: %s\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.3 = [26 x i8] c"[!!] Could not open '%s'\0A\00"
@SEEK_SET = global i32 0
@.str.4 = [39 x i8] c"[!!] Could not seek to position %08lX\0A\00"
@.str.5 = [35 x i8] c"[!!] Could not read complete file\0A\00"
@.str.6 = [53 x i8] c"[!!] Could not determine NB image Width and Height.\0A\00"
define i32 @extractNB(i32* %0, i32 %1, i64 %2, i64 %3, i64 %4) {
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%8 = tail call i8* @getSectionName(i64 %2)
%9 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %1, i8* %8, i64 %2)
%10 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%11 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %5
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %16
16: ; preds = %13, %5
%17 = load i32, i32* @SEEK_SET
%18 = call i64 @fseek(i32* %0, i64 %3, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i64 %3)
br label %23
23: ; preds = %20, %16
%24 = call i64 @bufferedReadWrite(i32* %0, i32* %11, i64 %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %29
29: ; preds = %23, %26
%30 = phi i32 [ 0, %26 ], [ 1, %23 ]
%31 = call i32 @fclose(i32* %11)
%32 = call i64 @isSectionImage(i64 %2)
%33 = icmp eq i64 %32, 0
br i1 %33, label %46, label %34
34: ; preds = %29
switch i64 %4, label %36 [
i64 262144, label %39
i64 153600, label %39
i64 655360, label %35
i64 614912, label %35
]
35: ; preds = %34, %34
br label %39
36: ; preds = %34
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.6, i64 0, i64 0))
br label %46
39: ; preds = %34, %34, %35
%40 = phi i32 [ 480, %35 ], [ 240, %34 ], [ 240, %34 ]
%41 = phi i32 [ 640, %35 ], [ 320, %34 ], [ 320, %34 ]
%42 = shl nuw nsw i32 %40, 1
%43 = mul nuw nsw i32 %42, %41
%44 = zext i32 %43 to i64
%45 = call i32 @convertNB2BMP(i32* %0, i8* %7, i32 %40, i32 %41, i64 %44, i64 %3)
br label %46
46: ; preds = %29, %39, %36
%47 = phi i32 [ 0, %36 ], [ %30, %39 ], [ %30, %29 ]
ret i32 %47
}