#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;
struct option {
  char *member_0;
  char member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
};
typedef int int8_t;
typedef int int32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(int /*<<< orphan*/);
int ch341Configure(int, int);
int ch341EraseChip();
int ch341ReadStatus();
int /*<<< orphan*/ ch341Release();
int ch341SetStream(int);
int ch341SpiCapacity();
int ch341SpiRead(int *, int /*<<< orphan*/, int);
int ch341SpiWrite(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int getopt_long(int, char **, char *, struct option const *, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ no_argument;
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ required_argument;
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int32_t ret;
  uint8_t *buf;
  FILE *fp;
  char *filename;
  int cap;
  int length = 0;
  char op = 0;
  int8_t c;
  const char usage[] =
      "\nUsage:\n"
      " -h, --help             display this message\n"
      " -i, --info             read the chip ID info\n"
      " -e, --erase            erase the entire chip\n"
      " -l, --length <bytes>   manually set length\n"
      " -w, --write <filename> write chip with data from filename\n"
      " -r, --read <filename>  read chip and save data to filename\n";
  const struct option options[] = {{"help", no_argument, 0, 'h'},
                                   {"erase", no_argument, 0, 'e'},
                                   {"write", required_argument, 0, 'w'},
                                   {"length", required_argument, 0, 'l'},
                                   {"read", required_argument, 0, 'r'},
                                   {0, 0, 0, 0}};
  int32_t optidx = 0;
  while ((c = getopt_long(argc, argv, "hiew:r:l:", options, &optidx)) != -1) {
    switch (c) {
    case 'i':
    case 'e':
      if (!op)
        op = c;
      else
        op = 'x';
      break;
    case 'w':
    case 'r':
      if (!op) {
        op = c;
        filename = (char *)malloc(strlen(optarg) + 1);
        strcpy(filename, optarg);
      } else
        op = 'x';
      break;
    case 'l':
      length = atoi(optarg);
      break;
    default:
      printf("%s\n", usage);
      return 0;
    }
  }
  if (op == 0) {
    fprintf(stderr, "%s\n", usage);
    return 0;
  }
  if (op == 'x') {
    fprintf(stderr, "Conflicting options, only one option at a time.\n");
    return -1;
  }
  ret = ch341Configure(0x1A86, 0x5512);
  if (ret < 0)
    return -1;
  ret = ch341SetStream(0x01);
  if (ret < 0)
    goto out;
  ret = ch341SpiCapacity();
  if (ret < 0)
    goto out;
  cap = 1 << ret;
  printf("Chip capacity is %d\n", cap);
  if (length != 0) {
    cap = length;
  }
  if (op == 'i')
    goto out;
  if (op == 'e') {
    uint8_t timeout = 0;
    ret = ch341EraseChip();
    if (ret < 0)
      goto out;
    do {
      sleep(1);
      ret = ch341ReadStatus();
      if (ret < 0)
        goto out;
      printf(".");
      fflush(stdout);
      timeout++;
      if (timeout == 100)
        break;
    } while (ret != 0);
    if (timeout == 100)
      fprintf(stderr, "Chip erase timeout.\n");
    else
      printf("Chip erase done!\n");
  }
  if ((op == 'r') || (op == 'w')) {
    buf = (uint8_t *)malloc(cap);
    if (!buf) {
      fprintf(stderr, "Malloc failed for read buffer.\n");
      goto out;
    }
  }
  if (op == 'r') {
    ret = ch341SpiRead(buf, 0, cap);
    if (ret < 0)
      goto out;
    fp = fopen(filename, "wb");
    if (!fp) {
      fprintf(stderr, "Couldn't open file %s for writing.\n", filename);
      goto out;
    }
    fwrite(buf, 1, cap, fp);
    if (ferror(fp))
      fprintf(stderr, "Error writing file [%s]\n", filename);
    fclose(fp);
  }
  if (op == 'w') {
    fp = fopen(filename, "rb");
    if (!fp) {
      fprintf(stderr, "Couldn't open file %s for reading.\n", filename);
      goto out;
    }
    ret = fread(buf, 1, cap, fp);
    if (ferror(fp)) {
      fprintf(stderr, "Error reading file [%s]\n", filename);
      if (fp)
        fclose(fp);
      goto out;
    }
    fprintf(stderr, "File Size is [%d]\n", ret);
    fclose(fp);
    ret = ch341SpiWrite(buf, 0, ret);
  }
out:
  ch341Release();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x38(%rbp)
movb   $0x0,-0x39(%rbp)
lea    -0x180(%rbp),%rdi
lea    0xd9e(%rip),%rsi
mov    $0x135,%edx
call   1120 <memcpy@plt>
lea    -0x210(%rbp),%rdi
lea    0xebb(%rip),%rax
mov    %rax,-0x210(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x208(%rbp)
movl   $0x0,-0x204(%rbp)
movl   $0x68,-0x200(%rbp)
add    $0x18,%rdi
lea    0xe8b(%rip),%rax
mov    %rax,-0x1f8(%rbp)
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x1f0(%rbp)
movl   $0x0,-0x1ec(%rbp)
movl   $0x65,-0x1e8(%rbp)
add    $0x18,%rdi
lea    0xe5c(%rip),%rax
mov    %rax,-0x1e0(%rbp)
mov    0x2c9e(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x1d8(%rbp)
movl   $0x0,-0x1d4(%rbp)
movl   $0x77,-0x1d0(%rbp)
add    $0x18,%rdi
lea    0xe2d(%rip),%rax
mov    %rax,-0x1c8(%rbp)
mov    0x2c69(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x1c0(%rbp)
movl   $0x0,-0x1bc(%rbp)
movl   $0x6c,-0x1b8(%rbp)
add    $0x18,%rdi
lea    0xdff(%rip),%rax
mov    %rax,-0x1b0(%rbp)
mov    0x2c34(%rip),%rax
mov    (%rax),%eax
mov    %al,-0x1a8(%rbp)
movl   $0x0,-0x1a4(%rbp)
movl   $0x72,-0x1a0(%rbp)
add    $0x18,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   10e0 <memset@plt>
movl   $0x0,-0x214(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x210(%rbp),%rcx
lea    0xdab(%rip),%rdx
lea    -0x214(%rbp),%r8
call   10c0 <getopt_long@plt>
mov    %eax,-0x40(%rbp)
cmp    $0xffffffff,%eax
je     14e4 <main+0x264>
mov    -0x40(%rbp),%eax
add    $0xffffff9b,%eax
mov    %eax,%ecx
mov    %rcx,-0x220(%rbp)
sub    $0x12,%eax
ja     14be <main+0x23e>
mov    -0x220(%rbp),%rax
lea    0xbcb(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpb   $0x0,-0x39(%rbp)
jne    1453 <main+0x1d3>
mov    -0x40(%rbp),%eax
mov    %al,-0x39(%rbp)
jmp    1457 <main+0x1d7>
movb   $0x78,-0x39(%rbp)
jmp    14df <main+0x25f>
cmpb   $0x0,-0x39(%rbp)
jne    149f <main+0x21f>
mov    -0x40(%rbp),%eax
mov    %al,-0x39(%rbp)
mov    0x2b85(%rip),%rax
mov    (%rax),%edi
call   10b0 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1130 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2b65(%rip),%rax
mov    (%rax),%esi
call   1050 <strcpy@plt>
jmp    14a3 <main+0x223>
movb   $0x78,-0x39(%rbp)
jmp    14df <main+0x25f>
mov    0x2b49(%rip),%rax
mov    (%rax),%edi
call   1160 <atoi@plt>
mov    %eax,-0x38(%rbp)
jmp    14df <main+0x25f>
lea    -0x180(%rbp),%rsi
lea    0xce0(%rip),%rdi
mov    $0x0,%al
call   10d0 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    18c4 <main+0x644>
jmp    13e2 <main+0x162>
movsbl -0x39(%rbp),%eax
cmp    $0x0,%eax
jne    151b <main+0x29b>
mov    0x2ad8(%rip),%rax
mov    (%rax),%edi
lea    -0x180(%rbp),%rdx
lea    0xca4(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    18c4 <main+0x644>
movsbl -0x39(%rbp),%eax
cmp    $0x78,%eax
jne    154b <main+0x2cb>
mov    0x2aa1(%rip),%rax
mov    (%rax),%edi
lea    0xc78(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    18c4 <main+0x644>
mov    $0x1a86,%edi
mov    $0x5512,%esi
call   10f0 <ch341Configure@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1573 <main+0x2f3>
movl   $0xffffffff,-0x4(%rbp)
jmp    18c4 <main+0x644>
mov    $0x1,%edi
call   1170 <ch341SetStream@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    158f <main+0x30f>
jmp    18b6 <main+0x636>
mov    $0x0,%al
call   1030 <ch341SpiCapacity@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    15a8 <main+0x328>
jmp    18b6 <main+0x636>
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xc22(%rip),%rdi
mov    $0x0,%al
call   10d0 <printf@plt>
cmpl   $0x0,-0x38(%rbp)
je     15d6 <main+0x356>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
movsbl -0x39(%rbp),%eax
cmp    $0x69,%eax
jne    15e8 <main+0x368>
jmp    18b6 <main+0x636>
movsbl -0x39(%rbp),%eax
cmp    $0x65,%eax
jne    16c8 <main+0x448>
movl   $0x0,-0x218(%rbp)
mov    $0x0,%al
call   1040 <ch341EraseChip@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1618 <main+0x398>
jmp    18b6 <main+0x636>
jmp    161d <main+0x39d>
mov    $0x1,%edi
call   1190 <sleep@plt>
mov    $0x0,%al
call   1090 <ch341ReadStatus@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1640 <main+0x3c0>
jmp    18b6 <main+0x636>
lea    0xbaf(%rip),%rdi
mov    $0x0,%al
call   10d0 <printf@plt>
mov    0x296b(%rip),%rax
mov    (%rax),%edi
call   1140 <fflush@plt>
mov    -0x218(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x218(%rbp)
cmpl   $0x64,-0x218(%rbp)
jne    167d <main+0x3fd>
jmp    168c <main+0x40c>
jmp    1682 <main+0x402>
cmpl   $0x0,-0x14(%rbp)
jne    161d <main+0x39d>
cmpl   $0x64,-0x218(%rbp)
jne    16b5 <main+0x435>
mov    0x2930(%rip),%rax
mov    (%rax),%edi
lea    0xb4f(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
jmp    16c3 <main+0x443>
lea    0xb51(%rip),%rdi
mov    $0x0,%al
call   10d0 <printf@plt>
jmp    16c8 <main+0x448>
movsbl -0x39(%rbp),%eax
cmp    $0x72,%eax
je     16e2 <main+0x462>
movsbl -0x39(%rbp),%eax
cmp    $0x77,%eax
jne    171a <main+0x49a>
mov    -0x34(%rbp),%edi
call   1130 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1715 <main+0x495>
mov    0x28d0(%rip),%rax
mov    (%rax),%edi
lea    0xb16(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
jmp    18b6 <main+0x636>
jmp    171a <main+0x49a>
movsbl -0x39(%rbp),%eax
cmp    $0x72,%eax
jne    17d2 <main+0x552>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%edx
xor    %esi,%esi
call   1060 <ch341SpiRead@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1747 <main+0x4c7>
jmp    18b6 <main+0x636>
mov    -0x30(%rbp),%rdi
lea    0xaed(%rip),%rsi
call   1150 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1786 <main+0x506>
mov    0x2863(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xac8(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
jmp    18b6 <main+0x636>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1180 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ferror@plt>
cmp    $0x0,%rax
je     17c9 <main+0x549>
mov    0x281b(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xaa4(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <fclose@plt>
movsbl -0x39(%rbp),%eax
cmp    $0x77,%eax
jne    18b1 <main+0x631>
mov    -0x30(%rbp),%rdi
lea    0xa95(%rip),%rsi
call   1150 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    181e <main+0x59e>
mov    0x27cb(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xa70(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
jmp    18b6 <main+0x636>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fread@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <ferror@plt>
cmp    $0x0,%rax
je     187d <main+0x5fd>
mov    0x2780(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xa49(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
cmpq   $0x0,-0x28(%rbp)
je     1878 <main+0x5f8>
mov    -0x28(%rbp),%rdi
call   10a0 <fclose@plt>
jmp    18b6 <main+0x636>
mov    0x274c(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xa2f(%rip),%rsi
mov    $0x0,%al
call   1110 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   11a0 <ch341SpiWrite@plt>
mov    %eax,-0x14(%rbp)
jmp    18b6 <main+0x636>
mov    $0x0,%al
call   1100 <ch341Release@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1d8,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xdf3(%rip),%rsi
lea    0xa0(%rsp),%rdi
mov    $0x135,%edx
call   1130 <memcpy@plt>
lea    0xf0f(%rip),%rax
mov    %rax,0x10(%rsp)
mov    0x2cf6(%rip),%rax
mov    (%rax),%al
mov    %al,0x18(%rsp)
movabs $0x6800000000,%rcx
mov    %rcx,0x1c(%rsp)
lea    0xeec(%rip),%rcx
mov    %rcx,0x28(%rsp)
mov    %al,0x30(%rsp)
movabs $0x6500000000,%rax
mov    %rax,0x34(%rsp)
lea    0xed3(%rip),%rax
mov    %rax,0x40(%rsp)
mov    0x2cc7(%rip),%rax
mov    (%rax),%al
mov    %al,0x48(%rsp)
movabs $0x7700000000,%rcx
mov    %rcx,0x4c(%rsp)
lea    0xeb1(%rip),%rcx
mov    %rcx,0x58(%rsp)
mov    %al,0x60(%rsp)
movabs $0x6c00000000,%rcx
mov    %rcx,0x64(%rsp)
lea    0xe99(%rip),%rcx
mov    %rcx,0x70(%rsp)
mov    %al,0x78(%rsp)
movabs $0x7200000000,%rax
mov    %rax,0x7c(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x88(%rsp)
movq   $0x0,0x98(%rsp)
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
lea    0x4(%rsp),%r15
lea    0xc62(%rip),%r13
xor    %r14d,%r14d
jmp    13bf <main+0x12f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %r14b,%r14b
mov    $0x78,%eax
cmovne %eax,%r12d
mov    %r12d,%r14d
mov    %ebp,%edi
mov    %rbx,%rsi
lea    0xe27(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    %r15,%r8
call   10e0 <getopt_long@plt>
mov    %eax,%r12d
lea    -0x65(%r12),%eax
cmp    $0x12,%eax
ja     1436 <main+0x1a6>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
test   %r14b,%r14b
mov    $0x78,%r14b
jne    13bf <main+0x12f>
mov    0x2bfa(%rip),%r14
mov    (%r14),%edi
call   10d0 <strlen@plt>
lea    0x1(%rax),%edi
call   1140 <malloc@plt>
mov    (%r14),%esi
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
call   1060 <strcpy@plt>
mov    %r12d,%r14d
jmp    13bf <main+0x12f>
mov    0x2bce(%rip),%rax
mov    (%rax),%edi
call   1170 <atoi@plt>
mov    %eax,(%rsp)
jmp    13bf <main+0x12f>
cmp    $0xffffffff,%r12d
jne    146a <main+0x1da>
cmp    $0x78,%r14b
je     148d <main+0x1fd>
test   %r14b,%r14b
jne    14ab <main+0x21b>
mov    0x2b82(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
xor    %ebx,%ebx
lea    0xa0(%rsp),%rdx
xor    %eax,%eax
call   1120 <fprintf@plt>
jmp    1479 <main+0x1e9>
lea    0xa0(%rsp),%rdi
call   1080 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x1d8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2b3c(%rip),%rax
mov    (%rax),%edi
lea    0xd63(%rip),%rsi
xor    %eax,%eax
call   1120 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1479 <main+0x1e9>
mov    $0x1a86,%edi
mov    $0x5512,%esi
call   1100 <ch341Configure@plt>
test   %eax,%eax
js     15c7 <main+0x337>
mov    $0x1,%edi
call   1180 <ch341SetStream@plt>
test   %eax,%eax
js     1730 <main+0x4a0>
xor    %eax,%eax
call   1040 <ch341SpiCapacity@plt>
test   %eax,%eax
js     1730 <main+0x4a0>
mov    $0x1,%r12d
mov    %eax,%ecx
shl    %cl,%r12d
lea    0xd3c(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   10f0 <printf@plt>
mov    (%rsp),%eax
test   %eax,%eax
cmovne %eax,%r12d
lea    -0x65(%r14),%eax
cmp    $0x12,%al
ja     1537 <main+0x2a7>
movzbl %al,%eax
lea    0xb32(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %r12d,%edi
call   1140 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1639 <main+0x3a9>
cmp    $0x77,%r14b
je     168b <main+0x3fb>
cmp    $0x72,%r14b
jne    1730 <main+0x4a0>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1070 <ch341SpiRead@plt>
test   %eax,%eax
js     1730 <main+0x4a0>
lea    0xd14(%rip),%rsi
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1160 <fopen@plt>
test   %rax,%rax
je     1655 <main+0x3c5>
mov    %rax,%r14
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rax,%rcx
call   1190 <fwrite@plt>
mov    %r14,%rdi
call   1090 <ferror@plt>
test   %rax,%rax
je     15ba <main+0x32a>
mov    0x2a29(%rip),%rax
mov    (%rax),%edi
lea    0xcf2(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1120 <fprintf@plt>
mov    %r14,%rdi
call   10c0 <fclose@plt>
jmp    1730 <main+0x4a0>
mov    $0xffffffff,%ebx
jmp    1479 <main+0x1e9>
xor    %eax,%eax
call   1050 <ch341EraseChip@plt>
test   %eax,%eax
js     1730 <main+0x4a0>
mov    $0x64,%ebp
mov    0x29d4(%rip),%r15
mov    $0x1,%edi
call   11a0 <sleep@plt>
xor    %eax,%eax
call   10b0 <ch341ReadStatus@plt>
test   %eax,%eax
js     1730 <main+0x4a0>
mov    %eax,%ebx
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r15),%edi
call   1150 <fflush@plt>
lea    -0x1(%rbp),%eax
cmp    $0x1,%ebp
je     1627 <main+0x397>
mov    %eax,%ebp
test   %ebx,%ebx
jne    15ec <main+0x35c>
test   %eax,%eax
je     166a <main+0x3da>
lea    0xcdc(%rip),%rdi
call   1080 <puts@plt>
jmp    1681 <main+0x3f1>
mov    0x2990(%rip),%rax
mov    (%rax),%edi
lea    0xc12(%rip),%rsi
xor    %eax,%eax
call   1120 <fprintf@plt>
jmp    1730 <main+0x4a0>
mov    0x2974(%rip),%rax
mov    (%rax),%edi
lea    0xc19(%rip),%rsi
jmp    16fe <main+0x46e>
mov    0x295f(%rip),%rax
mov    (%rax),%edi
lea    0xbcc(%rip),%rsi
xor    %eax,%eax
call   1120 <fprintf@plt>
cmp    $0x77,%r14b
jne    1541 <main+0x2b1>
lea    0xc29(%rip),%rsi
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1160 <fopen@plt>
test   %rax,%rax
je     16ee <main+0x45e>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %rax,%rcx
call   10a0 <fread@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1090 <ferror@plt>
mov    0x2904(%rip),%rcx
mov    (%rcx),%edi
test   %rax,%rax
je     170a <main+0x47a>
lea    0xc08(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1120 <fprintf@plt>
mov    %r15,%rdi
call   10c0 <fclose@plt>
jmp    1730 <main+0x4a0>
mov    0x28db(%rip),%rax
mov    (%rax),%edi
lea    0xbc0(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1120 <fprintf@plt>
jmp    1730 <main+0x4a0>
lea    0xbea(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1120 <fprintf@plt>
mov    %r15,%rdi
call   10c0 <fclose@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   11b0 <ch341SpiWrite@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1110 <ch341Release@plt>
jmp    1479 <main+0x1e9>

LLVM IR (-O0):
%struct.option = type { i8*, i8, i32, i32 }
@__const.main.usage = [309 x i8] c"\0AUsage:\0A -h, --help display this message\0A -i, --info read the chip ID info\0A -e, --erase erase the entire chip\0A -l, --length <bytes> manually set length\0A -w, --write <filename> write chip with data from filename\0A -r, --read <filename> read chip and save data to filename\0A\00"
@.str = [5 x i8] c"help\00"
@no_argument = global i32 0
@.str.1 = [6 x i8] c"erase\00"
@.str.2 = [6 x i8] c"write\00"
@required_argument = global i32 0
@.str.3 = [7 x i8] c"length\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [10 x i8] c"hiew:r:l:\00"
@optarg = global i32 0
@.str.6 = [4 x i8] c"%s\0A\00"
@stderr = global i32 0
@.str.7 = [49 x i8] c"Conflicting options, only one option at a time.\0A\00"
@.str.8 = [21 x i8] c"Chip capacity is %d\0A\00"
@.str.9 = [2 x i8] c".\00"
@stdout = global i32 0
@.str.10 = [21 x i8] c"Chip erase timeout.\0A\00"
@.str.11 = [18 x i8] c"Chip erase done!\0A\00"
@.str.12 = [32 x i8] c"Malloc failed for read buffer.\0A\00"
@.str.13 = [3 x i8] c"wb\00"
@.str.14 = [36 x i8] c"Couldn't open file %s for writing.\0A\00"
@.str.15 = [25 x i8] c"Error writing file [%s]\0A\00"
@.str.16 = [3 x i8] c"rb\00"
@.str.17 = [36 x i8] c"Couldn't open file %s for reading.\0A\00"
@.str.18 = [25 x i8] c"Error reading file [%s]\0A\00"
@.str.19 = [19 x i8] c"File Size is [%d]\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 i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
%13 = alloca i32
%14 = alloca [309 x i8]
%15 = alloca [6 x %struct.option]
%16 = alloca i32
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %11
store i8 0, i8* %12
%18 = bitcast [309 x i8]* %14 to i8*
%19 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %15, i64 0, i64 0
%20 = getelementptr inbounds %struct.option, %struct.option* %19, i32 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %20
%21 = getelementptr inbounds %struct.option, %struct.option* %19, i32 0, i32 1
%22 = load i32, i32* @no_argument
%23 = trunc i32 %22 to i8
store i8 %23, i8* %21
%24 = getelementptr inbounds %struct.option, %struct.option* %19, i32 0, i32 2
store i32 0, i32* %24
%25 = getelementptr inbounds %struct.option, %struct.option* %19, i32 0, i32 3
store i32 104, i32* %25
%26 = getelementptr inbounds %struct.option, %struct.option* %19, i64 1
%27 = getelementptr inbounds %struct.option, %struct.option* %26, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %27
%28 = getelementptr inbounds %struct.option, %struct.option* %26, i32 0, i32 1
%29 = load i32, i32* @no_argument
%30 = trunc i32 %29 to i8
store i8 %30, i8* %28
%31 = getelementptr inbounds %struct.option, %struct.option* %26, i32 0, i32 2
store i32 0, i32* %31
%32 = getelementptr inbounds %struct.option, %struct.option* %26, i32 0, i32 3
store i32 101, i32* %32
%33 = getelementptr inbounds %struct.option, %struct.option* %26, i64 1
%34 = getelementptr inbounds %struct.option, %struct.option* %33, i32 0, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %34
%35 = getelementptr inbounds %struct.option, %struct.option* %33, i32 0, i32 1
%36 = load i32, i32* @required_argument
%37 = trunc i32 %36 to i8
store i8 %37, i8* %35
%38 = getelementptr inbounds %struct.option, %struct.option* %33, i32 0, i32 2
store i32 0, i32* %38
%39 = getelementptr inbounds %struct.option, %struct.option* %33, i32 0, i32 3
store i32 119, i32* %39
%40 = getelementptr inbounds %struct.option, %struct.option* %33, i64 1
%41 = getelementptr inbounds %struct.option, %struct.option* %40, i32 0, i32 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %41
%42 = getelementptr inbounds %struct.option, %struct.option* %40, i32 0, i32 1
%43 = load i32, i32* @required_argument
%44 = trunc i32 %43 to i8
store i8 %44, i8* %42
%45 = getelementptr inbounds %struct.option, %struct.option* %40, i32 0, i32 2
store i32 0, i32* %45
%46 = getelementptr inbounds %struct.option, %struct.option* %40, i32 0, i32 3
store i32 108, i32* %46
%47 = getelementptr inbounds %struct.option, %struct.option* %40, i64 1
%48 = getelementptr inbounds %struct.option, %struct.option* %47, i32 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %48
%49 = getelementptr inbounds %struct.option, %struct.option* %47, i32 0, i32 1
%50 = load i32, i32* @required_argument
%51 = trunc i32 %50 to i8
store i8 %51, i8* %49
%52 = getelementptr inbounds %struct.option, %struct.option* %47, i32 0, i32 2
store i32 0, i32* %52
%53 = getelementptr inbounds %struct.option, %struct.option* %47, i32 0, i32 3
store i32 114, i32* %53
%54 = getelementptr inbounds %struct.option, %struct.option* %47, i64 1
%55 = bitcast %struct.option* %54 to i8*
store i32 0, i32* %16
br label %56
56: ; preds = %94, %2
%57 = load i32, i32* %4
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %15, i64 0, i64 0
%60 = call i32 @getopt_long(i32 %57, i8** %58, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %struct.option* %59, i32* %16)
store i32 %60, i32* %13
%61 = icmp ne i32 %60, -1
br i1 %61, label %62, label %95
62: ; preds = %56
%63 = load i32, i32* %13
switch i32 %63, label %91 [
i32 105, label %64
i32 101, label %64
i32 119, label %72
i32 114, label %72
i32 108, label %88
]
64: ; preds = %62, %62
%65 = load i8, i8* %12
%66 = icmp ne i8 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %64
%68 = load i32, i32* %13
%69 = trunc i32 %68 to i8
store i8 %69, i8* %12
br label %71
70: ; preds = %64
store i8 120, i8* %12
br label %71
71: ; preds = %70, %67
br label %94
72: ; preds = %62, %62
%73 = load i8, i8* %12
%74 = icmp ne i8 %73, 0
br i1 %74, label %86, label %75
75: ; preds = %72
%76 = load i32, i32* %13
%77 = trunc i32 %76 to i8
store i8 %77, i8* %12
%78 = load i32, i32* @optarg
%79 = call i32 @strlen(i32 %78)
%80 = add nsw i32 %79, 1
%81 = call i64 @malloc(i32 %80)
%82 = inttoptr i64 %81 to i8*
store i8* %82, i8** %9
%83 = load i8*, i8** %9
%84 = load i32, i32* @optarg
%85 = call i32 @strcpy(i8* %83, i32 %84)
br label %87
86: ; preds = %72
store i8 120, i8* %12
br label %87
87: ; preds = %86, %75
br label %94
88: ; preds = %62
%89 = load i32, i32* @optarg
%90 = call i32 @atoi(i32 %89)
store i32 %90, i32* %11
br label %94
91: ; preds = %62
%92 = getelementptr inbounds [309 x i8], [309 x i8]* %14, i64 0, i64 0
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %92)
store i32 0, i32* %3
br label %273
94: ; preds = %88, %87, %71
br label %56
95: ; preds = %56
%96 = load i8, i8* %12
%97 = sext i8 %96 to i32
%98 = icmp eq i32 %97, 0
br i1 %98, label %99, label %103
99: ; preds = %95
%100 = load i32, i32* @stderr
%101 = getelementptr inbounds [309 x i8], [309 x i8]* %14, i64 0, i64 0
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %101)
store i32 0, i32* %3
br label %273
103: ; preds = %95
%104 = load i8, i8* %12
%105 = sext i8 %104 to i32
%106 = icmp eq i32 %105, 120
br i1 %106, label %107, label %110
107: ; preds = %103
%108 = load i32, i32* @stderr
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %273
110: ; preds = %103
%111 = call i32 @ch341Configure(i32 6790, i32 21778)
store i32 %111, i32* %6
%112 = load i32, i32* %6
%113 = icmp slt i32 %112, 0
br i1 %113, label %114, label %115
114: ; preds = %110
store i32 -1, i32* %3
br label %273
115: ; preds = %110
%116 = call i32 @ch341SetStream(i32 1)
store i32 %116, i32* %6
%117 = load i32, i32* %6
%118 = icmp slt i32 %117, 0
br i1 %118, label %119, label %120
119: ; preds = %115
br label %271
120: ; preds = %115
%121 = call i32 (...) @ch341SpiCapacity()
store i32 %121, i32* %6
%122 = load i32, i32* %6
%123 = icmp slt i32 %122, 0
br i1 %123, label %124, label %125
124: ; preds = %120
br label %271
125: ; preds = %120
%126 = load i32, i32* %6
%127 = shl i32 1, %126
store i32 %127, i32* %10
%128 = load i32, i32* %10
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %128)
%130 = load i32, i32* %11
%131 = icmp ne i32 %130, 0
br i1 %131, label %132, label %134
132: ; preds = %125
%133 = load i32, i32* %11
store i32 %133, i32* %10
br label %134
134: ; preds = %132, %125
%135 = load i8, i8* %12
%136 = sext i8 %135 to i32
%137 = icmp eq i32 %136, 105
br i1 %137, label %138, label %139
138: ; preds = %134
br label %271
139: ; preds = %134
%140 = load i8, i8* %12
%141 = sext i8 %140 to i32
%142 = icmp eq i32 %141, 101
br i1 %142, label %143, label %177
143: ; preds = %139
store i32 0, i32* %17
%144 = call i32 (...) @ch341EraseChip()
store i32 %144, i32* %6
%145 = load i32, i32* %6
%146 = icmp slt i32 %145, 0
br i1 %146, label %147, label %148
147: ; preds = %143
br label %271
148: ; preds = %143
br label %149
149: ; preds = %165, %148
%150 = call i32 @sleep(i32 1)
%151 = call i32 (...) @ch341ReadStatus()
store i32 %151, i32* %6
%152 = load i32, i32* %6
%153 = icmp slt i32 %152, 0
br i1 %153, label %154, label %155
154: ; preds = %149
br label %271
155: ; preds = %149
%156 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%157 = load i32, i32* @stdout
%158 = call i32 @fflush(i32 %157)
%159 = load i32, i32* %17
%160 = add nsw i32 %159, 1
store i32 %160, i32* %17
%161 = load i32, i32* %17
%162 = icmp eq i32 %161, 100
br i1 %162, label %163, label %164
163: ; preds = %155
br label %168
164: ; preds = %155
br label %165
165: ; preds = %164
%166 = load i32, i32* %6
%167 = icmp ne i32 %166, 0
br i1 %167, label %149, label %168
168: ; preds = %165, %163
%169 = load i32, i32* %17
%170 = icmp eq i32 %169, 100
br i1 %170, label %171, label %174
171: ; preds = %168
%172 = load i32, i32* @stderr
%173 = call i32 (i32, i8*, ...) @fprintf(i32 %172, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
br label %176
174: ; preds = %168
%175 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
br label %176
176: ; preds = %174, %171
br label %177
177: ; preds = %176, %139
%178 = load i8, i8* %12
%179 = sext i8 %178 to i32
%180 = icmp eq i32 %179, 114
br i1 %180, label %185, label %181
181: ; preds = %177
%182 = load i8, i8* %12
%183 = sext i8 %182 to i32
%184 = icmp eq i32 %183, 119
br i1 %184, label %185, label %195
185: ; preds = %181, %177
%186 = load i32, i32* %10
%187 = call i64 @malloc(i32 %186)
%188 = inttoptr i64 %187 to i32*
store i32* %188, i32** %7
%189 = load i32*, i32** %7
%190 = icmp ne i32* %189, null
br i1 %190, label %194, label %191
191: ; preds = %185
%192 = load i32, i32* @stderr
%193 = call i32 (i32, i8*, ...) @fprintf(i32 %192, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.12, i64 0, i64 0))
br label %271
194: ; preds = %185
br label %195
195: ; preds = %194, %181
%196 = load i8, i8* %12
%197 = sext i8 %196 to i32
%198 = icmp eq i32 %197, 114
br i1 %198, label %199, label %230
199: ; preds = %195
%200 = load i32*, i32** %7
%201 = load i32, i32* %10
%202 = call i32 @ch341SpiRead(i32* %200, i32 0, i32 %201)
store i32 %202, i32* %6
%203 = load i32, i32* %6
%204 = icmp slt i32 %203, 0
br i1 %204, label %205, label %206
205: ; preds = %199
br label %271
206: ; preds = %199
%207 = load i8*, i8** %9
%208 = call i32* @fopen(i8* %207, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i64 0, i64 0))
store i32* %208, i32** %8
%209 = load i32*, i32** %8
%210 = icmp ne i32* %209, null
br i1 %210, label %215, label %211
211: ; preds = %206
%212 = load i32, i32* @stderr
%213 = load i8*, i8** %9
%214 = call i32 (i32, i8*, ...) @fprintf(i32 %212, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.14, i64 0, i64 0), i8* %213)
br label %271
215: ; preds = %206
%216 = load i32*, i32** %7
%217 = load i32, i32* %10
%218 = load i32*, i32** %8
%219 = call i32 @fwrite(i32* %216, i32 1, i32 %217, i32* %218)
%220 = load i32*, i32** %8
%221 = call i64 @ferror(i32* %220)
%222 = icmp ne i64 %221, 0
br i1 %222, label %223, label %227
223: ; preds = %215
%224 = load i32, i32* @stderr
%225 = load i8*, i8** %9
%226 = call i32 (i32, i8*, ...) @fprintf(i32 %224, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.15, i64 0, i64 0), i8* %225)
br label %227
227: ; preds = %223, %215
%228 = load i32*, i32** %8
%229 = call i32 @fclose(i32* %228)
br label %230
230: ; preds = %227, %195
%231 = load i8, i8* %12
%232 = sext i8 %231 to i32
%233 = icmp eq i32 %232, 119
br i1 %233, label %234, label %270
234: ; preds = %230
%235 = load i8*, i8** %9
%236 = call i32* @fopen(i8* %235, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0))
store i32* %236, i32** %8
%237 = load i32*, i32** %8
%238 = icmp ne i32* %237, null
br i1 %238, label %243, label %239
239: ; preds = %234
%240 = load i32, i32* @stderr
%241 = load i8*, i8** %9
%242 = call i32 (i32, i8*, ...) @fprintf(i32 %240, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.17, i64 0, i64 0), i8* %241)
br label %271
243: ; preds = %234
%244 = load i32*, i32** %7
%245 = load i32, i32* %10
%246 = load i32*, i32** %8
%247 = call i32 @fread(i32* %244, i32 1, i32 %245, i32* %246)
store i32 %247, i32* %6
%248 = load i32*, i32** %8
%249 = call i64 @ferror(i32* %248)
%250 = icmp ne i64 %249, 0
br i1 %250, label %251, label %261
251: ; preds = %243
%252 = load i32, i32* @stderr
%253 = load i8*, i8** %9
%254 = call i32 (i32, i8*, ...) @fprintf(i32 %252, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.18, i64 0, i64 0), i8* %253)
%255 = load i32*, i32** %8
%256 = icmp ne i32* %255, null
br i1 %256, label %257, label %260
257: ; preds = %251
%258 = load i32*, i32** %8
%259 = call i32 @fclose(i32* %258)
br label %260
260: ; preds = %257, %251
br label %271
261: ; preds = %243
%262 = load i32, i32* @stderr
%263 = load i32, i32* %6
%264 = call i32 (i32, i8*, ...) @fprintf(i32 %262, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.19, i64 0, i64 0), i32 %263)
%265 = load i32*, i32** %8
%266 = call i32 @fclose(i32* %265)
%267 = load i32*, i32** %7
%268 = load i32, i32* %6
%269 = call i32 @ch341SpiWrite(i32* %267, i32 0, i32 %268)
store i32 %269, i32* %6
br label %270
270: ; preds = %261, %230
br label %271
271: ; preds = %270, %260, %239, %211, %205, %191, %154, %147, %138, %124, %119
%272 = call i32 (...) @ch341Release()
store i32 0, i32* %3
br label %273
273: ; preds = %271, %114, %107, %99, %91
%274 = load i32, i32* %3
ret i32 %274
}

LLVM IR (-O2):
%struct.option = type { i8*, i8, i32, i32 }
@__const.main.usage = [309 x i8] c"\0AUsage:\0A -h, --help display this message\0A -i, --info read the chip ID info\0A -e, --erase erase the entire chip\0A -l, --length <bytes> manually set length\0A -w, --write <filename> write chip with data from filename\0A -r, --read <filename> read chip and save data to filename\0A\00"
@.str = [5 x i8] c"help\00"
@no_argument = global i32 0
@.str.1 = [6 x i8] c"erase\00"
@.str.2 = [6 x i8] c"write\00"
@required_argument = global i32 0
@.str.3 = [7 x i8] c"length\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [10 x i8] c"hiew:r:l:\00"
@optarg = global i32 0
@.str.6 = [4 x i8] c"%s\0A\00"
@stderr = global i32 0
@.str.7 = [49 x i8] c"Conflicting options, only one option at a time.\0A\00"
@.str.8 = [21 x i8] c"Chip capacity is %d\0A\00"
@stdout = global i32 0
@.str.10 = [21 x i8] c"Chip erase timeout.\0A\00"
@.str.12 = [32 x i8] c"Malloc failed for read buffer.\0A\00"
@.str.13 = [3 x i8] c"wb\00"
@.str.14 = [36 x i8] c"Couldn't open file %s for writing.\0A\00"
@.str.15 = [25 x i8] c"Error writing file [%s]\0A\00"
@.str.16 = [3 x i8] c"rb\00"
@.str.17 = [36 x i8] c"Couldn't open file %s for reading.\0A\00"
@.str.18 = [25 x i8] c"Error reading file [%s]\0A\00"
@.str.19 = [19 x i8] c"File Size is [%d]\0A\00"
@str = [17 x i8] c"Chip erase done!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [309 x i8]
%4 = alloca [6 x %struct.option]
%5 = alloca i32
%6 = getelementptr inbounds [309 x i8], [309 x i8]* %3, i64 0, i64 0
%7 = bitcast [6 x %struct.option]* %4 to i8*
%8 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 0
%9 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 0, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 0, i32 1
%11 = load i32, i32* @no_argument
%12 = trunc i32 %11 to i8
store i8 %12, i8* %10
%13 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 0, i32 2
store i32 0, i32* %13
%14 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 0, i32 3
store i32 104, i32* %14
%15 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 1, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 1, i32 1
store i8 %12, i8* %16
%17 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 1, i32 2
store i32 0, i32* %17
%18 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 1, i32 3
store i32 101, i32* %18
%19 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 2, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 2, i32 1
%21 = load i32, i32* @required_argument
%22 = trunc i32 %21 to i8
store i8 %22, i8* %20
%23 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 2, i32 2
store i32 0, i32* %23
%24 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 2, i32 3
store i32 119, i32* %24
%25 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 3, i32 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %25
%26 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 3, i32 1
store i8 %22, i8* %26
%27 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 3, i32 2
store i32 0, i32* %27
%28 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 3, i32 3
store i32 108, i32* %28
%29 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 4, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8** %29
%30 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 4, i32 1
store i8 %22, i8* %30
%31 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 4, i32 2
store i32 0, i32* %31
%32 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 4, i32 3
store i32 114, i32* %32
%33 = getelementptr inbounds [6 x %struct.option], [6 x %struct.option]* %4, i64 0, i64 5
%34 = bitcast %struct.option* %33 to i8*
%35 = bitcast i32* %5 to i8*
store i32 0, i32* %5
br label %36
36: ; preds = %54, %2
%37 = phi i8* [ %60, %54 ], [ undef, %2 ]
%38 = phi i32 [ %41, %54 ], [ 0, %2 ]
%39 = phi i8 [ %55, %54 ], [ 0, %2 ]
br label %40
40: ; preds = %36, %63
%41 = phi i32 [ %38, %36 ], [ %65, %63 ]
%42 = phi i8 [ %39, %36 ], [ %44, %63 ]
br label %43
43: ; preds = %50, %40
%44 = phi i8 [ %42, %40 ], [ %51, %50 ]
%45 = call i32 @getopt_long(i32 %0, i8** %1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %struct.option* %8, i32* %5)
switch i32 %45, label %66 [
i32 -1, label %68
i32 105, label %46
i32 101, label %46
i32 119, label %52
i32 114, label %52
i32 108, label %63
]
46: ; preds = %43, %43
%47 = icmp eq i8 %44, 0
%48 = trunc i32 %45 to i8
%49 = select i1 %47, i8 %48, i8 120
br label %50
50: ; preds = %46, %52
%51 = phi i8 [ 120, %52 ], [ %49, %46 ]
br label %43
52: ; preds = %43, %43
%53 = icmp eq i8 %44, 0
br i1 %53, label %54, label %50
54: ; preds = %52
%55 = trunc i32 %45 to i8
%56 = load i32, i32* @optarg
%57 = call i32 @strlen(i32 %56)
%58 = add nsw i32 %57, 1
%59 = call i64 @malloc(i32 %58)
%60 = inttoptr i64 %59 to i8*
%61 = load i32, i32* @optarg
%62 = call i32 @strcpy(i8* %60, i32 %61)
br label %36
63: ; preds = %43
%64 = load i32, i32* @optarg
%65 = call i32 @atoi(i32 %64)
br label %40
66: ; preds = %43
%67 = call i32 @puts(i8* %6)
br label %159
68: ; preds = %43
switch i8 %44, label %75 [
i8 0, label %69
i8 120, label %72
]
69: ; preds = %68
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %6)
br label %159
72: ; preds = %68
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
br label %159
75: ; preds = %68
%76 = call i32 @ch341Configure(i32 6790, i32 21778)
%77 = icmp slt i32 %76, 0
br i1 %77, label %159, label %78
78: ; preds = %75
%79 = call i32 @ch341SetStream(i32 1)
%80 = icmp slt i32 %79, 0
br i1 %80, label %157, label %81
81: ; preds = %78
%82 = call i32 (...) @ch341SpiCapacity()
%83 = icmp slt i32 %82, 0
br i1 %83, label %157, label %84
84: ; preds = %81
%85 = shl nuw i32 1, %82
%86 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %85)
%87 = icmp eq i32 %41, 0
%88 = select i1 %87, i32 %85, i32 %41
switch i8 %44, label %119 [
i8 105, label %157
i8 101, label %89
i8 119, label %112
i8 114, label %112
]
89: ; preds = %84
%90 = call i32 (...) @ch341EraseChip()
%91 = icmp slt i32 %90, 0
br i1 %91, label %157, label %92
92: ; preds = %89, %97
%93 = phi i32 [ %101, %97 ], [ 0, %89 ]
%94 = call i32 @sleep(i32 1)
%95 = call i32 (...) @ch341ReadStatus()
%96 = icmp slt i32 %95, 0
br i1 %96, label %157, label %97
97: ; preds = %92
%98 = call i32 @putchar(i32 46)
%99 = load i32, i32* @stdout
%100 = call i32 @fflush(i32 %99)
%101 = add nuw nsw i32 %93, 1
%102 = icmp ne i32 %101, 100
%103 = icmp ne i32 %95, 0
%104 = select i1 %102, i1 %103, i1 false
br i1 %104, label %92, label %105
105: ; preds = %97
%106 = icmp eq i32 %101, 100
br i1 %106, label %107, label %110
107: ; preds = %105
%108 = load i32, i32* @stderr
%109 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.10, i64 0, i64 0))
br label %119
110: ; preds = %105
%111 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %119
112: ; preds = %84, %84
%113 = call i64 @malloc(i32 %88)
%114 = inttoptr i64 %113 to i32*
%115 = icmp eq i64 %113, 0
br i1 %115, label %116, label %119
116: ; preds = %112
%117 = load i32, i32* @stderr
%118 = call i32 (i32, i8*, ...) @fprintf(i32 %117, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.12, i64 0, i64 0))
br label %157
119: ; preds = %110, %107, %84, %112
%120 = phi i32* [ %114, %112 ], [ undef, %84 ], [ undef, %107 ], [ undef, %110 ]
switch i8 %44, label %157 [
i8 114, label %121
i8 119, label %139
]
121: ; preds = %119
%122 = call i32 @ch341SpiRead(i32* %120, i32 0, i32 %88)
%123 = icmp slt i32 %122, 0
br i1 %123, label %157, label %124
124: ; preds = %121
%125 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.13, i64 0, i64 0))
%126 = icmp eq i32* %125, null
br i1 %126, label %127, label %130
127: ; preds = %124
%128 = load i32, i32* @stderr
%129 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.14, i64 0, i64 0), i8* %37)
br label %157
130: ; preds = %124
%131 = call i32 @fwrite(i32* %120, i32 1, i32 %88, i32* %125)
%132 = call i64 @ferror(i32* %125)
%133 = icmp eq i64 %132, 0
br i1 %133, label %137, label %134
134: ; preds = %130
%135 = load i32, i32* @stderr
%136 = call i32 (i32, i8*, ...) @fprintf(i32 %135, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.15, i64 0, i64 0), i8* %37)
br label %137
137: ; preds = %134, %130
%138 = call i32 @fclose(i32* %125)
br label %157
139: ; preds = %119
%140 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0))
%141 = icmp eq i32* %140, null
br i1 %141, label %142, label %145
142: ; preds = %139
%143 = load i32, i32* @stderr
%144 = call i32 (i32, i8*, ...) @fprintf(i32 %143, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.17, i64 0, i64 0), i8* %37)
br label %157
145: ; preds = %139
%146 = call i32 @fread(i32* %120, i32 1, i32 %88, i32* %140)
%147 = call i64 @ferror(i32* %140)
%148 = icmp eq i64 %147, 0
%149 = load i32, i32* @stderr
br i1 %148, label %153, label %150
150: ; preds = %145
%151 = call i32 (i32, i8*, ...) @fprintf(i32 %149, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.18, i64 0, i64 0), i8* %37)
%152 = call i32 @fclose(i32* %140)
br label %157
153: ; preds = %145
%154 = call i32 (i32, i8*, ...) @fprintf(i32 %149, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.19, i64 0, i64 0), i32 %146)
%155 = call i32 @fclose(i32* %140)
%156 = call i32 @ch341SpiWrite(i32* %120, i32 0, i32 %146)
br label %157
157: ; preds = %92, %84, %137, %89, %119, %153, %150, %121, %81, %78, %142, %127, %116
%158 = call i32 (...) @ch341Release()
br label %159
159: ; preds = %75, %157, %72, %69, %66
%160 = phi i32 [ 0, %66 ], [ 0, %69 ], [ -1, %72 ], [ 0, %157 ], [ -1, %75 ]
ret i32 %160
}