#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*/ O_RDONLY;
scalar_t__ create_file_tpef(int, int, int, int /*<<< orphan*/,
                            int /*<<< orphan*/, int /*<<< orphan*/,
                            int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int power_up_iso(int, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int read_binary(int, int, char, char *);
scalar_t__ select_file(int, int);
scalar_t__ send_num_mask(int, char *);
int /*<<< orphan*/ serial_deinit(int);
int serial_init(char *);
scalar_t__ update_binary(int, int, int, char *);
scalar_t__ verify_PIN(int, char *);
int main(int argc, char *argv[]) {
  int fd, fds, atrlen, i, j, len, scode_len;
  char mask[100], atr[100], data[300];
  if (argc != 2) {
    printf("Usage: %s <sCode file name>\n", argv[0]);
    exit(-1);
  }
  if ((fds = open(argv[1], O_RDONLY)) < 0) {
    perror("Unable to open sCode file");
    return -1;
  }
  fd = serial_init("/dev/smartpoll");
  if (send_num_mask(fd, mask) < 0) {
    perror("send_num_mask failed");
    return -1;
  }
  printf("Mask is : %s\n", mask);
  if ((atrlen = power_up_iso(fd, atr)) < 0) {
    perror("power_up_iso failed");
    return -1;
  }
  printf("ATR: ");
  for (i = 0; i < atrlen; i++)
    printf("%x ", (unsigned char)atr[i]);
  printf("\n");
  if (select_file(fd, 0x3f00) < 0) {
    printf("Select file failed\n");
    return -1;
  }
  printf("Selected file 0x3f00\n");
  data[0] = 0x47;
  data[1] = 0x46;
  data[2] = 0x55;
  data[3] = 0x39;
  data[4] = 0x38;
  data[5] = 0x49;
  data[6] = 0x90;
  data[7] = 0x63;
  if (verify_PIN(fd, data) < 0)
    return -1;
  printf("Sucessfully verified the PIN for the master file\n");
  if (create_file_tpef(fd, 0x2030, 200, 0, 0, 0, 0) < 0) {
    printf("Create file failed\n");
    return -1;
  }
  printf("Created a file 0x2030 of size 200 bytes\n");
  if (select_file(fd, 0x2030) < 0) {
    printf("Select file failed\n");
    return -1;
  }
  printf("Selected file 0x2030\n");
  scode_len = read(fds, data + 1, 200);
  if (scode_len < 0) {
    perror("Unable to read scode_len");
    return -1;
  }
  data[scode_len + 1] = 0;
  data[0] = scode_len;
  printf("scode_len=%d, sCode=%s\n", scode_len, data);
  for (i = 0, j = scode_len + 1; j > 0;) {
    if (update_binary(fd, i, (j >= 20) ? 20 : j, data + i) < 0)
      return -1;
    if (j >= 20) {
      j -= 20;
      i += 20;
    } else
      break;
  }
  printf("Wrote to file 0x2030\n");
  if ((len = read_binary(fd, 0, 1, data)) < 0) {
    printf("read binary failed\n");
    return -1;
  }
  if (data[0] != scode_len) {
    printf("Stored data size does not match data!!\n");
    return -1;
  }
  if ((len = read_binary(fd, 1, data[0], data + 1)) < 0) {
    printf("read binary failed\n");
    return -1;
  }
  printf("Data (after write) (%d): ", len);
  for (i = 0; i < len; i++)
    printf("%x ", (unsigned char)data[i + 1]);
  printf("\n");
  serial_deinit(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1222 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    125c <main+0x7c>
lea    0xdd2(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
lea    0xdd4(%rip),%rdi
call   1100 <serial_init@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
call   1070 <send_num_mask@plt>
cmp    $0x0,%rax
jge    129c <main+0xbc>
lea    0xdbb(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
lea    -0x90(%rbp),%rsi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x100(%rbp),%rsi
call   1060 <power_up_iso@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12e4 <main+0x104>
lea    0xd96(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    132d <main+0x14d>
movslq -0x20(%rbp),%rax
movzbl -0x100(%rbp,%rax,1),%esi
ff
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12f9 <main+0x119>
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x3f00,%esi
call   1090 <select_file@plt>
cmp    $0x0,%rax
jge    136c <main+0x18c>
lea    0xd2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movb   $0x47,-0x230(%rbp)
movb   $0x46,-0x22f(%rbp)
movb   $0x55,-0x22e(%rbp)
movb   $0x39,-0x22d(%rbp)
movb   $0x38,-0x22c(%rbp)
movb   $0x49,-0x22b(%rbp)
movb   $0x90,-0x22a(%rbp)
movb   $0x63,-0x229(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
call   1080 <verify_PIN@plt>
cmp    $0x0,%rax
jge    13d7 <main+0x1f7>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
lea    0xcd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x2030,%esi
mov    $0xc8,%edx
xor    %r9d,%r9d
mov    %r9d,%ecx
mov    %r9d,%r8d
movl   $0x0,(%rsp)
call   10f0 <create_file_tpef@plt>
cmp    $0x0,%rax
jge    142b <main+0x24b>
lea    0xccb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x2030,%esi
call   1090 <select_file@plt>
cmp    $0x0,%rax
jge    146a <main+0x28a>
lea    0xc30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
lea    0xcaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x230(%rbp),%rsi
add    $0x1,%rsi
mov    $0xc8,%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    14b5 <main+0x2d5>
lea    0xc92(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x230(%rbp,%rax,1)
mov    -0x2c(%rbp),%eax
mov    %al,-0x230(%rbp)
mov    -0x2c(%rbp),%esi
lea    -0x230(%rbp),%rdx
lea    0xc70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    159b <main+0x3bb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x238(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x234(%rbp)
cmpl   $0x14,-0x24(%rbp)
jl     152c <main+0x34c>
mov    $0x14,%eax
mov    %eax,-0x23c(%rbp)
jmp    1535 <main+0x355>
mov    -0x24(%rbp),%eax
mov    %eax,-0x23c(%rbp)
mov    -0x234(%rbp),%esi
mov    -0x238(%rbp),%edi
mov    -0x23c(%rbp),%edx
lea    -0x230(%rbp),%rcx
movslq -0x20(%rbp),%rax
add    %rax,%rcx
call   10e0 <update_binary@plt>
cmp    $0x0,%rax
jge    1570 <main+0x390>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
cmpl   $0x14,-0x24(%rbp)
jl     1591 <main+0x3b1>
mov    -0x24(%rbp),%eax
sub    $0x14,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0x20(%rbp)
jmp    1596 <main+0x3b6>
jmp    159b <main+0x3bb>
jmp    14f6 <main+0x316>
lea    0xbc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rcx
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <read_binary@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    15e5 <main+0x405>
lea    0xbab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
movsbl -0x230(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     160f <main+0x42f>
lea    0xb95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
mov    -0x14(%rbp),%edi
mov    -0x230(%rbp),%al
lea    -0x230(%rbp),%rcx
add    $0x1,%rcx
mov    $0x1,%esi
movsbl %al,%edx
call   1030 <read_binary@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1656 <main+0x476>
lea    0xb3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16bc <main+0x4dc>
mov    -0x28(%rbp),%esi
lea    0xb59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    16a6 <main+0x4c6>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cltq
movzbl -0x230(%rbp,%rax,1),%esi
ff
lea    0x9f2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    166e <main+0x48e>
lea    0xb0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   10a0 <serial_deinit@plt>
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
cmp    $0x2,%edi
jne    1549 <main+0x349>
mov    0x8(%rsi),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
test   %eax,%eax
js     14e6 <main+0x2e6>
mov    %eax,%r13d
lea    0xdf9(%rip),%rdi
call   1120 <serial_init@plt>
mov    %eax,%r15d
lea    0x1b0(%rsp),%rsi
mov    %eax,%edi
call   1090 <send_num_mask@plt>
test   %rax,%rax
js     14ef <main+0x2ef>
lea    0xdf6(%rip),%rdi
lea    0x1b0(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x140(%rsp),%rsi
mov    %r15d,%edi
call   1080 <power_up_iso@plt>
test   %eax,%eax
js     14f8 <main+0x2f8>
mov    %eax,%ebp
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
je     12cb <main+0xcb>
mov    %ebp,%ebx
lea    0xdda(%rip),%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl 0x140(%rsp,%rbp,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    12b0 <main+0xb0>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r15d,%edi
mov    $0x3f00,%esi
call   10b0 <select_file@plt>
test   %rax,%rax
js     14bc <main+0x2bc>
lea    0xde0(%rip),%rdi
call   1050 <puts@plt>
movabs $0x6390493839554647,%rax
49 90 63
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%r12
mov    %r15d,%edi
mov    %r12,%rsi
call   10a0 <verify_PIN@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     150a <main+0x30a>
lea    0xdbb(%rip),%rdi
call   1050 <puts@plt>
movl   $0x0,(%rsp)
mov    %r15d,%edi
mov    $0x2030,%esi
mov    $0xc8,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1110 <create_file_tpef@plt>
test   %rax,%rax
js     14ca <main+0x2ca>
lea    0xdb6(%rip),%rdi
call   1050 <puts@plt>
mov    %r15d,%edi
mov    $0x2030,%esi
call   10b0 <select_file@plt>
test   %rax,%rax
js     14d8 <main+0x2d8>
lea    0x11(%rsp),%rbx
lea    0xdb7(%rip),%rdi
call   1050 <puts@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc8,%edx
call   1070 <read@plt>
test   %eax,%eax
js     153b <main+0x33b>
mov    %eax,%r13d
mov    %eax,%eax
movb   $0x0,0x1(%rax,%r12,1)
mov    %r13b,0x10(%rsp)
lea    0xce3(%rip),%rdi
xor    %ebp,%ebp
mov    %r13d,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x15(%r13),%ebx
mov    $0x14,%r12d
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffec,%ebx
cmp    $0x14,%ebx
mov    %ebx,%edx
cmovae %r12d,%edx
lea    (%rsp,%rbp,1),%rcx
add    $0x10,%rcx
mov    %r15d,%edi
mov    %ebp,%esi
call   1100 <update_binary@plt>
test   %rax,%rax
js     150a <main+0x30a>
cmp    $0x14,%ebx
jb     1415 <main+0x215>
add    $0x14,%rbp
cmp    $0x14,%ebx
jg     13e0 <main+0x1e0>
lea    0xd39(%rip),%rdi
call   1050 <puts@plt>
lea    0x10(%rsp),%rcx
mov    %r15d,%edi
xor    %esi,%esi
mov    $0x1,%edx
call   1030 <read_binary@plt>
test   %eax,%eax
js     151f <main+0x31f>
movsbl 0x10(%rsp),%eax
cmp    %eax,%r13d
jne    152d <main+0x32d>
movsbl %al,%edx
mov    %r15d,%edi
mov    $0x1,%esi
lea    0x11(%rsp),%rcx
call   1030 <read_binary@plt>
test   %eax,%eax
js     151f <main+0x31f>
mov    %eax,%ebp
lea    0xc47(%rip),%rdi
xor    %r14d,%r14d
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebp,%ebp
je     14a8 <main+0x2a8>
mov    %ebp,%ebx
lea    0xbf9(%rip),%r12
xor    %ebp,%ebp
nopl   0x0(%rax)
movzbl 0x11(%rsp,%rbp,1),%esi
add    $0x1,%rbp
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %rbp,%rbx
jne    1490 <main+0x290>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r15d,%edi
call   10c0 <serial_deinit@plt>
jmp    150a <main+0x30a>
lea    0xcf4(%rip),%rdi
call   1050 <puts@plt>
jmp    1504 <main+0x304>
lea    0xcd3(%rip),%rdi
call   1050 <puts@plt>
jmp    150a <main+0x30a>
lea    0xcd8(%rip),%rdi
call   1050 <puts@plt>
jmp    150a <main+0x30a>
lea    0xb30(%rip),%rdi
jmp    14ff <main+0x2ff>
lea    0xb50(%rip),%rdi
jmp    14ff <main+0x2ff>
lea    0xb6a(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc6b(%rip),%rdi
call   1050 <puts@plt>
jmp    150a <main+0x30a>
lea    0xc36(%rip),%rdi
call   1050 <puts@plt>
jmp    150a <main+0x30a>
lea    0xb45(%rip),%rdi
call   10e0 <perror@plt>
jmp    150a <main+0x30a>
mov    (%rsi),%rsi
lea    0xaad(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Usage: %s <sCode file name>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Unable to open sCode file\00"
@.str.2 = [15 x i8] c"/dev/smartpoll\00"
@.str.3 = [21 x i8] c"send_num_mask failed\00"
@.str.4 = [14 x i8] c"Mask is : %s\0A\00"
@.str.5 = [20 x i8] c"power_up_iso failed\00"
@.str.6 = [6 x i8] c"ATR: \00"
@.str.7 = [4 x i8] c"%x \00"
@.str.8 = [2 x i8] c"\0A\00"
@.str.9 = [20 x i8] c"Select file failed\0A\00"
@.str.10 = [22 x i8] c"Selected file 0x3f00\0A\00"
@.str.11 = [50 x i8] c"Sucessfully verified the PIN for the master file\0A\00"
@.str.12 = [20 x i8] c"Create file failed\0A\00"
@.str.13 = [41 x i8] c"Created a file 0x2030 of size 200 bytes\0A\00"
@.str.14 = [22 x i8] c"Selected file 0x2030\0A\00"
@.str.15 = [25 x i8] c"Unable to read scode_len\00"
@.str.16 = [24 x i8] c"scode_len=%d, sCode=%s\0A\00"
@.str.17 = [22 x i8] c"Wrote to file 0x2030\0A\00"
@.str.18 = [20 x i8] c"read binary failed\0A\00"
@.str.19 = [40 x i8] c"Stored data size does not match data!!\0A\00"
@.str.20 = [26 x i8] c"Data (after write) (%d): \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 i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [100 x i8]
%14 = alloca [100 x i8]
%15 = alloca [300 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
33: ; preds = %24
%34 = call i32 @serial_init(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%37 = call i64 @send_num_mask(i32 %35, i8* %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
41: ; preds = %33
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %6
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%46 = call i32 @power_up_iso(i32 %44, i8* %45)
store i32 %46, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
50: ; preds = %41
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %9
br label %52
52: ; preds = %63, %50
%53 = load i32, i32* %9
%54 = load i32, i32* %8
%55 = icmp slt i32 %53, %54
br i1 %55, label %56, label %66
56: ; preds = %52
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 %58
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %61)
br label %63
63: ; preds = %56
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %52
66: ; preds = %52
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%68 = load i32, i32* %6
%69 = call i64 @select_file(i32 %68, i32 16128)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
73: ; preds = %66
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.10, i64 0, i64 0))
%75 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
store i8 71, i8* %75
%76 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 1
store i8 70, i8* %76
%77 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 2
store i8 85, i8* %77
%78 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 3
store i8 57, i8* %78
%79 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 4
store i8 56, i8* %79
%80 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 5
store i8 73, i8* %80
%81 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 6
store i8 -112, i8* %81
%82 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 7
store i8 99, i8* %82
%83 = load i32, i32* %6
%84 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%85 = call i64 @verify_PIN(i32 %83, i8* %84)
%86 = icmp slt i64 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %73
store i32 -1, i32* %3
br label %203
88: ; preds = %73
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.11, i64 0, i64 0))
%90 = load i32, i32* %6
%91 = call i64 @create_file_tpef(i32 %90, i32 8240, i32 200, i32 0, i32 0, i32 0, i32 0)
%92 = icmp slt i64 %91, 0
br i1 %92, label %93, label %95
93: ; preds = %88
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.12, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
95: ; preds = %88
%96 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.13, i64 0, i64 0))
%97 = load i32, i32* %6
%98 = call i64 @select_file(i32 %97, i32 8240)
%99 = icmp slt i64 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %95
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
102: ; preds = %95
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.14, i64 0, i64 0))
%104 = load i32, i32* %7
%105 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%106 = getelementptr inbounds i8, i8* %105, i64 1
%107 = call i32 @read(i32 %104, i8* %106, i32 200)
store i32 %107, i32* %12
%108 = load i32, i32* %12
%109 = icmp slt i32 %108, 0
br i1 %109, label %110, label %112
110: ; preds = %102
%111 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.15, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
112: ; preds = %102
%113 = load i32, i32* %12
%114 = add nsw i32 %113, 1
%115 = sext i32 %114 to i64
%116 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 %115
store i8 0, i8* %116
%117 = load i32, i32* %12
%118 = trunc i32 %117 to i8
%119 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
store i8 %118, i8* %119
%120 = load i32, i32* %12
%121 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%122 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.16, i64 0, i64 0), i32 %120, i8* %121)
store i32 0, i32* %9
%123 = load i32, i32* %12
%124 = add nsw i32 %123, 1
store i32 %124, i32* %10
br label %125
125: ; preds = %154, %112
%126 = load i32, i32* %10
%127 = icmp sgt i32 %126, 0
br i1 %127, label %128, label %155
128: ; preds = %125
%129 = load i32, i32* %6
%130 = load i32, i32* %9
%131 = load i32, i32* %10
%132 = icmp sge i32 %131, 20
br i1 %132, label %133, label %134
133: ; preds = %128
br label %136
134: ; preds = %128
%135 = load i32, i32* %10
br label %136
136: ; preds = %134, %133
%137 = phi i32 [ 20, %133 ], [ %135, %134 ]
%138 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%139 = load i32, i32* %9
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds i8, i8* %138, i64 %140
%142 = call i64 @update_binary(i32 %129, i32 %130, i32 %137, i8* %141)
%143 = icmp slt i64 %142, 0
br i1 %143, label %144, label %145
144: ; preds = %136
store i32 -1, i32* %3
br label %203
145: ; preds = %136
%146 = load i32, i32* %10
%147 = icmp sge i32 %146, 20
br i1 %147, label %148, label %153
148: ; preds = %145
%149 = load i32, i32* %10
%150 = sub nsw i32 %149, 20
store i32 %150, i32* %10
%151 = load i32, i32* %9
%152 = add nsw i32 %151, 20
store i32 %152, i32* %9
br label %154
153: ; preds = %145
br label %155
154: ; preds = %148
br label %125
155: ; preds = %153, %125
%156 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.17, i64 0, i64 0))
%157 = load i32, i32* %6
%158 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%159 = call i32 @read_binary(i32 %157, i32 0, i8 signext 1, i8* %158)
store i32 %159, i32* %11
%160 = icmp slt i32 %159, 0
br i1 %160, label %161, label %163
161: ; preds = %155
%162 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.18, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
163: ; preds = %155
%164 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%165 = load i8, i8* %164
%166 = sext i8 %165 to i32
%167 = load i32, i32* %12
%168 = icmp ne i32 %166, %167
br i1 %168, label %169, label %171
169: ; preds = %163
%170 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.19, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
171: ; preds = %163
%172 = load i32, i32* %6
%173 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%174 = load i8, i8* %173
%175 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 0
%176 = getelementptr inbounds i8, i8* %175, i64 1
%177 = call i32 @read_binary(i32 %172, i32 1, i8 signext %174, i8* %176)
store i32 %177, i32* %11
%178 = icmp slt i32 %177, 0
br i1 %178, label %179, label %181
179: ; preds = %171
%180 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.18, i64 0, i64 0))
store i32 -1, i32* %3
br label %203
181: ; preds = %171
%182 = load i32, i32* %11
%183 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.20, i64 0, i64 0), i32 %182)
store i32 0, i32* %9
br label %184
184: ; preds = %196, %181
%185 = load i32, i32* %9
%186 = load i32, i32* %11
%187 = icmp slt i32 %185, %186
br i1 %187, label %188, label %199
188: ; preds = %184
%189 = load i32, i32* %9
%190 = add nsw i32 %189, 1
%191 = sext i32 %190 to i64
%192 = getelementptr inbounds [300 x i8], [300 x i8]* %15, i64 0, i64 %191
%193 = load i8, i8* %192
%194 = zext i8 %193 to i32
%195 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %194)
br label %196
196: ; preds = %188
%197 = load i32, i32* %9
%198 = add nsw i32 %197, 1
store i32 %198, i32* %9
br label %184
199: ; preds = %184
%200 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%201 = load i32, i32* %6
%202 = call i32 @serial_deinit(i32 %201)
br label %203
203: ; preds = %199, %179, %169, %161, %144, %110, %100, %93, %87, %71, %48, %39, %31
%204 = load i32, i32* %3
ret i32 %204
}

