#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 */
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
scalar_t__ EINTR;
int POLLHUP;
int POLLIN;
int POLLPRI;
scalar_t__ errno;
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int const, unsigned char *, int);
int /*<<< orphan*/ sleep(int);
int time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int const, unsigned char *, int);
int write_segment(int const, unsigned char *, unsigned int, unsigned int, int);
int write_flash(const int fd, unsigned char *data, unsigned int addrmin,
                unsigned int addrmax, int memsize, const int timeout) {
  int nfd;
  int tic = time(NULL);
  int retry = 0;
  unsigned int segmentptr = 0;
  int segmentsize = 0;
  int bootsize = 0;
  int flashsize = 0;
  char triggered = 0;
  struct pollfd pfd[1];
  if (fd) {
    pfd[0].fd = fd;
    pfd[0].events = POLLIN | POLLPRI;
  } else {
    return -1;
  }
  while ((time(NULL) - tic) < timeout || triggered) {
    unsigned char c = 0;
    static int segmentresult = 1;
    if (segmentresult == 0) {
      break;
    }
    if ((nfd = poll(pfd, 1, 1000)) == -1 && errno != EINTR) {
      sleep(60);
    }
    if (pfd[0].revents & POLLHUP) {
      pfd[0].fd = -1;
      continue;
    }
    if (read(fd, &c, 1) && c) {
      switch (c) {
      case 'U':
        write(fd, &c, 1);
        break;
      case '>':
        c = '<';
        write(fd, &c, 1);
        triggered = 1;
        printf("writing flash... please wait!\n");
        break;
      case '!':
        segmentresult =
            write_segment(fd, data, segmentptr, addrmax, segmentsize);
        segmentptr++;
        break;
      case '@':
        retry++;
        segmentptr--;
        segmentresult =
            write_segment(fd, data, segmentptr, addrmax, segmentsize);
        segmentptr++;
        break;
      case '%':
        c = 0x00;
        write(fd, &c, 1);
        c = 0xFF;
        write(fd, &c, 1);
        break;
      default:
        break;
      }
      if (retry > 3) {
        printf("Programming failed!\n");
        c = 0xff;
        write(fd, &c, 1);
        write(fd, &c, 1);
        return 0;
      }
      switch (c) {
      case 'Q':
        segmentsize = 32;
        break;
      case 'R':
        segmentsize = 64;
        break;
      case 'S':
        segmentsize = 128;
        break;
      case 'T':
        segmentsize = 256;
        break;
      case 'V':
        segmentsize = 512;
        break;
      }
      switch (c) {
      case 'a':
        bootsize = 128;
        break;
      case 'b':
        bootsize = 256;
        break;
      case 'c':
        bootsize = 512;
        break;
      case 'd':
        bootsize = 1024;
        break;
      case 'e':
        bootsize = 2048;
        break;
      case 'f':
        bootsize = 4096;
        break;
      }
      switch (c) {
      case 'g':
        flashsize = 1024;
        break;
      case 'h':
        flashsize = 2048;
        break;
      case 'i':
        flashsize = 4096;
        break;
      case 'l':
        flashsize = 8196;
        break;
      case 'm':
        flashsize = 16384;
        break;
      case 'n':
        flashsize = 32768;
        break;
      case 'o':
        flashsize = 65536;
        break;
      case 'p':
        flashsize = 131072;
        break;
      case 'q':
        flashsize = 262144;
        break;
      case 'r':
        flashsize = 40960;
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movb   $0x0,-0x3d(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11ea <write_flash+0x7a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    0x2e10(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x4c(%rbp)
jmp    11f6 <write_flash+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    15cd <write_flash+0x45d>
jmp    11fb <write_flash+0x8b>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%ecx
sub    -0x28(%rbp),%ecx
mov    $0x1,%al
cmp    -0x20(%rbp),%ecx
mov    %al,-0x52(%rbp)
jl     1224 <write_flash+0xb4>
movsbl -0x3d(%rbp),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x52(%rbp)
mov    -0x52(%rbp),%al
test   $0x1,%al
jne    1234 <write_flash+0xc4>
jmp    15c6 <write_flash+0x456>
movb   $0x0,-0x51(%rbp)
cmpl   $0x0,0x2e19(%rip)
jne    124a <write_flash+0xda>
jmp    15c6 <write_flash+0x456>
lea    -0x50(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1070 <poll@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    128d <write_flash+0x11d>
mov    0x2d58(%rip),%rax
mov    (%rax),%rax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
je     128d <write_flash+0x11d>
mov    $0x3c,%edi
call   1080 <sleep@plt>
mov    -0x48(%rbp),%eax
mov    0x2d49(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12ae <write_flash+0x13e>
movl   $0xffffffff,-0x50(%rbp)
jmp    11fb <write_flash+0x8b>
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     15c1 <write_flash+0x451>
movzbl -0x51(%rbp),%eax
cmp    $0x0,%eax
je     15c1 <write_flash+0x451>
movzbl -0x51(%rbp),%eax
mov    %eax,-0x58(%rbp)
sub    $0x21,%eax
je     136f <write_flash+0x1ff>
jmp    12eb <write_flash+0x17b>
mov    -0x58(%rbp),%eax
sub    $0x25,%eax
je     13d5 <write_flash+0x265>
jmp    12fc <write_flash+0x18c>
mov    -0x58(%rbp),%eax
sub    $0x3e,%eax
je     1345 <write_flash+0x1d5>
jmp    130d <write_flash+0x19d>
mov    -0x58(%rbp),%eax
sub    $0x40,%eax
je     1399 <write_flash+0x229>
jmp    131e <write_flash+0x1ae>
mov    -0x58(%rbp),%eax
sub    $0x55,%eax
jne    1404 <write_flash+0x294>
jmp    132f <write_flash+0x1bf>
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1409 <write_flash+0x299>
movb   $0x3c,-0x51(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x1,-0x3d(%rbp)
lea    0xcfb(%rip),%rdi
call   1040 <printf@plt>
jmp    1409 <write_flash+0x299>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x34(%rbp),%r8d
call   1090 <write_segment@plt>
mov    %eax,0x2ccd(%rip)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1409 <write_flash+0x299>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x34(%rbp),%r8d
call   1090 <write_segment@plt>
mov    %eax,0x2c91(%rip)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1409 <write_flash+0x299>
movb   $0x0,-0x51(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0xff,-0x51(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1409 <write_flash+0x299>
jmp    1409 <write_flash+0x299>
cmpl   $0x3,-0x2c(%rbp)
jle    1451 <write_flash+0x2e1>
lea    0xc65(%rip),%rdi
call   1040 <printf@plt>
movb   $0xff,-0x51(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
jmp    15cd <write_flash+0x45d>
movzbl -0x51(%rbp),%eax
add    $0xffffffaf,%eax
mov    %eax,%ecx
mov    %rcx,-0x60(%rbp)
sub    $0x5,%eax
ja     14b2 <write_flash+0x342>
mov    -0x60(%rbp),%rax
lea    0xb8e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x20,-0x34(%rbp)
jmp    14b2 <write_flash+0x342>
movl   $0x40,-0x34(%rbp)
jmp    14b2 <write_flash+0x342>
movl   $0x80,-0x34(%rbp)
jmp    14b2 <write_flash+0x342>
movl   $0x100,-0x34(%rbp)
jmp    14b2 <write_flash+0x342>
movl   $0x200,-0x34(%rbp)
movzbl -0x51(%rbp),%eax
add    $0xffffff9f,%eax
mov    %eax,%ecx
mov    %rcx,-0x68(%rbp)
sub    $0x5,%eax
ja     151f <write_flash+0x3af>
mov    -0x68(%rbp),%rax
lea    0xb45(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x80,-0x38(%rbp)
jmp    151f <write_flash+0x3af>
movl   $0x100,-0x38(%rbp)
jmp    151f <write_flash+0x3af>
movl   $0x200,-0x38(%rbp)
jmp    151f <write_flash+0x3af>
movl   $0x400,-0x38(%rbp)
jmp    151f <write_flash+0x3af>
movl   $0x800,-0x38(%rbp)
jmp    151f <write_flash+0x3af>
movl   $0x1000,-0x38(%rbp)
movzbl -0x51(%rbp),%eax
add    $0xffffff99,%eax
mov    %eax,%ecx
mov    %rcx,-0x70(%rbp)
sub    $0xb,%eax
ja     15bc <write_flash+0x44c>
mov    -0x70(%rbp),%rax
lea    0xaf0(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x400,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x800,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x1000,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x2004,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x4000,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x8000,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x10000,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x20000,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0x40000,-0x3c(%rbp)
jmp    15bc <write_flash+0x44c>
movl   $0xa000,-0x3c(%rbp)
jmp    15c1 <write_flash+0x451>
jmp    11fb <write_flash+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_flash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%r15d
mov    %ecx,%ebp
mov    %rsi,%rbx
mov    %edi,%r12d
xor    %r14d,%r14d
xor    %edi,%edi
call   1060 <time@plt>
test   %r12d,%r12d
je     13eb <write_flash+0x27b>
mov    %eax,%r13d
mov    %rbx,0x20(%rsp)
mov    %ebp,0x8(%rsp)
mov    %r12d,0x10(%rsp)
mov    0x2e24(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x14(%rsp)
xor    %edi,%edi
call   1060 <time@plt>
sub    %r13d,%eax
cmp    %r15d,%eax
jge    142c <write_flash+0x2bc>
xor    %ebx,%ebx
movl   $0x0,0x4(%rsp)
xor    %ebp,%ebp
movl   $0x0,0xc(%rsp)
nopw   0x0(%rax,%rax,1)
movb   $0x0,0x3(%rsp)
cmpl   $0x0,0x2e5c(%rip)
je     1429 <write_flash+0x2b9>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3e8,%edx
call   1070 <poll@plt>
cmp    $0xffffffff,%eax
jne    1240 <write_flash+0xd0>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%rax
je     1240 <write_flash+0xd0>
mov    $0x3c,%edi
call   1080 <sleep@plt>
nopl   0x0(%rax,%rax,1)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
test   %eax,0x18(%rsp)
je     1280 <write_flash+0x110>
movl   $0xffffffff,0x10(%rsp)
ff
xor    %r14d,%r14d
xor    %edi,%edi
call   1060 <time@plt>
sub    %r13d,%eax
cmp    %r15d,%eax
jl     11f0 <write_flash+0x80>
test   %bl,%bl
jne    11f0 <write_flash+0x80>
jmp    142c <write_flash+0x2bc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1257 <write_flash+0xe7>
movzbl 0x3(%rsp),%eax
test   %al,%al
je     1257 <write_flash+0xe7>
lea    -0x21(%rax),%ecx
cmp    $0x1f,%cl
ja     12e3 <write_flash+0x173>
movzbl %cl,%ecx
lea    0xd4e(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    %r12d,%edi
mov    0x20(%rsp),%rsi
mov    %ebp,%edx
mov    0x8(%rsp),%ecx
mov    0x4(%rsp),%r8d
call   1090 <write_segment@plt>
mov    %eax,0x2d7f(%rip)
add    $0x1,%ebp
mov    $0x21,%al
jmp    1386 <write_flash+0x216>
cmp    $0x55,%al
jne    1386 <write_flash+0x216>
mov    %r12d,%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0x55,%al
jmp    1386 <write_flash+0x216>
movb   $0x0,0x3(%rsp)
mov    %r12d,%edi
lea    0x3(%rsp),%r14
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movb   $0xff,0x3(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    $0xff,%al
jmp    1386 <write_flash+0x216>
movb   $0x3c,0x3(%rsp)
mov    %r12d,%edi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xd57(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%bl
mov    $0x3c,%al
jmp    1386 <write_flash+0x216>
addl   $0x1,0xc(%rsp)
lea    -0x1(%rbp),%edx
mov    %r12d,%edi
mov    0x20(%rsp),%rsi
mov    0x8(%rsp),%ecx
mov    0x4(%rsp),%r8d
call   1090 <write_segment@plt>
mov    %eax,0x2cd4(%rip)
mov    $0x40,%al
cmpl   $0x4,0xc(%rsp)
jge    13f3 <write_flash+0x283>
add    $0xaf,%al
cmp    $0x5,%al
ja     1257 <write_flash+0xe7>
movzbl %al,%eax
lea    0xcdf(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x20,0x4(%rsp)
jmp    1257 <write_flash+0xe7>
movl   $0x40,0x4(%rsp)
jmp    1257 <write_flash+0xe7>
movl   $0x80,0x4(%rsp)
jmp    1257 <write_flash+0xe7>
movl   $0x100,0x4(%rsp)
jmp    1257 <write_flash+0xe7>
movl   $0x200,0x4(%rsp)
jmp    1257 <write_flash+0xe7>
mov    $0xffffffff,%r14d
jmp    142c <write_flash+0x2bc>
lea    0xc9e(%rip),%rdi
call   1030 <puts@plt>
movb   $0xff,0x3(%rsp)
lea    0x3(%rsp),%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLPRI = global i32 0
@write_flash.segmentresult = internal global i32 1
@errno = global i64 0
@EINTR = global i64 0
@POLLHUP = global i32 0
@.str = [31 x i8] c"writing flash... please wait!\0A\00"
@.str.1 = [21 x i8] c"Programming failed!\0A\00"
define i32 @write_flash(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i8
%22 = alloca [1 x %struct.pollfd]
%23 = alloca i8
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%24 = call i32 @time(i32* null)
store i32 %24, i32* %15
store i32 0, i32* %16
store i32 0, i32* %17
store i32 0, i32* %18
store i32 0, i32* %19
store i32 0, i32* %20
store i8 0, i8* %21
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %6
%28 = load i32, i32* %8
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %22, i64 0, i64 0
%30 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* @POLLIN
%32 = load i32, i32* @POLLPRI
%33 = or i32 %31, %32
%34 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %22, i64 0, i64 0
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %34, i32 0, i32 1
store i32 %33, i32* %35
br label %37
36: ; preds = %6
store i32 -1, i32* %7
br label %162
37: ; preds = %27
br label %38
38: ; preds = %160, %71, %37
%39 = call i32 @time(i32* null)
%40 = load i32, i32* %15
%41 = sub nsw i32 %39, %40
%42 = load i32, i32* %13
%43 = icmp slt i32 %41, %42
br i1 %43, label %48, label %44
44: ; preds = %38
%45 = load i8, i8* %21
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 0
br label %48
48: ; preds = %44, %38
%49 = phi i1 [ true, %38 ], [ %47, %44 ]
br i1 %49, label %50, label %161
50: ; preds = %48
store i8 0, i8* %23
%51 = load i32, i32* @write_flash.segmentresult
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %50
br label %161
54: ; preds = %50
%55 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %22, i64 0, i64 0
%56 = call i32 @poll(%struct.pollfd* %55, i32 1, i32 1000)
store i32 %56, i32* %14
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %64
58: ; preds = %54
%59 = load i64, i64* @errno
%60 = load i64, i64* @EINTR
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @sleep(i32 60)
br label %64
64: ; preds = %62, %58, %54
%65 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %22, i64 0, i64 0
%66 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %65, i32 0, i32 2
%67 = load i32, i32* %66
%68 = load i32, i32* @POLLHUP
%69 = and i32 %67, %68
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %64
%72 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %22, i64 0, i64 0
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %72, i32 0, i32 0
store i32 -1, i32* %73
br label %38
74: ; preds = %64
%75 = load i32, i32* %8
%76 = call i64 @read(i32 %75, i8* %23, i32 1)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %160
78: ; preds = %74
%79 = load i8, i8* %23
%80 = zext i8 %79 to i32
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %160
82: ; preds = %78
%83 = load i8, i8* %23
%84 = zext i8 %83 to i32
switch i32 %84, label %119 [
i32 85, label %85
i32 62, label %88
i32 33, label %92
i32 64, label %101
i32 37, label %114
]
85: ; preds = %82
%86 = load i32, i32* %8
%87 = call i32 @write(i32 %86, i8* %23, i32 1)
br label %120
88: ; preds = %82
store i8 60, i8* %23
%89 = load i32, i32* %8
%90 = call i32 @write(i32 %89, i8* %23, i32 1)
store i8 1, i8* %21
%91 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %120
92: ; preds = %82
%93 = load i32, i32* %8
%94 = load i8*, i8** %9
%95 = load i32, i32* %17
%96 = load i32, i32* %11
%97 = load i32, i32* %18
%98 = call i32 @write_segment(i32 %93, i8* %94, i32 %95, i32 %96, i32 %97)
store i32 %98, i32* @write_flash.segmentresult
%99 = load i32, i32* %17
%100 = add i32 %99, 1
store i32 %100, i32* %17
br label %120
101: ; preds = %82
%102 = load i32, i32* %16
%103 = add nsw i32 %102, 1
store i32 %103, i32* %16
%104 = load i32, i32* %17
%105 = add i32 %104, -1
store i32 %105, i32* %17
%106 = load i32, i32* %8
%107 = load i8*, i8** %9
%108 = load i32, i32* %17
%109 = load i32, i32* %11
%110 = load i32, i32* %18
%111 = call i32 @write_segment(i32 %106, i8* %107, i32 %108, i32 %109, i32 %110)
store i32 %111, i32* @write_flash.segmentresult
%112 = load i32, i32* %17
%113 = add i32 %112, 1
store i32 %113, i32* %17
br label %120
114: ; preds = %82
store i8 0, i8* %23
%115 = load i32, i32* %8
%116 = call i32 @write(i32 %115, i8* %23, i32 1)
store i8 -1, i8* %23
%117 = load i32, i32* %8
%118 = call i32 @write(i32 %117, i8* %23, i32 1)
br label %120
119: ; preds = %82
br label %120
120: ; preds = %119, %114, %101, %92, %88, %85
%121 = load i32, i32* %16
%122 = icmp sgt i32 %121, 3
br i1 %122, label %123, label %129
123: ; preds = %120
%124 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i8 -1, i8* %23
%125 = load i32, i32* %8
%126 = call i32 @write(i32 %125, i8* %23, i32 1)
%127 = load i32, i32* %8
%128 = call i32 @write(i32 %127, i8* %23, i32 1)
store i32 0, i32* %7
br label %162
129: ; preds = %120
%130 = load i8, i8* %23
%131 = zext i8 %130 to i32
switch i32 %131, label %137 [
i32 81, label %132
i32 82, label %133
i32 83, label %134
i32 84, label %135
i32 86, label %136
]
132: ; preds = %129
store i32 32, i32* %18
br label %137
133: ; preds = %129
store i32 64, i32* %18
br label %137
134: ; preds = %129
store i32 128, i32* %18
br label %137
135: ; preds = %129
store i32 256, i32* %18
br label %137
136: ; preds = %129
store i32 512, i32* %18
br label %137
137: ; preds = %129, %136, %135, %134, %133, %132
%138 = load i8, i8* %23
%139 = zext i8 %138 to i32
switch i32 %139, label %146 [
i32 97, label %140
i32 98, label %141
i32 99, label %142
i32 100, label %143
i32 101, label %144
i32 102, label %145
]
140: ; preds = %137
store i32 128, i32* %19
br label %146
141: ; preds = %137
store i32 256, i32* %19
br label %146
142: ; preds = %137
store i32 512, i32* %19
br label %146
143: ; preds = %137
store i32 1024, i32* %19
br label %146
144: ; preds = %137
store i32 2048, i32* %19
br label %146
145: ; preds = %137
store i32 4096, i32* %19
br label %146
146: ; preds = %137, %145, %144, %143, %142, %141, %140
%147 = load i8, i8* %23
%148 = zext i8 %147 to i32
switch i32 %148, label %159 [
i32 103, label %149
i32 104, label %150
i32 105, label %151
i32 108, label %152
i32 109, label %153
i32 110, label %154
i32 111, label %155
i32 112, label %156
i32 113, label %157
i32 114, label %158
]
149: ; preds = %146
store i32 1024, i32* %20
br label %159
150: ; preds = %146
store i32 2048, i32* %20
br label %159
151: ; preds = %146
store i32 4096, i32* %20
br label %159
152: ; preds = %146
store i32 8196, i32* %20
br label %159
153: ; preds = %146
store i32 16384, i32* %20
br label %159
154: ; preds = %146
store i32 32768, i32* %20
br label %159
155: ; preds = %146
store i32 65536, i32* %20
br label %159
156: ; preds = %146
store i32 131072, i32* %20
br label %159
157: ; preds = %146
store i32 262144, i32* %20
br label %159
158: ; preds = %146
store i32 40960, i32* %20
br label %159
159: ; preds = %146, %158, %157, %156, %155, %154, %153, %152, %151, %150, %149
br label %160
160: ; preds = %159, %78, %74
br label %38
161: ; preds = %53, %48
store i32 0, i32* %7
br label %162
162: ; preds = %161, %123, %36
%163 = load i32, i32* %7
ret i32 %163
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@POLLPRI = global i32 0
@write_flash.segmentresult = internal unnamed_addr global i32 1
@errno = global i64 0
@EINTR = global i64 0
@POLLHUP = global i32 0
@str = [20 x i8] c"Programming failed!\00"
@str.2 = [30 x i8] c"writing flash... please wait!\00"
define i32 @write_flash(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca [1 x %struct.pollfd]
%8 = alloca i8
%9 = tail call i32 @time(i32* null)
%10 = bitcast [1 x %struct.pollfd]* %7 to i8*
%11 = icmp eq i32 %0, 0
br i1 %11, label %95, label %12
12: ; preds = %6
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0, i32 0
store i32 %0, i32* %14
%15 = load i32, i32* @POLLIN
%16 = load i32, i32* @POLLPRI
%17 = or i32 %16, %15
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0, i32 1
store i32 %17, i32* %18
%19 = call i32 @time(i32* null)
%20 = sub nsw i32 %19, %9
%21 = icmp slt i32 %20, %5
br i1 %21, label %22, label %95
22: ; preds = %12
%23 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0, i32 2
br label %24
24: ; preds = %22, %85
%25 = phi i8 [ 0, %22 ], [ %89, %85 ]
%26 = phi i32 [ 0, %22 ], [ %88, %85 ]
%27 = phi i32 [ 0, %22 ], [ %87, %85 ]
%28 = phi i32 [ 0, %22 ], [ %86, %85 ]
store i8 0, i8* %8
%29 = load i32, i32* @write_flash.segmentresult
%30 = icmp eq i32 %29, 0
br i1 %30, label %84, label %31
31: ; preds = %24
%32 = call i32 @poll(%struct.pollfd* %13, i32 1, i32 1000)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = call i32 @sleep(i32 60)
br label %40
40: ; preds = %38, %34, %31
%41 = load i32, i32* %23
%42 = load i32, i32* @POLLHUP
%43 = and i32 %42, %41
%44 = icmp eq i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %40
store i32 -1, i32* %14
br label %85
46: ; preds = %40
%47 = call i64 @read(i32 %0, i8* %8, i32 1)
%48 = icmp ne i64 %47, 0
%49 = load i8, i8* %8
%50 = icmp ne i8 %49, 0
%51 = select i1 %48, i1 %50, i1 false
br i1 %51, label %52, label %85
52: ; preds = %46
switch i8 %49, label %68 [
i8 85, label %53
i8 62, label %55
i8 33, label %58
i8 64, label %61
i8 37, label %65
]
53: ; preds = %52
%54 = call i32 @write(i32 %0, i8* %8, i32 1)
br label %68
55: ; preds = %52
store i8 60, i8* %8
%56 = call i32 @write(i32 %0, i8* %8, i32 1)
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.2, i64 0, i64 0))
br label %68
58: ; preds = %52
%59 = call i32 @write_segment(i32 %0, i8* %1, i32 %27, i32 %3, i32 %26)
store i32 %59, i32* @write_flash.segmentresult
%60 = add i32 %27, 1
br label %68
61: ; preds = %52
%62 = add nsw i32 %28, 1
%63 = add i32 %27, -1
%64 = call i32 @write_segment(i32 %0, i8* %1, i32 %63, i32 %3, i32 %26)
store i32 %64, i32* @write_flash.segmentresult
br label %68
65: ; preds = %52
store i8 0, i8* %8
%66 = call i32 @write(i32 %0, i8* %8, i32 1)
store i8 -1, i8* %8
%67 = call i32 @write(i32 %0, i8* %8, i32 1)
br label %68
68: ; preds = %52, %65, %61, %58, %55, %53
%69 = phi i8 [ %49, %52 ], [ -1, %65 ], [ 64, %61 ], [ 33, %58 ], [ 60, %55 ], [ 85, %53 ]
%70 = phi i32 [ %28, %52 ], [ %28, %65 ], [ %62, %61 ], [ %28, %58 ], [ %28, %55 ], [ %28, %53 ]
%71 = phi i32 [ %27, %52 ], [ %27, %65 ], [ %27, %61 ], [ %60, %58 ], [ %27, %55 ], [ %27, %53 ]
%72 = phi i8 [ %25, %52 ], [ %25, %65 ], [ %25, %61 ], [ %25, %58 ], [ 1, %55 ], [ %25, %53 ]
%73 = icmp sgt i32 %70, 3
br i1 %73, label %74, label %78
74: ; preds = %68
%75 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
store i8 -1, i8* %8
%76 = call i32 @write(i32 %0, i8* %8, i32 1)
%77 = call i32 @write(i32 %0, i8* %8, i32 1)
br label %84
78: ; preds = %68
switch i8 %69, label %85 [
i8 81, label %79
i8 82, label %80
i8 83, label %81
i8 84, label %82
i8 86, label %83
]
79: ; preds = %78
br label %85
80: ; preds = %78
br label %85
81: ; preds = %78
br label %85
82: ; preds = %78
br label %85
83: ; preds = %78
br label %85
84: ; preds = %24, %74
br label %95
85: ; preds = %79, %80, %81, %82, %83, %78, %46, %45
%86 = phi i32 [ %28, %45 ], [ %28, %46 ], [ %70, %78 ], [ %70, %83 ], [ %70, %82 ], [ %70, %81 ], [ %70, %80 ], [ %70, %79 ]
%87 = phi i32 [ %27, %45 ], [ %27, %46 ], [ %71, %78 ], [ %71, %83 ], [ %71, %82 ], [ %71, %81 ], [ %71, %80 ], [ %71, %79 ]
%88 = phi i32 [ %26, %45 ], [ %26, %46 ], [ %26, %78 ], [ 512, %83 ], [ 256, %82 ], [ 128, %81 ], [ 64, %80 ], [ 32, %79 ]
%89 = phi i8 [ %25, %45 ], [ %25, %46 ], [ %72, %78 ], [ %72, %83 ], [ %72, %82 ], [ %72, %81 ], [ %72, %80 ], [ %72, %79 ]
%90 = call i32 @time(i32* null)
%91 = sub nsw i32 %90, %9
%92 = icmp slt i32 %91, %5
%93 = icmp ne i8 %89, 0
%94 = select i1 %92, i1 true, i1 %93
br i1 %94, label %24, label %95
95: ; preds = %85, %12, %84, %6
%96 = phi i32 [ -1, %6 ], [ 0, %84 ], [ 0, %12 ], [ 0, %85 ]
ret i32 %96
}