#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*/ uint8_t;
struct bch_control {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ encode_page(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ free_bch(struct bch_control *);
int /*<<< orphan*/ fwrite(void *, int, int, int /*<<< orphan*/ *);
struct bch_control *init_bch(int, int, int);
void *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sector_read(int /*<<< orphan*/ *, char *, int /*<<< orphan*/,
                               int, struct bch_control *);
int main(int argc, char **argv) {
  FILE *ifp, *ofp;
  void *obuf;
  int i, size, sector, num_sectors, nand_page_size;
  char *infile, *outfile, *ptr;
  struct bch_control *bch;
  if (argc < 6) {
    printf("Usage: %s infile outfile start_sector num_sectors nand_page_size\n",
           argv[0]);
    return 0;
  }
  infile = argv[1];
  outfile = argv[2];
  sector = atoi(argv[3]);
  num_sectors = atoi(argv[4]);
  nand_page_size = atoi(argv[5]);
  size = 512 * num_sectors;
  obuf = malloc(size);
  if (obuf == NULL) {
    printf("Error allocating %d bytes of buffer\n", size);
    return -1;
  }
  ifp = fopen(infile, "rb");
  if (ifp == NULL) {
    printf("Cannot open %s file\n", infile);
    free(obuf);
    return -2;
  }
  ofp = fopen(outfile, "wb");
  if (ifp == NULL) {
    printf("Cannot open %s file\n", outfile);
    fclose(ifp);
    free(obuf);
    return -3;
  }
  bch = init_bch(13, 8, 0x25af);
  ptr = (char *)obuf;
  for (i = 0; i < num_sectors; i++) {
    sector_read(ifp, ptr, sector++, nand_page_size, bch);
    encode_page((uint8_t *)ptr, (uint8_t *)ptr, 512);
    ptr += 512;
  }
  fwrite(obuf, 1, size, ofp);
  fclose(ifp);
  fclose(ofp);
  free(obuf);
  free_bch(bch);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x6,-0x8(%rbp)
jge    11f1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13cb <main+0x21b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
shl    $0x9,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1276 <main+0xc6>
mov    -0x30(%rbp),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cb <main+0x21b>
mov    -0x48(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12bc <main+0x10c>
mov    -0x48(%rbp),%rsi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13cb <main+0x21b>
mov    -0x50(%rbp),%rdi
lea    0xdb8(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    130b <main+0x15b>
mov    -0x50(%rbp),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    13cb <main+0x21b>
mov    $0xd,%edi
mov    $0x8,%esi
mov    $0x25af,%edx
call   1050 <init_bch@plt>
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    138b <main+0x1db>
mov    -0x18(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x34(%rbp),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x60(%rbp),%r8
call   10a0 <sector_read@plt>
mov    -0x58(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    $0x200,%edx
call   10d0 <encode_page@plt>
mov    -0x58(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x58(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1332 <main+0x182>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x60(%rbp),%rdi
call   1080 <free_bch@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%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,%r15
cmp    $0x5,%edi
jg     11de <main+0x2e>
mov    (%r15),%rsi
lea    0xe30(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1342 <main+0x192>
mov    0x8(%r15),%r14
mov    0x10(%r15),%rax
mov    %rax,0x8(%rsp)
mov    0x18(%r15),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
mov    0x20(%r15),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
mov    0x28(%r15),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r13d
mov    %ebp,%r12d
shl    $0x9,%r12d
mov    %r12d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     130c <main+0x15c>
mov    %rax,%r15
lea    0xe38(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1324 <main+0x174>
mov    %r12d,0x14(%rsp)
mov    %r15,0x18(%rsp)
lea    0xe2e(%rip),%rsi
mov    %rax,0x20(%rsp)
mov    0x8(%rsp),%rdi
call   1090 <fopen@plt>
mov    %rax,0x8(%rsp)
mov    $0xd,%edi
mov    $0x8,%esi
mov    $0x25af,%edx
call   1050 <init_bch@plt>
mov    %rax,%r14
test   %ebp,%ebp
jle    12c8 <main+0x118>
mov    0x18(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rbx),%r12d
mov    0x20(%rsp),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %r13d,%ecx
mov    %r14,%r8
call   10a0 <sector_read@plt>
mov    %r15,%rdi
mov    %r15,%rsi
mov    $0x200,%edx
call   10d0 <encode_page@plt>
add    $0x200,%r15
mov    %r12d,%ebx
add    $0xffffffff,%ebp
jne    1290 <main+0xe0>
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    0x14(%rsp),%edx
mov    0x8(%rsp),%rbp
mov    %rbp,%rcx
call   10c0 <fwrite@plt>
mov    0x20(%rsp),%rdi
call   1040 <fclose@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1080 <free_bch@plt>
xor    %ebx,%ebx
jmp    1342 <main+0x192>
lea    0xd2f(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1342 <main+0x192>
lea    0xd3f(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bch_control = type { i32 }
@.str = [66 x i8] c"Usage: %s infile outfile start_sector num_sectors nand_page_size\0A\00"
@.str.1 = [37 x i8] c"Error allocating %d bytes of buffer\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [21 x i8] c"Cannot open %s file\0A\00"
@.str.4 = [3 x i8] c"wb\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 i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca %struct.bch_control*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 6
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 0, i32* %3
br label %113
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %14
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
store i8* %31, i8** %15
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %11
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 4
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %12
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 5
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %13
%44 = load i32, i32* %12
%45 = mul nsw i32 512, %44
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = call i8* @malloc(i32 %46)
store i8* %47, i8** %8
%48 = load i8*, i8** %8
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %53
50: ; preds = %25
%51 = load i32, i32* %10
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %51)
store i32 -1, i32* %3
br label %113
53: ; preds = %25
%54 = load i8*, i8** %14
%55 = call i32* @fopen(i8* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %55, i32** %6
%56 = load i32*, i32** %6
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %63
58: ; preds = %53
%59 = load i8*, i8** %14
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %59)
%61 = load i8*, i8** %8
%62 = call i32 @free(i8* %61)
store i32 -2, i32* %3
br label %113
63: ; preds = %53
%64 = load i8*, i8** %15
%65 = call i32* @fopen(i8* %64, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %65, i32** %7
%66 = load i32*, i32** %6
%67 = icmp eq i32* %66, null
br i1 %67, label %68, label %75
68: ; preds = %63
%69 = load i8*, i8** %15
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %69)
%71 = load i32*, i32** %6
%72 = call i32 @fclose(i32* %71)
%73 = load i8*, i8** %8
%74 = call i32 @free(i8* %73)
store i32 -3, i32* %3
br label %113
75: ; preds = %63
%76 = call %struct.bch_control* @init_bch(i32 13, i32 8, i32 9647)
store %struct.bch_control* %76, %struct.bch_control** %17
%77 = load i8*, i8** %8
store i8* %77, i8** %16
store i32 0, i32* %9
br label %78
78: ; preds = %97, %75
%79 = load i32, i32* %9
%80 = load i32, i32* %12
%81 = icmp slt i32 %79, %80
br i1 %81, label %82, label %100
82: ; preds = %78
%83 = load i32*, i32** %6
%84 = load i8*, i8** %16
%85 = load i32, i32* %11
%86 = add nsw i32 %85, 1
store i32 %86, i32* %11
%87 = load i32, i32* %13
%88 = load %struct.bch_control*, %struct.bch_control** %17
%89 = call i32 @sector_read(i32* %83, i8* %84, i32 %85, i32 %87, %struct.bch_control* %88)
%90 = load i8*, i8** %16
%91 = bitcast i8* %90 to i32*
%92 = load i8*, i8** %16
%93 = bitcast i8* %92 to i32*
%94 = call i32 @encode_page(i32* %91, i32* %93, i32 512)
%95 = load i8*, i8** %16
%96 = getelementptr inbounds i8, i8* %95, i64 512
store i8* %96, i8** %16
br label %97
97: ; preds = %82
%98 = load i32, i32* %9
%99 = add nsw i32 %98, 1
store i32 %99, i32* %9
br label %78
100: ; preds = %78
%101 = load i8*, i8** %8
%102 = load i32, i32* %10
%103 = load i32*, i32** %7
%104 = call i32 @fwrite(i8* %101, i32 1, i32 %102, i32* %103)
%105 = load i32*, i32** %6
%106 = call i32 @fclose(i32* %105)
%107 = load i32*, i32** %7
%108 = call i32 @fclose(i32* %107)
%109 = load i8*, i8** %8
%110 = call i32 @free(i8* %109)
%111 = load %struct.bch_control*, %struct.bch_control** %17
%112 = call i32 @free_bch(%struct.bch_control* %111)
store i32 0, i32* %3
br label %113
113: ; preds = %100, %68, %58, %50, %20
%114 = load i32, i32* %3
ret i32 %114
}

LLVM IR (-O2):
%struct.bch_control = type { i32 }
@.str = [66 x i8] c"Usage: %s infile outfile start_sector num_sectors nand_page_size\0A\00"
@.str.1 = [37 x i8] c"Error allocating %d bytes of buffer\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [21 x i8] c"Cannot open %s file\0A\00"
@.str.4 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 6
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %53
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 3
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 4
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 5
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = shl nsw i32 %17, 9
%22 = tail call i8* @malloc(i32 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %7
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %53
26: ; preds = %7
%27 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%31 = tail call i32 @free(i8* %22)
br label %53
32: ; preds = %26
%33 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%34 = tail call %struct.bch_control* @init_bch(i32 13, i32 8, i32 9647)
%35 = icmp sgt i32 %17, 0
br i1 %35, label %36, label %47
36: ; preds = %32, %36
%37 = phi i8* [ %44, %36 ], [ %22, %32 ]
%38 = phi i32 [ %45, %36 ], [ 0, %32 ]
%39 = phi i32 [ %40, %36 ], [ %14, %32 ]
%40 = add nsw i32 %39, 1
%41 = tail call i32 @sector_read(i32* %27, i8* %37, i32 %39, i32 %20, %struct.bch_control* %34)
%42 = bitcast i8* %37 to i32*
%43 = tail call i32 @encode_page(i32* %42, i32* %42, i32 512)
%44 = getelementptr inbounds i8, i8* %37, i64 512
%45 = add nuw nsw i32 %38, 1
%46 = icmp eq i32 %45, %17
br i1 %46, label %47, label %36
47: ; preds = %36, %32
%48 = tail call i32 @fwrite(i8* %22, i32 1, i32 %21, i32* %33)
%49 = tail call i32 @fclose(i32* %27)
%50 = tail call i32 @fclose(i32* %33)
%51 = tail call i32 @free(i8* %22)
%52 = tail call i32 @free_bch(%struct.bch_control* %34)
br label %53
53: ; preds = %47, %29, %24, %4
%54 = phi i32 [ 0, %4 ], [ -1, %24 ], [ -2, %29 ], [ 0, %47 ]
ret i32 %54
}