LLVM IR (-O2):
@.str = [29 x i8] c"Usage: %s <sCode file name>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Unable to open sCode file\00"
@.str.2 = [15 x i8] c"/dev/smartpoll\00"
@.str.3 = [21 x i8] c"send_num_mask failed\00"
@.str.4 = [14 x i8] c"Mask is : %s\0A\00"
@.str.5 = [20 x i8] c"power_up_iso failed\00"
@.str.6 = [6 x i8] c"ATR: \00"
@.str.7 = [4 x i8] c"%x \00"
@.str.15 = [25 x i8] c"Unable to read scode_len\00"
@.str.16 = [24 x i8] c"scode_len=%d, sCode=%s\0A\00"
@.str.20 = [26 x i8] c"Data (after write) (%d): \00"
@str = [21 x i8] c"Selected file 0x3f00\00"
@str.21 = [49 x i8] c"Sucessfully verified the PIN for the master file\00"
@str.22 = [40 x i8] c"Created a file 0x2030 of size 200 bytes\00"
@str.23 = [21 x i8] c"Selected file 0x2030\00"
@str.24 = [21 x i8] c"Wrote to file 0x2030\00"
@str.26 = [39 x i8] c"Stored data size does not match data!!\00"
@str.27 = [19 x i8] c"read binary failed\00"
@str.29 = [19 x i8] c"Create file failed\00"
@str.30 = [19 x i8] c"Select file failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = alloca [300 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 2
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %135
22: ; preds = %14
%23 = tail call i32 @serial_init(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%24 = call i64 @send_num_mask(i32 %23, i8* %6)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %135
28: ; preds = %22
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%30 = call i32 @power_up_iso(i32 %23, i8* %7)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %135
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%36 = icmp eq i32 %30, 0
br i1 %36, label %47, label %37
37: ; preds = %34
%38 = zext i32 %30 to i64
br label %39
39: ; preds = %37, %39
%40 = phi i64 [ 0, %37 ], [ %45, %39 ]
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %43)
%45 = add nuw nsw i64 %40, 1
%46 = icmp eq i64 %45, %38
br i1 %46, label %47, label %39
47: ; preds = %39, %34
%48 = call i32 @putchar(i32 10)
%49 = call i64 @select_file(i32 %23, i32 16128)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.30, i64 0, i64 0))
br label %135
53: ; preds = %47
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
store i8 71, i8* %8
%55 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 1
store i8 70, i8* %55
%56 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 2
store i8 85, i8* %56
%57 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 3
store i8 57, i8* %57
%58 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 4
store i8 56, i8* %58
%59 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 5
store i8 73, i8* %59
%60 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 6
store i8 -112, i8* %60
%61 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 7
store i8 99, i8* %61
%62 = call i64 @verify_PIN(i32 %23, i8* %8)
%63 = icmp slt i64 %62, 0
br i1 %63, label %135, label %64
64: ; preds = %53
%65 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str.21, i64 0, i64 0))
%66 = call i64 @create_file_tpef(i32 %23, i32 8240, i32 200, i32 0, i32 0, i32 0, i32 0)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %64
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.29, i64 0, i64 0))
br label %135
70: ; preds = %64
%71 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.22, i64 0, i64 0))
%72 = call i64 @select_file(i32 %23, i32 8240)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %70
%75 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.30, i64 0, i64 0))
br label %135
76: ; preds = %70
%77 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.23, i64 0, i64 0))
%78 = call i32 @read(i32 %18, i8* %55, i32 200)
%79 = icmp slt i32 %78, 0
br i1 %79, label %80, label %82
80: ; preds = %76
%81 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.15, i64 0, i64 0))
br label %135
82: ; preds = %76
%83 = add nuw nsw i32 %78, 1
%84 = zext i32 %83 to i64
%85 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 %84
store i8 0, i8* %85
%86 = trunc i32 %78 to i8
store i8 %86, i8* %8
%87 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.16, i64 0, i64 0), i32 %78, i8* %8)
br label %88
88: ; preds = %82, %98
%89 = phi i64 [ 0, %82 ], [ %100, %98 ]
%90 = phi i32 [ %83, %82 ], [ %99, %98 ]
%91 = icmp ugt i32 %90, 19
%92 = select i1 %91, i32 20, i32 %90
%93 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 %89
%94 = trunc i64 %89 to i32
%95 = call i64 @update_binary(i32 %23, i32 %94, i32 %92, i8* %93)
%96 = icmp slt i64 %95, 0
br i1 %96, label %135, label %97
97: ; preds = %88
br i1 %91, label %98, label %102
98: ; preds = %97
%99 = add nsw i32 %90, -20
%100 = add nuw nsw i64 %89, 20
%101 = icmp sgt i32 %90, 20
br i1 %101, label %88, label %102
102: ; preds = %98, %97
%103 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.24, i64 0, i64 0))
%104 = call i32 @read_binary(i32 %23, i32 0, i8 signext 1, i8* %8)
%105 = icmp slt i32 %104, 0
br i1 %105, label %106, label %108
106: ; preds = %102
%107 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.27, i64 0, i64 0))
br label %135
108: ; preds = %102
%109 = load i8, i8* %8
%110 = sext i8 %109 to i32
%111 = icmp eq i32 %78, %110
br i1 %111, label %114, label %112
112: ; preds = %108
%113 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.26, i64 0, i64 0))
br label %135
114: ; preds = %108
%115 = call i32 @read_binary(i32 %23, i32 1, i8 signext %109, i8* %55)
%116 = icmp slt i32 %115, 0
br i1 %116, label %117, label %119
117: ; preds = %114
%118 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.27, i64 0, i64 0))
br label %135
119: ; preds = %114
%120 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.20, i64 0, i64 0), i32 %115)
%121 = icmp eq i32 %115, 0
br i1 %121, label %132, label %122
122: ; preds = %119
%123 = zext i32 %115 to i64
br label %124
124: ; preds = %122, %124
%125 = phi i64 [ 0, %122 ], [ %126, %124 ]
%126 = add nuw nsw i64 %125, 1
%127 = getelementptr inbounds [300 x i8], [300 x i8]* %5, i64 0, i64 %126
%128 = load i8, i8* %127
%129 = zext i8 %128 to i32
%130 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %129)
%131 = icmp eq i64 %126, %123
br i1 %131, label %132, label %124
132: ; preds = %124, %119
%133 = call i32 @putchar(i32 10)
%134 = call i32 @serial_deinit(i32 %23)
br label %135
135: ; preds = %88, %53, %132, %117, %112, %106, %80, %74, %68, %51, %32, %26, %20
%136 = phi i32 [ -1, %20 ], [ -1, %26 ], [ -1, %32 ], [ -1, %51 ], [ -1, %68 ], [ -1, %74 ], [ -1, %80 ], [ -1, %106 ], [ -1, %112 ], [ -1, %117 ], [ 0, %132 ], [ -1, %53 ], [ -1, %88 ]
ret i32 %136
}