#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*/ word_value;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ daemon(int, int);
int /*<<< orphan*/ erase_pot_mtd(char *);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, unsigned int *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ update_to_file(int);
int /*<<< orphan*/ write(int, unsigned int *, int);
int main(int argc, char **argv) {
  int devfd;
  int pot_value;
  int word_len;
  int bitone_len;
  struct timeval timo;
  unsigned int word_value;
  printf("POT is Running...\n");
  daemon(1, 1);
  devfd = open("/dev/mmcblk0p15", O_RDWR);
  if (devfd < 0) {
    printf("Open mtd POT error!\n");
    return -1;
  }
  word_len = -1;
  do {
    word_len++;
    if (word_len >= (2048 / 4)) {
      erase_pot_mtd("/dev/mmcblk0p15");
      word_len = -1;
      continue;
    }
    read(devfd, &word_value, sizeof(word_value));
  } while (word_value == 0x00000000);
  lseek(devfd, -4, SEEK_CUR);
  if (word_value == 0xFFFFFFFF)
    bitone_len = 32;
  else {
    bitone_len = 0;
    do {
      bitone_len++;
      word_value = word_value >> 1;
    } while (word_value);
  }
  pot_value = (word_len * 32) + (32 - bitone_len);
  if (pot_value >= 4320)
    goto fin;
  update_to_file(pot_value);
  for (;;) {
    timo.tv_sec = 1 * 60;
    timo.tv_usec = 1;
    select(1, NULL, NULL, NULL, &timo);
    read(devfd, &word_value, sizeof(word_value));
    lseek(devfd, -4, SEEK_CUR);
    word_value = word_value >> 1;
    write(devfd, &word_value, sizeof(word_value));
    if (word_value != 0x00000000)
      lseek(devfd, -4, SEEK_CUR);
    pot_value++;
    if (pot_value >= 4320)
      goto fin;
    update_to_file(pot_value);
  }
fin:
  close(devfd);
  update_to_file(pot_value);
  printf("POT is Finished!!!\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%esi
mov    %esi,%edi
call   10b0 <daemon@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1208 <main+0x68>
lea    0xe2c(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <main+0x20b>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x200,-0x1c(%rbp)
jl     123d <main+0x9d>
lea    0xde7(%rip),%rdi
call   1060 <erase_pot_mtd@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    124e <main+0xae>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1090 <read@plt>
cmpl   $0x0,-0x2c(%rbp)
je     120f <main+0x6f>
mov    -0x14(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffc,%esi
call   1070 <lseek@plt>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1284 <main+0xe4>
movl   $0x20,-0x20(%rbp)
jmp    12ac <main+0x10c>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    128b <main+0xeb>
jmp    12ac <main+0x10c>
mov    -0x1c(%rbp),%eax
shl    $0x5,%eax
mov    $0x20,%ecx
sub    -0x20(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x10e0,-0x18(%rbp)
jl     12d1 <main+0x131>
jmp    1388 <main+0x1e8>
mov    -0x18(%rbp),%edi
call   1030 <update_to_file@plt>
movl   $0x3c,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   10a0 <select@plt>
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1090 <read@plt>
mov    -0x14(%rbp),%edi
mov    0x2cc6(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffc,%esi
call   1070 <lseek@plt>
mov    -0x2c(%rbp),%eax
shr    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmpl   $0x0,-0x2c(%rbp)
je     1360 <main+0x1c0>
mov    -0x14(%rbp),%edi
mov    0x2c8c(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffc,%esi
call   1070 <lseek@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x10e0,-0x18(%rbp)
jl     137b <main+0x1db>
jmp    1388 <main+0x1e8>
mov    -0x18(%rbp),%edi
call   1030 <update_to_file@plt>
jmp    12d9 <main+0x139>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <update_to_file@plt>
lea    0xc99(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
mov    $0x1,%esi
call   10b0 <daemon@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     1272 <main+0xd2>
mov    %eax,%r13d
mov    $0xffffffff,%r12d
lea    0xe0a(%rip),%r14
lea    0xc(%rsp),%r15
jmp    121e <main+0x7e>
nopl   (%rax)
add    $0x1,%r12d
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1090 <read@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%ebp
test   %ebp,%ebp
jne    123b <main+0x9b>
cmp    $0x1ff,%r12d
jl     1200 <main+0x60>
mov    %r14,%rdi
call   1060 <erase_pot_mtd@plt>
mov    $0xffffffff,%r12d
xor    %ebp,%ebp
test   %ebp,%ebp
je     121e <main+0x7e>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    $0xfffffffc,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%ebp
je     1288 <main+0xe8>
mov    $0x20,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%eax
shr    %ebx
add    $0xffffffff,%ebp
cmp    $0x1,%eax
ja     1260 <main+0xc0>
mov    %ebx,0xc(%rsp)
jmp    128a <main+0xea>
lea    0xdbc(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    1380 <main+0x1e0>
xor    %ebp,%ebp
shl    $0x5,%r12d
lea    (%r12,%rbp,1),%r14d
cmp    $0x10df,%r14d
jg     1362 <main+0x1c2>
mov    %r14d,%edi
call   1030 <update_to_file@plt>
add    %r12d,%ebp
add    $0x1,%ebp
movabs $0x10000003c,%r12
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,0x10(%rsp)
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   10a0 <select@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1090 <read@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    $0xfffffffc,%esi
call   1070 <lseek@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%eax
shr    %eax
mov    %eax,0xc(%rsp)
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1050 <write@plt>
cmp    $0x2,%ebx
jb     1345 <main+0x1a5>
mov    0x2caa(%rip),%rax
mov    (%rax),%edx
mov    %r13d,%edi
mov    $0xfffffffc,%esi
call   1070 <lseek@plt>
cmp    $0x10e0,%ebp
je     135c <main+0x1bc>
mov    %ebp,%edi
call   1030 <update_to_file@plt>
add    $0x1,%ebp
jmp    12d0 <main+0x130>
mov    $0x10e0,%r14d
mov    %r13d,%edi
call   1080 <close@plt>
mov    %r14d,%edi
call   1030 <update_to_file@plt>
lea    0xca9(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [19 x i8] c"POT is Running...\0A\00"
@.str.1 = [16 x i8] c"/dev/mmcblk0p15\00"
@O_RDWR = global i32 0
@.str.2 = [21 x i8] c"Open mtd POT error!\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [20 x i8] c"POT is Finished!!!\0A\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 i32
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @daemon(i32 1, i32 1)
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %97
20: ; preds = %2
store i32 -1, i32* %8
br label %21
21: ; preds = %31, %20
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp sge i32 %24, 512
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @erase_pot_mtd(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %8
br label %31
28: ; preds = %21
%29 = load i32, i32* %6
%30 = call i32 @read(i32 %29, i32* %11, i32 4)
br label %31
31: ; preds = %28, %26
%32 = load i32, i32* %11
%33 = icmp eq i32 %32, 0
br i1 %33, label %21, label %34
34: ; preds = %31
%35 = load i32, i32* %6
%36 = load i32, i32* @SEEK_CUR
%37 = call i32 @lseek(i32 %35, i32 -4, i32 %36)
%38 = load i32, i32* %11
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 32, i32* %9
br label %51
41: ; preds = %34
store i32 0, i32* %9
br label %42
42: ; preds = %47, %41
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
%45 = load i32, i32* %11
%46 = lshr i32 %45, 1
store i32 %46, i32* %11
br label %47
47: ; preds = %42
%48 = load i32, i32* %11
%49 = icmp ne i32 %48, 0
br i1 %49, label %42, label %50
50: ; preds = %47
br label %51
51: ; preds = %50, %40
%52 = load i32, i32* %8
%53 = mul nsw i32 %52, 32
%54 = load i32, i32* %9
%55 = sub nsw i32 32, %54
%56 = add nsw i32 %53, %55
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = icmp sge i32 %57, 4320
br i1 %58, label %59, label %60
59: ; preds = %51
br label %91
60: ; preds = %51
%61 = load i32, i32* %7
%62 = call i32 @update_to_file(i32 %61)
br label %63
63: ; preds = %88, %60
%64 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 0
store i32 60, i32* %64
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
store i32 1, i32* %65
%66 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %10)
%67 = load i32, i32* %6
%68 = call i32 @read(i32 %67, i32* %11, i32 4)
%69 = load i32, i32* %6
%70 = load i32, i32* @SEEK_CUR
%71 = call i32 @lseek(i32 %69, i32 -4, i32 %70)
%72 = load i32, i32* %11
%73 = lshr i32 %72, 1
store i32 %73, i32* %11
%74 = load i32, i32* %6
%75 = call i32 @write(i32 %74, i32* %11, i32 4)
%76 = load i32, i32* %11
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %63
%79 = load i32, i32* %6
%80 = load i32, i32* @SEEK_CUR
%81 = call i32 @lseek(i32 %79, i32 -4, i32 %80)
br label %82
82: ; preds = %78, %63
%83 = load i32, i32* %7
%84 = add nsw i32 %83, 1
store i32 %84, i32* %7
%85 = load i32, i32* %7
%86 = icmp sge i32 %85, 4320
br i1 %86, label %87, label %88
87: ; preds = %82
br label %91
88: ; preds = %82
%89 = load i32, i32* %7
%90 = call i32 @update_to_file(i32 %89)
br label %63
91: ; preds = %87, %59
%92 = load i32, i32* %6
%93 = call i32 @close(i32 %92)
%94 = load i32, i32* %7
%95 = call i32 @update_to_file(i32 %94)
%96 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %97
97: ; preds = %91, %18
%98 = load i32, i32* %3
ret i32 %98
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str.1 = [16 x i8] c"/dev/mmcblk0p15\00"
@O_RDWR = global i32 0
@SEEK_CUR = global i32 0
@str = [18 x i8] c"POT is Running...\00"
@str.4 = [19 x i8] c"POT is Finished!!!\00"
@str.5 = [20 x i8] c"Open mtd POT error!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.timeval* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @daemon(i32 1, i32 1)
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %73
14: ; preds = %2, %24
%15 = phi i32 [ %25, %24 ], [ undef, %2 ]
%16 = phi i32 [ %27, %24 ], [ -1, %2 ]
%17 = icmp sgt i32 %16, 510
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @erase_pot_mtd(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %24
20: ; preds = %14
%21 = add nsw i32 %16, 1
%22 = call i32 @read(i32 %10, i32* %4, i32 4)
%23 = load i32, i32* %4
br label %24
24: ; preds = %20, %18
%25 = phi i32 [ %15, %18 ], [ %23, %20 ]
%26 = phi i32 [ 0, %18 ], [ %23, %20 ]
%27 = phi i32 [ -1, %18 ], [ %21, %20 ]
%28 = icmp eq i32 %26, 0
br i1 %28, label %14, label %29
29: ; preds = %24
%30 = load i32, i32* @SEEK_CUR
%31 = tail call i32 @lseek(i32 %10, i32 -4, i32 %30)
%32 = icmp eq i32 %26, -1
br i1 %32, label %41, label %33
33: ; preds = %29, %33
%34 = phi i32 [ %37, %33 ], [ %25, %29 ]
%35 = phi i32 [ %36, %33 ], [ 0, %29 ]
%36 = add nuw nsw i32 %35, 1
%37 = lshr i32 %34, 1
%38 = icmp ult i32 %34, 2
br i1 %38, label %39, label %33
39: ; preds = %33
store i32 %37, i32* %4
%40 = sub nsw i32 31, %35
br label %41
41: ; preds = %39, %29
%42 = phi i32 [ 0, %29 ], [ %40, %39 ]
%43 = shl nsw i32 %27, 5
%44 = add nsw i32 %42, %43
%45 = icmp sgt i32 %44, 4319
br i1 %45, label %68, label %46
46: ; preds = %41
%47 = tail call i32 @update_to_file(i32 %44)
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
br label %50
50: ; preds = %66, %46
%51 = phi i32 [ %44, %46 ], [ %64, %66 ]
store i32 60, i32* %48
store i32 1, i32* %49
%52 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %3)
%53 = call i32 @read(i32 %10, i32* %4, i32 4)
%54 = load i32, i32* @SEEK_CUR
%55 = call i32 @lseek(i32 %10, i32 -4, i32 %54)
%56 = load i32, i32* %4
%57 = lshr i32 %56, 1
store i32 %57, i32* %4
%58 = call i32 @write(i32 %10, i32* %4, i32 4)
%59 = icmp ult i32 %56, 2
br i1 %59, label %63, label %60
60: ; preds = %50
%61 = load i32, i32* @SEEK_CUR
%62 = call i32 @lseek(i32 %10, i32 -4, i32 %61)
br label %63
63: ; preds = %60, %50
%64 = add i32 %51, 1
%65 = icmp eq i32 %51, 4319
br i1 %65, label %68, label %66
66: ; preds = %63
%67 = call i32 @update_to_file(i32 %64)
br label %50
68: ; preds = %63, %41
%69 = phi i32 [ %44, %41 ], [ 4320, %63 ]
%70 = call i32 @close(i32 %10)
%71 = call i32 @update_to_file(i32 %69)
%72 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
br label %73
73: ; preds = %68, %12
%74 = phi i32 [ -1, %12 ], [ 0, %68 ]
ret i32 %74
}