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

/* Variables and functions */
int /*<<< orphan*/ mdelay(int);
int ov8858_eos_otp_read(int);
int /*<<< orphan*/ ov8858_eos_otp_write(int, int);
int ov8858_eos_check_otp_wb(int index) {
  int flag, i;
  int address_start = 0x7020;
  int address_end = 0x7020;
  ov8858_eos_otp_write(0x3d84, 0xc0);
  ov8858_eos_otp_write(0x3d88, (address_start >> 8));
  ov8858_eos_otp_write(0x3d89, (address_start & 0xff));
  ov8858_eos_otp_write(0x3d8A, (address_end >> 8));
  ov8858_eos_otp_write(0x3d8B, (address_end & 0xff));
  ov8858_eos_otp_write(0x3d81, 0x01);
  mdelay(5);
  flag = ov8858_eos_otp_read(0x7020);
  if (index == 1) {
    flag = (flag >> 6) & 0x03;
  } else if (index == 2) {
    flag = (flag >> 4) & 0x03;
  } else {
    flag = (flag >> 6) & 0x03;
  }
  for (i = address_start; i <= address_end; i++) {
    ov8858_eos_otp_write(i, 0x00);
  }
  if (flag == 0x00) {
    return 0;
  } else if (flag & 0x02) {
    return 1;
  } else {
    return 2;
  }
}


