#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 uint8_t;
typedef int /*<<< orphan*/ oobbuf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EINVAL;
int EIO;
int /*<<< orphan*/ __err_msg(char *, ...);
scalar_t__ ferror(int /*<<< orphan*/ *);
size_t fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int *, int, int);
int /*<<< orphan*/ nand_calculate_ecc(int *, int *);
int process_page(uint8_t *buf, size_t pagesize, FILE *fp_data, FILE *fp_oob,
                 size_t *written) {
  int eccpoi, oobsize;
  size_t i;
  uint8_t oobbuf[64];
  memset(oobbuf, 0xff, sizeof(oobbuf));
  switch (pagesize) {
  case 2048:
    oobsize = 64;
    eccpoi = 64 / 2;
    break;
  case 512:
    oobsize = 16;
    eccpoi = 16 / 2;
    break;
  default:
    do {
      ;
      __err_msg("Unsupported page size: %d\n", pagesize);
    } while (0);
    return -EINVAL;
  }
  for (i = 0; i < pagesize; i += 256, eccpoi += 3) {
    oobbuf[eccpoi++] = 0x0;
    nand_calculate_ecc(&buf[i], &oobbuf[eccpoi]);
  }
  *written += fwrite(buf, 1, pagesize, fp_data);
  if (fp_oob) {
    i = fwrite(oobbuf, 1, oobsize, fp_oob);
    if (ferror(fp_oob)) {
      do {
        ;
        __err_msg("IO error\n");
      } while (0);
      return -EIO;
    }
  } else {
    i = fwrite(oobbuf, 1, oobsize, fp_data);
    if (ferror(fp_data)) {
      do {
        ;
        __err_msg("IO error\n");
      } while (0);
      return -EIO;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<process_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x140(%rbp),%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x148(%rbp)
sub    $0x200,%rax
je     11cc <process_page+0x7c>
jmp    11a1 <process_page+0x51>
mov    -0x148(%rbp),%rax
sub    $0x800,%rax
jne    11df <process_page+0x8f>
jmp    11b9 <process_page+0x69>
movl   $0x40,-0x38(%rbp)
movl   $0x20,-0x34(%rbp)
jmp    1209 <process_page+0xb9>
movl   $0x10,-0x38(%rbp)
movl   $0x8,-0x34(%rbp)
jmp    1209 <process_page+0xb9>
jmp    11e4 <process_page+0x94>
mov    -0x18(%rbp),%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <__err_msg@plt>
xor    %eax,%eax
mov    0x2dd9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1364 <process_page+0x214>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1279 <process_page+0x129>
mov    -0x34(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x34(%rbp)
cltq
movl   $0x0,-0x140(%rbp,%rax,4)
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
movslq -0x34(%rbp),%rax
lea    -0x140(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
call   1060 <nand_calculate_ecc@plt>
mov    -0x40(%rbp),%rax
add    $0x100,%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x34(%rbp)
jmp    1211 <process_page+0xc1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     1303 <process_page+0x1b3>
lea    -0x140(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12fe <process_page+0x1ae>
jmp    12dd <process_page+0x18d>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1050 <__err_msg@plt>
xor    %eax,%eax
mov    0x2cec(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1364 <process_page+0x214>
jmp    135d <process_page+0x20d>
lea    -0x140(%rbp),%rdi
mov    -0x38(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1358 <process_page+0x208>
jmp    1337 <process_page+0x1e7>
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1050 <__err_msg@plt>
xor    %eax,%eax
mov    0x2c92(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1364 <process_page+0x214>
jmp    135d <process_page+0x20d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_page>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %r8,%r14
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x20(%rsp),%rdi
mov    $0xff,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
cmp    $0x800,%rbx
je     11b3 <process_page+0x63>
cmp    $0x200,%rbx
jne    1249 <process_page+0xf9>
mov    %r14,0x10(%rsp)
mov    %rbp,0x18(%rsp)
movl   $0x10,0xc(%rsp)
mov    $0x8,%eax
jmp    11ca <process_page+0x7a>
mov    %r14,0x10(%rsp)
mov    %rbp,0x18(%rsp)
movl   $0x40,0xc(%rsp)
mov    $0x20,%eax
lea    (%rsp,%rax,4),%r13
add    $0x24,%r13
xor    %r14d,%r14d
mov    %r15,%rbp
nopl   0x0(%rax,%rax,1)
movl   $0x0,-0x4(%r13)
mov    %rbp,%rdi
mov    %r13,%rsi
call   1060 <nand_calculate_ecc@plt>
add    $0x100,%r14
add    $0x10,%r13
add    $0x400,%rbp
cmp    %rbx,%r14
jb     11e0 <process_page+0x90>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    0x18(%rsp),%rbx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    0x10(%rsp),%rcx
add    %rax,(%rcx)
lea    0x20(%rsp),%rdi
mov    $0x1,%esi
test   %r12,%r12
je     1265 <process_page+0x115>
mov    0xc(%rsp),%edx
mov    %r12,%rcx
call   1070 <fwrite@plt>
mov    %r12,%rdi
jmp    1274 <process_page+0x124>
lea    0xdb0(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <__err_msg@plt>
mov    0x2d75(%rip),%rax
jmp    1295 <process_page+0x145>
mov    0xc(%rsp),%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1299 <process_page+0x149>
lea    0xd96(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <__err_msg@plt>
mov    0x2d4b(%rip),%rax
sub    (%rax),%ebp
jmp    129b <process_page+0x14b>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Unsupported page size: %d\0A\00"
@EINVAL = global i32 0
@.str.1 = [10 x i8] c"IO error\0A\00"
@EIO = global i32 0
define i32 @process_page(i32* %0, i64 %1, i32* %2, i32* %3, i64* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i64*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca [64 x i32]
store i32* %0, i32** %7
store i64 %1, i64* %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i64* %4, i64** %11
%16 = getelementptr inbounds [64 x i32], [64 x i32]* %15, i64 0, i64 0
%17 = call i32 @memset(i32* %16, i32 255, i32 256)
%18 = load i64, i64* %8
switch i64 %18, label %21 [
i64 2048, label %19
i64 512, label %20
]
19: ; preds = %5
store i32 64, i32* %13
store i32 32, i32* %12
br label %28
20: ; preds = %5
store i32 16, i32* %13
store i32 8, i32* %12
br label %28
21: ; preds = %5
br label %22
22: ; preds = %21
%23 = load i64, i64* %8
%24 = call i32 (i8*, ...) @__err_msg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* @EINVAL
%27 = sub nsw i32 0, %26
store i32 %27, i32* %6
br label %92
28: ; preds = %20, %19
store i64 0, i64* %14
br label %29
29: ; preds = %45, %28
%30 = load i64, i64* %14
%31 = load i64, i64* %8
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %50
33: ; preds = %29
%34 = load i32, i32* %12
%35 = add nsw i32 %34, 1
store i32 %35, i32* %12
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds [64 x i32], [64 x i32]* %15, i64 0, i64 %36
store i32 0, i32* %37
%38 = load i32*, i32** %7
%39 = load i64, i64* %14
%40 = getelementptr inbounds i32, i32* %38, i64 %39
%41 = load i32, i32* %12
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [64 x i32], [64 x i32]* %15, i64 0, i64 %42
%44 = call i32 @nand_calculate_ecc(i32* %40, i32* %43)
br label %45
45: ; preds = %33
%46 = load i64, i64* %14
%47 = add i64 %46, 256
store i64 %47, i64* %14
%48 = load i32, i32* %12
%49 = add nsw i32 %48, 3
store i32 %49, i32* %12
br label %29
50: ; preds = %29
%51 = load i32*, i32** %7
%52 = load i64, i64* %8
%53 = trunc i64 %52 to i32
%54 = load i32*, i32** %9
%55 = call i64 @fwrite(i32* %51, i32 1, i32 %53, i32* %54)
%56 = load i64*, i64** %11
%57 = load i64, i64* %56
%58 = add i64 %57, %55
store i64 %58, i64* %56
%59 = load i32*, i32** %10
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %76
61: ; preds = %50
%62 = getelementptr inbounds [64 x i32], [64 x i32]* %15, i64 0, i64 0
%63 = load i32, i32* %13
%64 = load i32*, i32** %10
%65 = call i64 @fwrite(i32* %62, i32 1, i32 %63, i32* %64)
store i64 %65, i64* %14
%66 = load i32*, i32** %10
%67 = call i64 @ferror(i32* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %61
br label %70
70: ; preds = %69
%71 = call i32 (i8*, ...) @__err_msg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %72
72: ; preds = %70
%73 = load i32, i32* @EIO
%74 = sub nsw i32 0, %73
store i32 %74, i32* %6
br label %92
75: ; preds = %61
br label %91
76: ; preds = %50
%77 = getelementptr inbounds [64 x i32], [64 x i32]* %15, i64 0, i64 0
%78 = load i32, i32* %13
%79 = load i32*, i32** %9
%80 = call i64 @fwrite(i32* %77, i32 1, i32 %78, i32* %79)
store i64 %80, i64* %14
%81 = load i32*, i32** %9
%82 = call i64 @ferror(i32* %81)
%83 = icmp ne i64 %82, 0
br i1 %83, label %84, label %90
84: ; preds = %76
br label %85
85: ; preds = %84
%86 = call i32 (i8*, ...) @__err_msg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %87
87: ; preds = %85
%88 = load i32, i32* @EIO
%89 = sub nsw i32 0, %88
store i32 %89, i32* %6
br label %92
90: ; preds = %76
br label %91
91: ; preds = %90, %75
store i32 0, i32* %6
br label %92
92: ; preds = %91, %87, %72, %25
%93 = load i32, i32* %6
ret i32 %93
}

LLVM IR (-O2):
@.str = [27 x i8] c"Unsupported page size: %d\0A\00"
@EINVAL = global i32 0
@.str.1 = [10 x i8] c"IO error\0A\00"
@EIO = global i32 0
define i32 @process_page(i32* %0, i64 %1, i32* nocapture %2, i32* %3, i64* nocapture %4) {
%6 = alloca [64 x i32]
%7 = bitcast [64 x i32]* %6 to i8*
%8 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 0
%9 = call i32 @memset(i32* %8, i32 255, i32 256)
switch i64 %1, label %11 [
i64 2048, label %15
i64 512, label %10
]
10: ; preds = %5
br label %15
11: ; preds = %5
%12 = call i32 (i8*, ...) @__err_msg(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %1)
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %51
15: ; preds = %10, %5
%16 = phi i64 [ 8, %10 ], [ 32, %5 ]
%17 = phi i32 [ 16, %10 ], [ 64, %5 ]
br label %18
18: ; preds = %15, %18
%19 = phi i64 [ %16, %15 ], [ %27, %18 ]
%20 = phi i64 [ 0, %15 ], [ %26, %18 ]
%21 = or i64 %19, 1
%22 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 %19
store i32 0, i32* %22
%23 = getelementptr inbounds i32, i32* %0, i64 %20
%24 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 %21
%25 = call i32 @nand_calculate_ecc(i32* %23, i32* %24)
%26 = add i64 %20, 256
%27 = add nuw i64 %19, 4
%28 = icmp ult i64 %26, %1
br i1 %28, label %18, label %29
29: ; preds = %18
%30 = trunc i64 %1 to i32
%31 = call i64 @fwrite(i32* %0, i32 1, i32 %30, i32* %2)
%32 = load i64, i64* %4
%33 = add i64 %32, %31
store i64 %33, i64* %4
%34 = icmp eq i32* %3, null
br i1 %34, label %43, label %35
35: ; preds = %29
%36 = call i64 @fwrite(i32* %8, i32 1, i32 %17, i32* %3)
%37 = call i64 @ferror(i32* %3)
%38 = icmp eq i64 %37, 0
br i1 %38, label %51, label %39
39: ; preds = %35
%40 = call i32 (i8*, ...) @__err_msg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%41 = load i32, i32* @EIO
%42 = sub nsw i32 0, %41
br label %51
43: ; preds = %29
%44 = call i64 @fwrite(i32* %8, i32 1, i32 %17, i32* %2)
%45 = call i64 @ferror(i32* %2)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %43
%48 = call i32 (i8*, ...) @__err_msg(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* @EIO
%50 = sub nsw i32 0, %49
br label %51
51: ; preds = %35, %43, %47, %39, %11
%52 = phi i32 [ %14, %11 ], [ %42, %39 ], [ %50, %47 ], [ 0, %43 ], [ 0, %35 ]
ret i32 %52
}