#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 /*<<< orphan*/ SEEK_SET;
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int libbch_decode_sec(struct bch_control *, int /*<<< orphan*/ *,
                      int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(void *, int /*<<< orphan*/ *, int);
int offset(int, int, int);
int /*<<< orphan*/ printf(char *, int, int);
int sector_read(FILE *fp, void *buff, int sec_num, int nand_page_size,
                struct bch_control *bch) {
  int ret;
  int file_offset = offset(sec_num, nand_page_size, 1);
  uint8_t inbuf[(512 + 3 + 13)];
  uint8_t outbuf[(512 + 3 + 13)];
  if (fp == NULL)
    return -1;
  fseek(fp, file_offset, SEEK_SET);
  ret = fread(inbuf, 1, (512 + 3 + 13), fp);
  if (ret != (512 + 3 + 13)) {
    return -2;
  }
  ret = libbch_decode_sec(bch, inbuf, outbuf);
  if (ret) {
    printf("LIBBCH Data %d error(s) in sector %d\n", ret, sec_num);
  }
  memcpy(buff, outbuf, 512);
  return ret;
}


x86 assembly (clang, -O0):
<sector_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    $0x1,%edx
call   1080 <offset@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <sector_read+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <sector_read+0xe3>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
lea    -0x870(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x210,%edx
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x210,-0x2c(%rbp)
je     11f2 <sector_read+0x92>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1243 <sector_read+0xe3>
mov    -0x28(%rbp),%rdi
lea    -0x870(%rbp),%rsi
lea    -0x10b0(%rbp),%rdx
call   1040 <libbch_decode_sec@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1228 <sector_read+0xc8>
mov    -0x2c(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xddd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x10b0(%rbp),%rsi
mov    $0x200,%edx
call   1060 <memcpy@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sector_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1088,%rsp
mov    %r8,%r15
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %edx,%edi
mov    %ecx,%esi
mov    $0x1,%edx
call   1080 <offset@plt>
test   %rbx,%rbx
je     11ff <sector_read+0x9f>
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <fseek@plt>
lea    0x840(%rsp),%rdi
mov    $0x1,%esi
mov    $0x210,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    $0xfffffffe,%ebx
cmp    $0x210,%eax
jne    1204 <sector_read+0xa4>
lea    0x840(%rsp),%rsi
mov    %rsp,%rdx
mov    %r15,%rdi
call   1040 <libbch_decode_sec@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11ed <sector_read+0x8d>
lea    0xe1c(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x200,%edx
call   1060 <memcpy@plt>
jmp    1204 <sector_read+0xa4>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x1088,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bch_control = type { i32 }
@SEEK_SET = global i32 0
@.str = [38 x i8] c"LIBBCH Data %d error(s) in sector %d\0A\00"
define i32 @sector_read(i32* %0, i8* %1, i32 %2, i32 %3, %struct.bch_control* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.bch_control*
%12 = alloca i32
%13 = alloca i32
%14 = alloca [528 x i32]
%15 = alloca [528 x i32]
store i32* %0, i32** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store %struct.bch_control* %4, %struct.bch_control** %11
%16 = load i32, i32* %9
%17 = load i32, i32* %10
%18 = call i32 @offset(i32 %16, i32 %17, i32 1)
store i32 %18, i32* %13
%19 = load i32*, i32** %7
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %5
store i32 -1, i32* %6
br label %49
22: ; preds = %5
%23 = load i32*, i32** %7
%24 = load i32, i32* %13
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %23, i32 %24, i32 %25)
%27 = getelementptr inbounds [528 x i32], [528 x i32]* %14, i64 0, i64 0
%28 = load i32*, i32** %7
%29 = call i32 @fread(i32* %27, i32 1, i32 528, i32* %28)
store i32 %29, i32* %12
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 528
br i1 %31, label %32, label %33
32: ; preds = %22
store i32 -2, i32* %6
br label %49
33: ; preds = %22
%34 = load %struct.bch_control*, %struct.bch_control** %11
%35 = getelementptr inbounds [528 x i32], [528 x i32]* %14, i64 0, i64 0
%36 = getelementptr inbounds [528 x i32], [528 x i32]* %15, i64 0, i64 0
%37 = call i32 @libbch_decode_sec(%struct.bch_control* %34, i32* %35, i32* %36)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = load i32, i32* %12
%42 = load i32, i32* %9
%43 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %41, i32 %42)
br label %44
44: ; preds = %40, %33
%45 = load i8*, i8** %8
%46 = getelementptr inbounds [528 x i32], [528 x i32]* %15, i64 0, i64 0
%47 = call i32 @memcpy(i8* %45, i32* %46, i32 512)
%48 = load i32, i32* %12
store i32 %48, i32* %6
br label %49
49: ; preds = %44, %32, %21
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
%struct.bch_control = type { i32 }
@SEEK_SET = global i32 0
@.str = [38 x i8] c"LIBBCH Data %d error(s) in sector %d\0A\00"
define i32 @sector_read(i32* %0, i8* %1, i32 %2, i32 %3, %struct.bch_control* %4) {
%6 = alloca [528 x i32]
%7 = alloca [528 x i32]
%8 = tail call i32 @offset(i32 %2, i32 %3, i32 1)
%9 = bitcast [528 x i32]* %6 to i8*
%10 = bitcast [528 x i32]* %7 to i8*
%11 = icmp eq i32* %0, null
br i1 %11, label %26, label %12
12: ; preds = %5
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %0, i32 %8, i32 %13)
%15 = getelementptr inbounds [528 x i32], [528 x i32]* %6, i64 0, i64 0
%16 = call i32 @fread(i32* %15, i32 1, i32 528, i32* %0)
%17 = icmp eq i32 %16, 528
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = getelementptr inbounds [528 x i32], [528 x i32]* %7, i64 0, i64 0
%20 = call i32 @libbch_decode_sec(%struct.bch_control* %4, i32* %15, i32* %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %20, i32 %2)
br label %24
24: ; preds = %22, %18
%25 = call i32 @memcpy(i8* %1, i32* %19, i32 512)
br label %26
26: ; preds = %12, %5, %24
%27 = phi i32 [ %20, %24 ], [ -1, %5 ], [ -2, %12 ]
ret i32 %27
}