x86 assembly (clang, -O0):
<ov8858_eos_check_otp_wb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x7020,-0x14(%rbp)
movl   $0x7020,-0x18(%rbp)
mov    $0x3d84,%edi
mov    $0xc0,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    -0x14(%rbp),%esi
sar    $0x8,%esi
mov    $0x3d88,%edi
call   1040 <ov8858_eos_otp_write@plt>
mov    -0x14(%rbp),%esi
and    $0xff,%esi
mov    $0x3d89,%edi
call   1040 <ov8858_eos_otp_write@plt>
mov    -0x18(%rbp),%esi
sar    $0x8,%esi
mov    $0x3d8a,%edi
call   1040 <ov8858_eos_otp_write@plt>
mov    -0x18(%rbp),%esi
and    $0xff,%esi
mov    $0x3d8b,%edi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x3d81,%edi
mov    $0x1,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x5,%edi
call   1030 <mdelay@plt>
mov    $0x7020,%edi
call   1050 <ov8858_eos_otp_read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11df <ov8858_eos_check_otp_wb+0xaf>
mov    -0xc(%rbp),%eax
sar    $0x6,%eax
and    $0x3,%eax
mov    %eax,-0xc(%rbp)
jmp    120b <ov8858_eos_check_otp_wb+0xdb>
cmpl   $0x2,-0x8(%rbp)
jne    11fa <ov8858_eos_check_otp_wb+0xca>
mov    -0xc(%rbp),%eax
sar    $0x4,%eax
and    $0x3,%eax
mov    %eax,-0xc(%rbp)
jmp    1206 <ov8858_eos_check_otp_wb+0xd6>
mov    -0xc(%rbp),%eax
sar    $0x6,%eax
and    $0x3,%eax
mov    %eax,-0xc(%rbp)
jmp    120b <ov8858_eos_check_otp_wb+0xdb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1235 <ov8858_eos_check_otp_wb+0x105>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1211 <ov8858_eos_check_otp_wb+0xe1>
cmpl   $0x0,-0xc(%rbp)
jne    124b <ov8858_eos_check_otp_wb+0x11b>
movl   $0x0,-0x4(%rbp)
jmp    126d <ov8858_eos_check_otp_wb+0x13d>
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1266 <ov8858_eos_check_otp_wb+0x136>
movl   $0x1,-0x4(%rbp)
jmp    126d <ov8858_eos_check_otp_wb+0x13d>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ov8858_eos_check_otp_wb>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x3d84,%edi
mov    $0xc0,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x3d88,%edi
mov    $0x70,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x3d89,%edi
mov    $0x20,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x3d8a,%edi
mov    $0x70,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x3d8b,%edi
mov    $0x20,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x3d81,%edi
mov    $0x1,%esi
call   1040 <ov8858_eos_otp_write@plt>
mov    $0x5,%edi
call   1030 <mdelay@plt>
mov    $0x7020,%edi
call   1050 <ov8858_eos_otp_read@plt>
mov    %eax,%ebp
cmp    $0x2,%ebx
je     11b4 <ov8858_eos_check_otp_wb+0x84>
cmp    $0x1,%ebx
jne    11b9 <ov8858_eos_check_otp_wb+0x89>
sar    $0x6,%ebp
jmp    11bc <ov8858_eos_check_otp_wb+0x8c>
sar    $0x4,%ebp
jmp    11bc <ov8858_eos_check_otp_wb+0x8c>
shr    $0x6,%ebp
mov    %ebp,%ebx
and    $0x3,%ebx
mov    $0x7020,%edi
xor    %esi,%esi
call   1040 <ov8858_eos_otp_write@plt>
and    $0x2,%ebp
cmp    $0x1,%ebp
mov    $0x1,%eax
adc    $0x0,%eax
test   %ebx,%ebx
cmove  %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ov8858_eos_check_otp_wb(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 28704, i32* %6
store i32 28704, i32* %7
%8 = call i32 @ov8858_eos_otp_write(i32 15748, i32 192)
%9 = load i32, i32* %6
%10 = ashr i32 %9, 8
%11 = call i32 @ov8858_eos_otp_write(i32 15752, i32 %10)
%12 = load i32, i32* %6
%13 = and i32 %12, 255
%14 = call i32 @ov8858_eos_otp_write(i32 15753, i32 %13)
%15 = load i32, i32* %7
%16 = ashr i32 %15, 8
%17 = call i32 @ov8858_eos_otp_write(i32 15754, i32 %16)
%18 = load i32, i32* %7
%19 = and i32 %18, 255
%20 = call i32 @ov8858_eos_otp_write(i32 15755, i32 %19)
%21 = call i32 @ov8858_eos_otp_write(i32 15745, i32 1)
%22 = call i32 @mdelay(i32 5)
%23 = call i32 @ov8858_eos_otp_read(i32 28704)
store i32 %23, i32* %4
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %1
%27 = load i32, i32* %4
%28 = ashr i32 %27, 6
%29 = and i32 %28, 3
store i32 %29, i32* %4
br label %42
30: ; preds = %1
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, 2
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i32, i32* %4
%35 = ashr i32 %34, 4
%36 = and i32 %35, 3
store i32 %36, i32* %4
br label %41
37: ; preds = %30
%38 = load i32, i32* %4
%39 = ashr i32 %38, 6
%40 = and i32 %39, 3
store i32 %40, i32* %4
br label %41
41: ; preds = %37, %33
br label %42
42: ; preds = %41, %26
%43 = load i32, i32* %6
store i32 %43, i32* %5
br label %44
44: ; preds = %51, %42
%45 = load i32, i32* %5
%46 = load i32, i32* %7
%47 = icmp sle i32 %45, %46
br i1 %47, label %48, label %54
48: ; preds = %44
%49 = load i32, i32* %5
%50 = call i32 @ov8858_eos_otp_write(i32 %49, i32 0)
br label %51
51: ; preds = %48
%52 = load i32, i32* %5
%53 = add nsw i32 %52, 1
store i32 %53, i32* %5
br label %44
54: ; preds = %44
%55 = load i32, i32* %4
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 0, i32* %2
br label %64
58: ; preds = %54
%59 = load i32, i32* %4
%60 = and i32 %59, 2
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 1, i32* %2
br label %64
63: ; preds = %58
store i32 2, i32* %2
br label %64
64: ; preds = %63, %62, %57
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
define i32 @ov8858_eos_check_otp_wb(i32 %0) {
%2 = tail call i32 @ov8858_eos_otp_write(i32 15748, i32 192)
%3 = tail call i32 @ov8858_eos_otp_write(i32 15752, i32 112)
%4 = tail call i32 @ov8858_eos_otp_write(i32 15753, i32 32)
%5 = tail call i32 @ov8858_eos_otp_write(i32 15754, i32 112)
%6 = tail call i32 @ov8858_eos_otp_write(i32 15755, i32 32)
%7 = tail call i32 @ov8858_eos_otp_write(i32 15745, i32 1)
%8 = tail call i32 @mdelay(i32 5)
%9 = tail call i32 @ov8858_eos_otp_read(i32 28704)
switch i32 %0, label %14 [
i32 1, label %10
i32 2, label %12
]
10: ; preds = %1
%11 = ashr i32 %9, 6
br label %16
12: ; preds = %1
%13 = ashr i32 %9, 4
br label %16
14: ; preds = %1
%15 = lshr i32 %9, 6
br label %16
16: ; preds = %12, %14, %10
%17 = phi i32 [ %11, %10 ], [ %13, %12 ], [ %15, %14 ]
%18 = and i32 %17, 3
%19 = tail call i32 @ov8858_eos_otp_write(i32 28704, i32 0)
%20 = icmp eq i32 %18, 0
%21 = and i32 %17, 2
%22 = icmp eq i32 %21, 0
%23 = select i1 %22, i32 2, i32 1
%24 = select i1 %20, i32 0, i32 %23
ret i32 %24
}