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

/* Variables and functions */
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ flush_bytes(int);
char *index(char *, char);
int /*<<< orphan*/ info(int, char *, ...);
int /*<<< orphan*/ memcpy(unsigned char *, char *, int);
int /*<<< orphan*/ set_tty_defaults(int, int);
int /*<<< orphan*/ st_cmd(int, char *, int, int);
scalar_t__ st_getbytes(int, char *, int);
int /*<<< orphan*/ st_parsehex(unsigned char *, char *);
int /*<<< orphan*/ st_termsetup(int);
int /*<<< orphan*/ st_xor(unsigned char *, int);
int strlen(char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
int /*<<< orphan*/ write_line(int, char *);
int upgrade_st(int devfd, FILE *f) {
  char buf[64];
  int i, ret = 0;
  st_termsetup(devfd);
  info(1, "ST upgrade: attempting to sync up with target...\n");
  for (i = 0;; i++) {
    flush_bytes(devfd);
    write(devfd, "\x7f", 1);
    if (st_getbytes(devfd, buf, 1) == 0 && buf[0] == 0x79)
      break;
    if (i == 2) {
      set_tty_defaults(devfd, 9600);
      write_line(devfd, "");
      write_line(devfd, ">CB");
      usleep(20000);
      flush_bytes(devfd);
      st_termsetup(devfd);
      usleep(20000);
    }
    if (i == 4)
      die("error: can't establish communication with "
          "target.  Cycle power and try again.\n");
  }
  info(1, "Erasing...\n");
  st_cmd(devfd, "\x01\xfe", 2, 5);
  st_cmd(devfd, "\x02\xfd", 2, 5);
  st_cmd(devfd, "\x43\xbc", 2, 1);
  st_cmd(devfd,
         "\x3e\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b"
         "\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"
         "\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27"
         "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35"
         "\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x3e",
         65, 1);
  info(1, "Programming...\n");
  while (fgets(buf, 64, f) != NULL && buf[0] == ':') {
    unsigned char binbuf[18];
    int len, j;
    char *newline;
    newline = index(buf, '\n');
    if (*newline)
      *newline = 0;
    newline = index(buf, '\r');
    if (*newline)
      *newline = 0;
    info(3, "processing: '%s'\n", buf);
    if (buf[7] != '0' || buf[8] != '0')
      continue;
    st_cmd(devfd, "\x31\xce", 2, 1);
    memcpy(binbuf, "\x08\x00\x00\x00", 5);
    st_parsehex(&binbuf[2], &buf[3]);
    st_parsehex(&binbuf[3], &buf[5]);
    st_xor(binbuf, 4);
    st_cmd(devfd, (char *)binbuf, 5, 1);
    len = strlen(buf);
    if (len < 13 || len > 43) {
      info(0, "warning: malformed line '%s'\n", buf);
      ret = 1;
      continue;
    }
    len = (len - 11) >> 1;
    binbuf[0] = len - 1;
    for (j = 0; j < len; j++)
      st_parsehex(&binbuf[j + 1], &buf[9 + j * 2]);
    st_xor(binbuf, len + 1);
    st_cmd(devfd, (char *)binbuf, len + 2, 1);
  }
  info(1, "\n");
  st_cmd(devfd, "\x21\xde", 2, 1);
  st_cmd(devfd, "\x08\x00\x00\x00\x08", 5, 0);
  return ret;
}


x86 assembly (clang, -O0):
<upgrade_st>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x58(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <st_termsetup@plt>
mov    $0x1,%edi
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   1110 <info@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x4(%rbp),%edi
call   10d0 <flush_bytes@plt>
mov    -0x4(%rbp),%edi
lea    0xde5(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1,%edx
call   10e0 <st_getbytes@plt>
cmp    $0x0,%rax
jne    1284 <upgrade_st+0x84>
movsbl -0x50(%rbp),%eax
cmp    $0x79,%eax
jne    1284 <upgrade_st+0x84>
jmp    1306 <upgrade_st+0x106>
cmpl   $0x2,-0x54(%rbp)
jne    12dd <upgrade_st+0xdd>
mov    -0x4(%rbp),%edi
mov    $0x2580,%esi
call   1090 <set_tty_defaults@plt>
mov    -0x4(%rbp),%edi
lea    0xe5a(%rip),%rsi
call   1100 <write_line@plt>
mov    -0x4(%rbp),%edi
lea    0xd80(%rip),%rsi
call   1100 <write_line@plt>
mov    $0x4e20,%edi
call   1120 <usleep@plt>
mov    -0x4(%rbp),%edi
call   10d0 <flush_bytes@plt>
mov    -0x4(%rbp),%edi
call   1030 <st_termsetup@plt>
mov    $0x4e20,%edi
call   1120 <usleep@plt>
cmpl   $0x4,-0x54(%rbp)
jne    12f3 <upgrade_st+0xf3>
lea    0xd4a(%rip),%rdi
call   10f0 <die@plt>
jmp    12f8 <upgrade_st+0xf8>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    123b <upgrade_st+0x3b>
mov    $0x1,%edi
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1110 <info@plt>
mov    -0x4(%rbp),%edi
lea    0xd70(%rip),%rsi
mov    $0x2,%edx
mov    $0x5,%ecx
call   1070 <st_cmd@plt>
mov    -0x4(%rbp),%edi
lea    0xd5a(%rip),%rsi
mov    $0x2,%edx
mov    $0x5,%ecx
call   1070 <st_cmd@plt>
mov    -0x4(%rbp),%edi
lea    0xd44(%rip),%rsi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
mov    -0x4(%rbp),%edi
lea    0xd2e(%rip),%rsi
mov    $0x41,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
mov    $0x1,%edi
lea    0xd55(%rip),%rsi
mov    $0x0,%al
call   1110 <info@plt>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x40,%esi
call   10a0 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x81(%rbp)
je     13c7 <upgrade_st+0x1c7>
movsbl -0x50(%rbp),%eax
cmp    $0x3a,%eax
sete   %al
mov    %al,-0x81(%rbp)
mov    -0x81(%rbp),%al
test   $0x1,%al
jne    13da <upgrade_st+0x1da>
jmp    159f <upgrade_st+0x39f>
lea    -0x50(%rbp),%rdi
mov    $0xa,%esi
call   1080 <index@plt>
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
cmpb   $0x0,(%rax)
je     1400 <upgrade_st+0x200>
mov    -0x80(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x50(%rbp),%rdi
mov    $0xd,%esi
call   1080 <index@plt>
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rax
cmpb   $0x0,(%rax)
je     1426 <upgrade_st+0x226>
mov    -0x80(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x50(%rbp),%rdx
mov    $0x3,%edi
lea    0xcb8(%rip),%rsi
mov    $0x0,%al
call   1110 <info@plt>
movsbl -0x49(%rbp),%eax
cmp    $0x30,%eax
jne    1457 <upgrade_st+0x257>
movsbl -0x48(%rbp),%eax
cmp    $0x30,%eax
je     145c <upgrade_st+0x25c>
jmp    1390 <upgrade_st+0x190>
mov    -0x4(%rbp),%edi
lea    0xc9a(%rip),%rsi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
lea    -0x70(%rbp),%rdi
lea    0xca4(%rip),%rsi
mov    $0x5,%edx
call   10b0 <memcpy@plt>
lea    -0x70(%rbp),%rdi
add    $0x2,%rdi
lea    -0x50(%rbp),%rsi
add    $0x3,%rsi
call   10c0 <st_parsehex@plt>
lea    -0x70(%rbp),%rdi
add    $0x3,%rdi
lea    -0x50(%rbp),%rsi
add    $0x5,%rsi
call   10c0 <st_parsehex@plt>
lea    -0x70(%rbp),%rdi
mov    $0x4,%esi
call   1040 <st_xor@plt>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x5,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
lea    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0xd,-0x74(%rbp)
jl     14f8 <upgrade_st+0x2f8>
cmpl   $0x2b,-0x74(%rbp)
jle    1518 <upgrade_st+0x318>
lea    -0x50(%rbp),%rdx
xor    %edi,%edi
lea    0xbfe(%rip),%rsi
mov    $0x0,%al
call   1110 <info@plt>
movl   $0x1,-0x58(%rbp)
jmp    1390 <upgrade_st+0x190>
mov    -0x74(%rbp),%eax
sub    $0xb,%eax
sar    $0x1,%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
sub    $0x1,%eax
mov    %al,-0x70(%rbp)
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%eax
cmp    -0x74(%rbp),%eax
jge    1574 <upgrade_st+0x374>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
cltq
lea    -0x70(%rbp),%rdi
add    %rax,%rdi
mov    -0x78(%rbp),%eax
shl    $0x1,%eax
add    $0x9,%eax
cltq
lea    -0x50(%rbp),%rsi
add    %rax,%rsi
call   10c0 <st_parsehex@plt>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    1534 <upgrade_st+0x334>
lea    -0x70(%rbp),%rdi
mov    -0x74(%rbp),%esi
add    $0x1,%esi
call   1040 <st_xor@plt>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    -0x74(%rbp),%edx
add    $0x2,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
jmp    1390 <upgrade_st+0x190>
mov    $0x1,%edi
lea    0xb53(%rip),%rsi
mov    $0x0,%al
call   1110 <info@plt>
mov    -0x4(%rbp),%edi
lea    0xb65(%rip),%rsi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
mov    -0x4(%rbp),%edi
lea    0xb54(%rip),%rsi
mov    $0x5,%edx
xor    %ecx,%ecx
call   1070 <st_cmd@plt>
mov    -0x58(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upgrade_st>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsi,0x28(%rsp)
mov    %edi,%r12d
call   1030 <st_termsetup@plt>
lea    0xdde(%rip),%rsi
xor    %ebp,%ebp
mov    $0x1,%edi
xor    %eax,%eax
call   1110 <info@plt>
mov    %r12d,%edi
call   10d0 <flush_bytes@plt>
lea    0xdf3(%rip),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   1050 <write@plt>
lea    0x30(%rsp),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   10e0 <st_getbytes@plt>
test   %rax,%rax
jne    126e <upgrade_st+0x6e>
cmpb   $0x79,0x30(%rsp)
je     133b <upgrade_st+0x13b>
lea    0xdbd(%rip),%rbx
lea    0x30(%rsp),%r14
lea    0xe7e(%rip),%r13
lea    0xdac(%rip),%r15
nopl   0x0(%rax,%rax,1)
cmp    $0x4,%ebp
je     12f0 <upgrade_st+0xf0>
cmp    $0x2,%ebp
jne    12fc <upgrade_st+0xfc>
mov    %r12d,%edi
mov    $0x2580,%esi
call   1090 <set_tty_defaults@plt>
mov    %r12d,%edi
mov    %r13,%rsi
call   1100 <write_line@plt>
mov    %r12d,%edi
mov    %r15,%rsi
call   1100 <write_line@plt>
mov    $0x4e20,%edi
call   1120 <usleep@plt>
mov    %r12d,%edi
call   10d0 <flush_bytes@plt>
mov    %r12d,%edi
call   1030 <st_termsetup@plt>
mov    $0x4e20,%edi
call   1120 <usleep@plt>
jmp    12fc <upgrade_st+0xfc>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xd41(%rip),%rdi
call   10f0 <die@plt>
add    $0x1,%ebp
mov    %r12d,%edi
call   10d0 <flush_bytes@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10e0 <st_getbytes@plt>
test   %rax,%rax
jne    1290 <upgrade_st+0x90>
cmpb   $0x79,0x30(%rsp)
jne    1290 <upgrade_st+0x90>
lea    0xd45(%rip),%rsi
movl   $0x0,0xc(%rsp)
mov    $0x1,%edi
xor    %eax,%eax
call   1110 <info@plt>
lea    0xd36(%rip),%rsi
mov    %r12d,%edi
mov    $0x2,%edx
mov    $0x5,%ecx
call   1070 <st_cmd@plt>
lea    0xd20(%rip),%rsi
mov    %r12d,%edi
mov    $0x2,%edx
mov    $0x5,%ecx
call   1070 <st_cmd@plt>
lea    0xd0a(%rip),%rsi
mov    %r12d,%edi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
lea    0xcf4(%rip),%rsi
mov    %r12d,%edi
mov    $0x41,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
lea    0xd1d(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1110 <info@plt>
lea    0x30(%rsp),%rdi
mov    $0x40,%esi
mov    0x28(%rsp),%rbx
mov    %rbx,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     15c7 <upgrade_st+0x3c7>
cmpb   $0x3a,0x30(%rsp)
jne    15c7 <upgrade_st+0x3c7>
lea    0x12(%rsp),%r14
lea    0x30(%rsp),%r15
lea    0xce5(%rip),%rbp
movl   $0x0,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0xa,%esi
call   1080 <index@plt>
cmpb   $0x0,(%rax)
je     1435 <upgrade_st+0x235>
movb   $0x0,(%rax)
mov    %r15,%rdi
mov    $0xd,%esi
call   1080 <index@plt>
cmpb   $0x0,(%rax)
je     144a <upgrade_st+0x24a>
movb   $0x0,(%rax)
mov    $0x3,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1110 <info@plt>
cmpb   $0x30,0x37(%rsp)
jne    15a7 <upgrade_st+0x3a7>
cmpb   $0x30,0x38(%rsp)
jne    15a7 <upgrade_st+0x3a7>
mov    %r12d,%edi
lea    0xc84(%rip),%rsi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
lea    0xc8a(%rip),%rsi
mov    $0x5,%edx
call   10b0 <memcpy@plt>
mov    %r14,%r13
mov    %r14,%rdi
lea    0x33(%rsp),%rsi
call   10c0 <st_parsehex@plt>
lea    0x13(%rsp),%rdi
lea    0x35(%rsp),%rsi
call   10c0 <st_parsehex@plt>
mov    %rbp,%rdi
mov    $0x4,%esi
call   1040 <st_xor@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x5,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %r15,%r14
mov    %eax,%r15d
lea    -0x2c(%r15),%eax
cmp    $0xffffffe0,%eax
ja     1530 <upgrade_st+0x330>
xor    %edi,%edi
lea    0xbfe(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1110 <info@plt>
movl   $0x1,0xc(%rsp)
lea    0xbd0(%rip),%rbp
mov    %r14,%r15
mov    %r13,%r14
jmp    15a7 <upgrade_st+0x3a7>
nopl   0x0(%rax)
add    $0xfffffff5,%r15d
shr    %r15d
lea    -0x1(%r15),%eax
mov    %al,0x10(%rsp)
lea    0x11(%rsp),%r13
mov    %r15,%rbp
lea    0x39(%rsp),%r14
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r14,%rsi
call   10c0 <st_parsehex@plt>
add    $0x2,%r14
add    $0x1,%r13
add    $0xffffffffffffffff,%rbp
jne    1550 <upgrade_st+0x350>
lea    0x1(%r15),%esi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <st_xor@plt>
add    $0x2,%r15d
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
mov    0x28(%rsp),%rbx
lea    0x12(%rsp),%r14
lea    0xb4c(%rip),%rbp
lea    0x30(%rsp),%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %rbx,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     15c7 <upgrade_st+0x3c7>
cmpb   $0x3a,0x30(%rsp)
je     1420 <upgrade_st+0x220>
lea    0xb30(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1110 <info@plt>
lea    0xb40(%rip),%rsi
mov    %r12d,%edi
mov    $0x2,%edx
mov    $0x1,%ecx
call   1070 <st_cmd@plt>
lea    0xb2f(%rip),%rsi
mov    %r12d,%edi
mov    $0x5,%edx
xor    %ecx,%ecx
call   1070 <st_cmd@plt>
mov    0xc(%rsp),%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [50 x i8] c"ST upgrade: attempting to sync up with target...\0A\00"
@.str.1 = [2 x i8] c"\7F\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [4 x i8] c">CB\00"
@.str.4 = [79 x i8] c"error: can't establish communication with target. Cycle power and try again.\0A\00"
@.str.5 = [12 x i8] c"Erasing...\0A\00"
@.str.6 = [3 x i8] c"\01\FE\00"
@.str.7 = [3 x i8] c"\02\FD\00"
@.str.8 = [3 x i8] c"C\BC\00"
@.str.9 = [66 x i8] c">\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F\10\11\12\13\14\15\16\17\18\19\1A\1B\1C\1D\1E\1F !\22#$%&'()*+,-./0123456789:;<=>?>\00"
@.str.10 = [16 x i8] c"Programming...\0A\00"
@.str.11 = [18 x i8] c"processing: '%s'\0A\00"
@.str.12 = [3 x i8] c"1\CE\00"
@.str.13 = [5 x i8] c"\08\00\00\00\00"
@.str.14 = [30 x i8] c"warning: malformed line '%s'\0A\00"
@.str.15 = [2 x i8] c"\0A\00"
@.str.16 = [3 x i8] c"!\DE\00"
@.str.17 = [6 x i8] c"\08\00\00\00\08\00"
define i32 @upgrade_st(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [64 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca [18 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %7
%12 = load i32, i32* %3
%13 = call i32 @st_termsetup(i32 %12)
%14 = call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %15
15: ; preds = %52, %2
%16 = load i32, i32* %3
%17 = call i32 @flush_bytes(i32 %16)
%18 = load i32, i32* %3
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%20 = load i32, i32* %3
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%22 = call i64 @st_getbytes(i32 %20, i8* %21, i32 1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %15
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 121
br i1 %28, label %29, label %30
29: ; preds = %24
br label %55
30: ; preds = %24, %15
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 2
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load i32, i32* %3
%35 = call i32 @set_tty_defaults(i32 %34, i32 9600)
%36 = load i32, i32* %3
%37 = call i32 @write_line(i32 %36, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %3
%39 = call i32 @write_line(i32 %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @usleep(i32 20000)
%41 = load i32, i32* %3
%42 = call i32 @flush_bytes(i32 %41)
%43 = load i32, i32* %3
%44 = call i32 @st_termsetup(i32 %43)
%45 = call i32 @usleep(i32 20000)
br label %46
46: ; preds = %33, %30
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, 4
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @die(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %15
55: ; preds = %29
%56 = call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32, i32* %3
%58 = call i32 @st_cmd(i32 %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2, i32 5)
%59 = load i32, i32* %3
%60 = call i32 @st_cmd(i32 %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2, i32 5)
%61 = load i32, i32* %3
%62 = call i32 @st_cmd(i32 %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 2, i32 1)
%63 = load i32, i32* %3
%64 = call i32 @st_cmd(i32 %63, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.9, i64 0, i64 0), i32 65, i32 1)
%65 = call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
br label %66
66: ; preds = %159, %130, %106, %55
%67 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%68 = load i32*, i32** %4
%69 = call i32* @fgets(i8* %67, i32 64, i32* %68)
%70 = icmp ne i32* %69, null
br i1 %70, label %71, label %76
71: ; preds = %66
%72 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp eq i32 %74, 58
br label %76
76: ; preds = %71, %66
%77 = phi i1 [ false, %66 ], [ %75, %71 ]
br i1 %77, label %78, label %169
78: ; preds = %76
%79 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%80 = call i8* @index(i8* %79, i8 signext 10)
store i8* %80, i8** %11
%81 = load i8*, i8** %11
%82 = load i8, i8* %81
%83 = icmp ne i8 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %78
%85 = load i8*, i8** %11
store i8 0, i8* %85
br label %86
86: ; preds = %84, %78
%87 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%88 = call i8* @index(i8* %87, i8 signext 13)
store i8* %88, i8** %11
%89 = load i8*, i8** %11
%90 = load i8, i8* %89
%91 = icmp ne i8 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %86
%93 = load i8*, i8** %11
store i8 0, i8* %93
br label %94
94: ; preds = %92, %86
%95 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%96 = call i32 (i32, i8*, ...) @info(i32 3, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i8* %95)
%97 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 7
%98 = load i8, i8* %97
%99 = sext i8 %98 to i32
%100 = icmp ne i32 %99, 48
br i1 %100, label %106, label %101
101: ; preds = %94
%102 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 8
%103 = load i8, i8* %102
%104 = sext i8 %103 to i32
%105 = icmp ne i32 %104, 48
br i1 %105, label %106, label %107
106: ; preds = %101, %94
br label %66
107: ; preds = %101
%108 = load i32, i32* %3
%109 = call i32 @st_cmd(i32 %108, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i32 2, i32 1)
%110 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%111 = call i32 @memcpy(i8* %110, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0), i32 5)
%112 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 2
%113 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 3
%114 = call i32 @st_parsehex(i8* %112, i8* %113)
%115 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 3
%116 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 5
%117 = call i32 @st_parsehex(i8* %115, i8* %116)
%118 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%119 = call i32 @st_xor(i8* %118, i32 4)
%120 = load i32, i32* %3
%121 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%122 = call i32 @st_cmd(i32 %120, i8* %121, i32 5, i32 1)
%123 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%124 = call i32 @strlen(i8* %123)
store i32 %124, i32* %9
%125 = load i32, i32* %9
%126 = icmp slt i32 %125, 13
br i1 %126, label %130, label %127
127: ; preds = %107
%128 = load i32, i32* %9
%129 = icmp sgt i32 %128, 43
br i1 %129, label %130, label %133
130: ; preds = %127, %107
%131 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%132 = call i32 (i32, i8*, ...) @info(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.14, i64 0, i64 0), i8* %131)
store i32 1, i32* %7
br label %66
133: ; preds = %127
%134 = load i32, i32* %9
%135 = sub nsw i32 %134, 11
%136 = ashr i32 %135, 1
store i32 %136, i32* %9
%137 = load i32, i32* %9
%138 = sub nsw i32 %137, 1
%139 = trunc i32 %138 to i8
%140 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
store i8 %139, i8* %140
store i32 0, i32* %10
br label %141
141: ; preds = %156, %133
%142 = load i32, i32* %10
%143 = load i32, i32* %9
%144 = icmp slt i32 %142, %143
br i1 %144, label %145, label %159
145: ; preds = %141
%146 = load i32, i32* %10
%147 = add nsw i32 %146, 1
%148 = sext i32 %147 to i64
%149 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 %148
%150 = load i32, i32* %10
%151 = mul nsw i32 %150, 2
%152 = add nsw i32 9, %151
%153 = sext i32 %152 to i64
%154 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 %153
%155 = call i32 @st_parsehex(i8* %149, i8* %154)
br label %156
156: ; preds = %145
%157 = load i32, i32* %10
%158 = add nsw i32 %157, 1
store i32 %158, i32* %10
br label %141
159: ; preds = %141
%160 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%161 = load i32, i32* %9
%162 = add nsw i32 %161, 1
%163 = call i32 @st_xor(i8* %160, i32 %162)
%164 = load i32, i32* %3
%165 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%166 = load i32, i32* %9
%167 = add nsw i32 %166, 2
%168 = call i32 @st_cmd(i32 %164, i8* %165, i32 %167, i32 1)
br label %66
169: ; preds = %76
%170 = call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.15, i64 0, i64 0))
%171 = load i32, i32* %3
%172 = call i32 @st_cmd(i32 %171, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32 2, i32 1)
%173 = load i32, i32* %3
%174 = call i32 @st_cmd(i32 %173, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.17, i64 0, i64 0), i32 5, i32 0)
%175 = load i32, i32* %7
ret i32 %175
}

LLVM IR (-O2):
@.str = [50 x i8] c"ST upgrade: attempting to sync up with target...\0A\00"
@.str.1 = [2 x i8] c"\7F\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [4 x i8] c">CB\00"
@.str.4 = [79 x i8] c"error: can't establish communication with target. Cycle power and try again.\0A\00"
@.str.5 = [12 x i8] c"Erasing...\0A\00"
@.str.6 = [3 x i8] c"\01\FE\00"
@.str.7 = [3 x i8] c"\02\FD\00"
@.str.8 = [3 x i8] c"C\BC\00"
@.str.9 = [66 x i8] c">\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F\10\11\12\13\14\15\16\17\18\19\1A\1B\1C\1D\1E\1F !\22#$%&'()*+,-./0123456789:;<=>?>\00"
@.str.10 = [16 x i8] c"Programming...\0A\00"
@.str.11 = [18 x i8] c"processing: '%s'\0A\00"
@.str.12 = [3 x i8] c"1\CE\00"
@.str.13 = [5 x i8] c"\08\00\00\00\00"
@.str.14 = [30 x i8] c"warning: malformed line '%s'\0A\00"
@.str.15 = [2 x i8] c"\0A\00"
@.str.16 = [3 x i8] c"!\DE\00"
@.str.17 = [6 x i8] c"\08\00\00\00\08\00"
define i32 @upgrade_st(i32 %0, i32* nocapture %1) {
%3 = alloca [64 x i8]
%4 = alloca [18 x i8]
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @st_termsetup(i32 %0)
%7 = tail call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @flush_bytes(i32 %0)
%9 = call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%10 = call i64 @st_getbytes(i32 %0, i8* %5, i32 1)
%11 = icmp eq i64 %10, 0
%12 = load i8, i8* %5
%13 = icmp eq i8 %12, 121
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %36, label %15
15: ; preds = %2, %27
%16 = phi i32 [ %28, %27 ], [ 0, %2 ]
switch i32 %16, label %27 [
i32 2, label %17
i32 4, label %25
]
17: ; preds = %15
%18 = call i32 @set_tty_defaults(i32 %0, i32 9600)
%19 = call i32 @write_line(i32 %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @write_line(i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @usleep(i32 20000)
%22 = call i32 @flush_bytes(i32 %0)
%23 = call i32 @st_termsetup(i32 %0)
%24 = call i32 @usleep(i32 20000)
br label %27
25: ; preds = %15
%26 = call i32 @die(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %17, %15, %25
%28 = add nuw nsw i32 %16, 1
%29 = call i32 @flush_bytes(i32 %0)
%30 = call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%31 = call i64 @st_getbytes(i32 %0, i8* %5, i32 1)
%32 = icmp eq i64 %31, 0
%33 = load i8, i8* %5
%34 = icmp eq i8 %33, 121
%35 = select i1 %32, i1 %34, i1 false
br i1 %35, label %36, label %15
36: ; preds = %27, %2
%37 = call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @st_cmd(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 2, i32 5)
%39 = call i32 @st_cmd(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32 2, i32 5)
%40 = call i32 @st_cmd(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i32 2, i32 1)
%41 = call i32 @st_cmd(i32 %0, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.9, i64 0, i64 0), i32 65, i32 1)
%42 = call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
%43 = call i32* @fgets(i8* %5, i32 64, i32* %1)
%44 = icmp ne i32* %43, null
%45 = load i8, i8* %5
%46 = icmp eq i8 %45, 58
%47 = select i1 %44, i1 %46, i1 false
br i1 %47, label %48, label %113
48: ; preds = %36
%49 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 0
%50 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 7
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 8
%52 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 2
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 3
%54 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 3
%55 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 5
br label %56
56: ; preds = %48, %106
%57 = phi i32 [ 0, %48 ], [ %107, %106 ]
%58 = call i8* @index(i8* %5, i8 signext 10)
%59 = load i8, i8* %58
%60 = icmp eq i8 %59, 0
br i1 %60, label %62, label %61
61: ; preds = %56
store i8 0, i8* %58
br label %62
62: ; preds = %61, %56
%63 = call i8* @index(i8* %5, i8 signext 13)
%64 = load i8, i8* %63
%65 = icmp eq i8 %64, 0
br i1 %65, label %67, label %66
66: ; preds = %62
store i8 0, i8* %63
br label %67
67: ; preds = %66, %62
%68 = call i32 (i32, i8*, ...) @info(i32 3, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0), i8* %5)
%69 = load i8, i8* %50
%70 = icmp ne i8 %69, 48
%71 = load i8, i8* %51
%72 = icmp ne i8 %71, 48
%73 = select i1 %70, i1 true, i1 %72
br i1 %73, label %106, label %74
74: ; preds = %67
%75 = call i32 @st_cmd(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i32 2, i32 1)
%76 = call i32 @memcpy(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.13, i64 0, i64 0), i32 5)
%77 = call i32 @st_parsehex(i8* %52, i8* %53)
%78 = call i32 @st_parsehex(i8* %54, i8* %55)
%79 = call i32 @st_xor(i8* %49, i32 4)
%80 = call i32 @st_cmd(i32 %0, i8* %49, i32 5, i32 1)
%81 = call i32 @strlen(i8* %5)
%82 = add i32 %81, -44
%83 = icmp ult i32 %82, -31
br i1 %83, label %84, label %86
84: ; preds = %74
%85 = call i32 (i32, i8*, ...) @info(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.14, i64 0, i64 0), i8* %5)
br label %106
86: ; preds = %74
%87 = add nsw i32 %81, -11
%88 = lshr i32 %87, 1
%89 = trunc i32 %88 to i8
%90 = add nsw i8 %89, -1
store i8 %90, i8* %49
%91 = zext i32 %88 to i64
br label %92
92: ; preds = %86, %92
%93 = phi i64 [ 0, %86 ], [ %94, %92 ]
%94 = add nuw nsw i64 %93, 1
%95 = getelementptr inbounds [18 x i8], [18 x i8]* %4, i64 0, i64 %94
%96 = shl nuw nsw i64 %93, 1
%97 = add nuw nsw i64 %96, 9
%98 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 %97
%99 = call i32 @st_parsehex(i8* %95, i8* %98)
%100 = icmp eq i64 %94, %91
br i1 %100, label %101, label %92
101: ; preds = %92
%102 = add nuw nsw i32 %88, 1
%103 = call i32 @st_xor(i8* %49, i32 %102)
%104 = add nuw nsw i32 %88, 2
%105 = call i32 @st_cmd(i32 %0, i8* %49, i32 %104, i32 1)
br label %106
106: ; preds = %67, %101, %84
%107 = phi i32 [ 1, %84 ], [ %57, %101 ], [ %57, %67 ]
%108 = call i32* @fgets(i8* %5, i32 64, i32* %1)
%109 = icmp ne i32* %108, null
%110 = load i8, i8* %5
%111 = icmp eq i8 %110, 58
%112 = select i1 %109, i1 %111, i1 false
br i1 %112, label %56, label %113
113: ; preds = %106, %36
%114 = phi i32 [ 0, %36 ], [ %107, %106 ]
%115 = call i32 (i32, i8*, ...) @info(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.15, i64 0, i64 0))
%116 = call i32 @st_cmd(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.16, i64 0, i64 0), i32 2, i32 1)
%117 = call i32 @st_cmd(i32 %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.17, i64 0, i64 0), i32 5, i32 0)
ret i32 %114
}