#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;
typedef int uint32_t;
struct stat {
  scalar_t__ st_rdev;
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ I2C_RETRIES;
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ I2C_TIMEOUT;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, int *, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int write(int, int *, int);
int main(int argc, char *argv[]) {
  uint32_t size = ~0L;
  uint32_t address = 0;
  if (argc != 3) {
    fprintf(stderr,
            "Usage: %s image.bin /dev/i2c-N\n   or: %s /dev/i2c-N image.bin\n",
            argv[0], argv[0]);
    exit(1);
  }
  struct stat stin;
  if (stat(argv[1], &stin) == -1) {
    perror("Failed to stat input");
    exit(1);
  }
  if (stin.st_rdev == 0) {
    if (stin.st_size >= 8 * 65536) {
      fprintf(stderr, "Input image exceeds maximum possible\n");
      exit(1);
    } else {
      size = stin.st_size;
    }
  }
  struct stat stout;
  if (stat(argv[2], &stout) == -1) {
    perror("Failed to stat output");
    exit(1);
  }
  if (stout.st_rdev == 0) {
    if (stout.st_size >= 8 * 65536) {
      fprintf(stderr, "Output image exceeds maximum possible\n");
      exit(1);
    } else if (size == (uint32_t)~0L) {
      size = stout.st_size;
    }
  }
  int fin = open(argv[1], O_RDWR);
  if (fin < 0) {
    perror("Failed to read input");
    exit(1);
  }
  int fout = open(argv[2], O_RDWR);
  if (fout < 0) {
    perror("Failed to access output");
    exit(1);
  }
  if (stin.st_rdev) {
    ioctl(fin, I2C_TIMEOUT, 1);
    ioctl(fin, I2C_RETRIES, 2);
  }
  if (stout.st_rdev) {
    ioctl(fout, I2C_TIMEOUT, 1);
    ioctl(fout, I2C_RETRIES, 2);
  }
  while (address < size) {
    uint8_t buf[2 + 128];
    buf[0] = (address & 0x0000ff00) >> 8;
    buf[1] = (address & 0x000000ff);
    if ((address & 0x0000ffff) == 0x000000) {
      uint8_t slave = address >> 16;
      slave &= 0x07;
      slave += 0x50;
      if (stin.st_rdev) {
        if (ioctl(fin, I2C_SLAVE, slave) == -1) {
          perror("Failed to set input slave address");
          exit(1);
        }
      }
      if (stout.st_rdev) {
        if (ioctl(fout, I2C_SLAVE, slave) == -1) {
          perror("Failed to set output slave address");
          exit(1);
        }
      }
    }
    if (stin.st_rdev) {
      write(fin, buf, 2);
    }
    int rdlen =
        read(fin, buf + 2, (size - address) >= 128 ? 128 : size - address);
    if (rdlen >= 0) {
      if (rdlen == 0) {
        break;
      }
      if (stout.st_rdev) {
        if (write(fout, buf, 2 + rdlen) < 2 + rdlen) {
          perror("Failed to write to i2c");
        }
      } else {
        if (write(fout, buf + 2, rdlen) < rdlen) {
          perror("Failed to write to file");
        }
      }
    } else {
      perror("Failed to read");
      exit(1);
    }
    address += rdlen;
    usleep(5 * 1000);
  }
  close(fin);
  close(fout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1200 <main+0x60>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1230 <main+0x90>
lea    0xe1e(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1274 <main+0xd4>
cmpl   $0x80000,-0x20(%rbp)
jl     1269 <main+0xc9>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1274 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    12a4 <main+0x104>
lea    0xde5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x38(%rbp)
jne    12f7 <main+0x157>
cmpl   $0x80000,-0x30(%rbp)
jl     12dd <main+0x13d>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    12ed <main+0x14d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    12f2 <main+0x152>
jmp    12f7 <main+0x157>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1330 <main+0x190>
lea    0xd96(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2ca1(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1369 <main+0x1c9>
lea    0xd72(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
je     13a0 <main+0x200>
mov    -0x3c(%rbp),%edi
mov    0x2c42(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
mov    -0x3c(%rbp),%edi
mov    0x2c5c(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edx
call   1040 <ioctl@plt>
cmpq   $0x0,-0x38(%rbp)
je     13d7 <main+0x237>
mov    -0x40(%rbp),%edi
mov    0x2c0b(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1040 <ioctl@plt>
mov    -0x40(%rbp),%edi
mov    0x2c25(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edx
call   1040 <ioctl@plt>
jmp    13dc <main+0x23c>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1628 <main+0x488>
mov    -0x18(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %eax,-0x250(%rbp)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x24c(%rbp)
mov    -0x18(%rbp),%eax
and    $0xffff,%eax
cmp    $0x0,%eax
jne    14d3 <main+0x333>
mov    -0x18(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x254(%rbp)
mov    -0x254(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x254(%rbp)
mov    -0x254(%rbp),%eax
add    $0x50,%eax
mov    %eax,-0x254(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1488 <main+0x2e8>
mov    -0x3c(%rbp),%edi
mov    0x2b81(%rip),%rax
mov    (%rax),%esi
mov    -0x254(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1483 <main+0x2e3>
lea    0xc70(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1488 <main+0x2e8>
cmpq   $0x0,-0x38(%rbp)
je     14ce <main+0x32e>
mov    -0x40(%rbp),%edi
mov    0x2b3b(%rip),%rax
mov    (%rax),%esi
mov    -0x254(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    14c9 <main+0x329>
lea    0xc4c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    14ce <main+0x32e>
jmp    14d3 <main+0x333>
cmpq   $0x0,-0x28(%rbp)
je     14f2 <main+0x352>
mov    -0x3c(%rbp),%edi
lea    -0x250(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x264(%rbp)
lea    -0x250(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x260(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
cmp    $0x80,%eax
jl     152e <main+0x38e>
mov    $0x80,%eax
mov    %eax,-0x268(%rbp)
jmp    153a <main+0x39a>
mov    -0x14(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x268(%rbp)
mov    -0x260(%rbp),%rsi
mov    -0x264(%rbp),%edi
mov    -0x268(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x258(%rbp)
cmpl   $0x0,-0x258(%rbp)
jl     15f7 <main+0x457>
cmpl   $0x0,-0x258(%rbp)
jne    1577 <main+0x3d7>
jmp    1628 <main+0x488>
cmpq   $0x0,-0x38(%rbp)
je     15bc <main+0x41c>
mov    -0x40(%rbp),%edi
lea    -0x250(%rbp),%rsi
mov    -0x258(%rbp),%edx
add    $0x2,%edx
call   1030 <write@plt>
mov    -0x258(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
jge    15b7 <main+0x417>
lea    0xb77(%rip),%rdi
call   10a0 <perror@plt>
jmp    15f2 <main+0x452>
mov    -0x40(%rbp),%edi
lea    -0x250(%rbp),%rsi
add    $0x8,%rsi
mov    -0x258(%rbp),%edx
call   1030 <write@plt>
cmp    -0x258(%rbp),%eax
jge    15ed <main+0x44d>
lea    0xb58(%rip),%rdi
call   10a0 <perror@plt>
jmp    15f2 <main+0x452>
jmp    160d <main+0x46d>
lea    0xb5a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x258(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1388,%edi
call   10c0 <usleep@plt>
jmp    13dc <main+0x23c>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    1450 <main+0x2b0>
mov    0x8(%rbp),%rdi
lea    0x20(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     1478 <main+0x2d8>
mov    0x20(%rsp),%r15
mov    $0xffffffff,%r14d
test   %r15,%r15
jne    11f6 <main+0x56>
mov    0x28(%rsp),%r14d
cmp    $0x80000,%r14d
jge    149c <main+0x2fc>
mov    0x10(%rbp),%rdi
lea    0x10(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     1481 <main+0x2e1>
mov    0x10(%rsp),%r12
test   %r12,%r12
jne    122e <main+0x8e>
mov    0x18(%rsp),%eax
cmp    $0x80000,%eax
jge    14ae <main+0x30e>
cmp    $0xffffffff,%r14d
cmove  %eax,%r14d
mov    0x8(%rbp),%rdi
mov    0x2da7(%rip),%rbx
mov    (%rbx),%esi
call   1090 <open@plt>
test   %eax,%eax
js     148a <main+0x2ea>
mov    %eax,%r13d
mov    0x10(%rbp),%rdi
mov    (%rbx),%esi
call   1090 <open@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
js     1493 <main+0x2f3>
test   %r15,%r15
je     1293 <main+0xf3>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    $0x1,%edx
call   1040 <ioctl@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    $0x2,%edx
call   1040 <ioctl@plt>
test   %r12,%r12
je     12c6 <main+0x126>
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <ioctl@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <ioctl@plt>
test   %r14d,%r14d
jle    1403 <main+0x263>
xor    %ebx,%ebx
mov    %r13d,0xc(%rsp)
jmp    1314 <main+0x174>
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
lea    0x38(%rsp),%rsi
mov    %ebp,%edx
call   1030 <write@plt>
lea    0xe49(%rip),%rdi
cmp    %ebp,%eax
jl     13f9 <main+0x259>
add    %ebp,%ebx
mov    $0x1388,%edi
call   10c0 <usleep@plt>
cmp    %ebx,%r14d
jle    1403 <main+0x263>
movzbl %bh,%eax
mov    %eax,0x30(%rsp)
movzbl %bl,%eax
mov    %eax,0x34(%rsp)
test   %bx,%bx
jne    1375 <main+0x1d5>
mov    %ebx,%ebp
shr    $0x10,%ebp
and    $0x7,%ebp
or     $0x50,%ebp
test   %r15,%r15
je     1353 <main+0x1b3>
mov    0x2c9a(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
mov    %ebp,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     143e <main+0x29e>
test   %r12,%r12
je     1375 <main+0x1d5>
mov    0x2c79(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%edi
mov    %ebp,%edx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1447 <main+0x2a7>
test   %r15,%r15
je     138c <main+0x1ec>
mov    %r13d,%edi
lea    0x30(%rsp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %r14d,%edx
sub    %ebx,%edx
cmp    $0x80,%edx
mov    $0x80,%eax
cmovge %eax,%edx
mov    %r13d,%edi
lea    0x38(%rsp),%rsi
call   1060 <read@plt>
test   %eax,%eax
js     1428 <main+0x288>
je     1403 <main+0x263>
mov    %eax,%ebp
test   %r12,%r12
je     12e0 <main+0x140>
mov    %r14d,%r13d
mov    %r12,%r14
mov    %r15,%r12
lea    0x2(%rbp),%r15d
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%rsi
mov    %r15d,%edx
call   1030 <write@plt>
lea    0xd47(%rip),%rdi
cmp    %r15d,%eax
mov    %r12,%r15
mov    %r14,%r12
mov    %r13d,%r14d
mov    0xc(%rsp),%r13d
jge    12ff <main+0x15f>
call   10a0 <perror@plt>
jmp    12ff <main+0x15f>
mov    %r13d,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd29(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xc9f(%rip),%rdi
jmp    142f <main+0x28f>
lea    0xcb8(%rip),%rdi
jmp    142f <main+0x28f>
mov    0x2b71(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rcx
lea    0xb9c(%rip),%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xbc0(%rip),%rdi
jmp    142f <main+0x28f>
lea    0xbf2(%rip),%rdi
jmp    142f <main+0x28f>
lea    0xc26(%rip),%rdi
jmp    142f <main+0x28f>
lea    0xc32(%rip),%rdi
jmp    142f <main+0x28f>
mov    0x2b25(%rip),%rax
mov    (%rax),%edi
lea    0xba8(%rip),%rsi
jmp    14be <main+0x31e>
mov    0x2b13(%rip),%rax
mov    (%rax),%edi
lea    0xbd2(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@stderr = global i32 0
@.str = [63 x i8] c"Usage: %s image.bin /dev/i2c-N\0A or: %s /dev/i2c-N image.bin\0A\00"
@.str.1 = [21 x i8] c"Failed to stat input\00"
@.str.2 = [38 x i8] c"Input image exceeds maximum possible\0A\00"
@.str.3 = [22 x i8] c"Failed to stat output\00"
@.str.4 = [39 x i8] c"Output image exceeds maximum possible\0A\00"
@O_RDWR = global i32 0
@.str.5 = [21 x i8] c"Failed to read input\00"
@.str.6 = [24 x i8] c"Failed to access output\00"
@I2C_TIMEOUT = global i32 0
@I2C_RETRIES = global i32 0
@I2C_SLAVE = global i32 0
@.str.7 = [34 x i8] c"Failed to set input slave address\00"
@.str.8 = [35 x i8] c"Failed to set output slave address\00"
@.str.9 = [23 x i8] c"Failed to write to i2c\00"
@.str.10 = [24 x i8] c"Failed to write to file\00"
@.str.11 = [15 x i8] c"Failed to read\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 %struct.stat
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
%12 = alloca [130 x i32]
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %7
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %27
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @stat(i8* %30, %struct.stat* %8)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %52
40: ; preds = %36
%41 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%42 = load i32, i32* %41
%43 = icmp sge i32 %42, 524288
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
%49 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%50 = load i32, i32* %49
store i32 %50, i32* %6
br label %51
51: ; preds = %48
br label %52
52: ; preds = %51, %36
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = call i32 @stat(i8* %55, %struct.stat* %9)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %52
%59 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %52
%62 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%63 = load i64, i64* %62
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %81
65: ; preds = %61
%66 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%67 = load i32, i32* %66
%68 = icmp sge i32 %67, 524288
br i1 %68, label %69, label %73
69: ; preds = %65
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %65
%74 = load i32, i32* %6
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%78 = load i32, i32* %77
store i32 %78, i32* %6
br label %79
79: ; preds = %76, %73
br label %80
80: ; preds = %79
br label %81
81: ; preds = %80, %61
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 1
%84 = load i8*, i8** %83
%85 = load i32, i32* @O_RDWR
%86 = call i32 @open(i8* %84, i32 %85)
store i32 %86, i32* %10
%87 = load i32, i32* %10
%88 = icmp slt i32 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %81
%90 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%91 = call i32 @exit(i32 1)
unreachable
92: ; preds = %81
%93 = load i8**, i8*** %5
%94 = getelementptr inbounds i8*, i8** %93, i64 2
%95 = load i8*, i8** %94
%96 = load i32, i32* @O_RDWR
%97 = call i32 @open(i8* %95, i32 %96)
store i32 %97, i32* %11
%98 = load i32, i32* %11
%99 = icmp slt i32 %98, 0
br i1 %99, label %100, label %103
100: ; preds = %92
%101 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%102 = call i32 @exit(i32 1)
unreachable
103: ; preds = %92
%104 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%105 = load i64, i64* %104
%106 = icmp ne i64 %105, 0
br i1 %106, label %107, label %114
107: ; preds = %103
%108 = load i32, i32* %10
%109 = load i32, i32* @I2C_TIMEOUT
%110 = call i32 @ioctl(i32 %108, i32 %109, i32 1)
%111 = load i32, i32* %10
%112 = load i32, i32* @I2C_RETRIES
%113 = call i32 @ioctl(i32 %111, i32 %112, i32 2)
br label %114
114: ; preds = %107, %103
%115 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%116 = load i64, i64* %115
%117 = icmp ne i64 %116, 0
br i1 %117, label %118, label %125
118: ; preds = %114
%119 = load i32, i32* %11
%120 = load i32, i32* @I2C_TIMEOUT
%121 = call i32 @ioctl(i32 %119, i32 %120, i32 1)
%122 = load i32, i32* %11
%123 = load i32, i32* @I2C_RETRIES
%124 = call i32 @ioctl(i32 %122, i32 %123, i32 2)
br label %125
125: ; preds = %118, %114
br label %126
126: ; preds = %237, %125
%127 = load i32, i32* %7
%128 = load i32, i32* %6
%129 = icmp slt i32 %127, %128
br i1 %129, label %130, label %242
130: ; preds = %126
%131 = load i32, i32* %7
%132 = and i32 %131, 65280
%133 = ashr i32 %132, 8
%134 = getelementptr inbounds [130 x i32], [130 x i32]* %12, i64 0, i64 0
store i32 %133, i32* %134
%135 = load i32, i32* %7
%136 = and i32 %135, 255
%137 = getelementptr inbounds [130 x i32], [130 x i32]* %12, i64 0, i64 1
store i32 %136, i32* %137
%138 = load i32, i32* %7
%139 = and i32 %138, 65535
%140 = icmp eq i32 %139, 0
br i1 %140, label %141, label %176
141: ; preds = %130
%142 = load i32, i32* %7
%143 = ashr i32 %142, 16
store i32 %143, i32* %13
%144 = load i32, i32* %13
%145 = and i32 %144, 7
store i32 %145, i32* %13
%146 = load i32, i32* %13
%147 = add nsw i32 %146, 80
store i32 %147, i32* %13
%148 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%149 = load i64, i64* %148
%150 = icmp ne i64 %149, 0
br i1 %150, label %151, label %161
151: ; preds = %141
%152 = load i32, i32* %10
%153 = load i32, i32* @I2C_SLAVE
%154 = load i32, i32* %13
%155 = call i32 @ioctl(i32 %152, i32 %153, i32 %154)
%156 = icmp eq i32 %155, -1
br i1 %156, label %157, label %160
157: ; preds = %151
%158 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%159 = call i32 @exit(i32 1)
unreachable
160: ; preds = %151
br label %161
161: ; preds = %160, %141
%162 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%163 = load i64, i64* %162
%164 = icmp ne i64 %163, 0
br i1 %164, label %165, label %175
165: ; preds = %161
%166 = load i32, i32* %11
%167 = load i32, i32* @I2C_SLAVE
%168 = load i32, i32* %13
%169 = call i32 @ioctl(i32 %166, i32 %167, i32 %168)
%170 = icmp eq i32 %169, -1
br i1 %170, label %171, label %174
171: ; preds = %165
%172 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
%173 = call i32 @exit(i32 1)
unreachable
174: ; preds = %165
br label %175
175: ; preds = %174, %161
br label %176
176: ; preds = %175, %130
%177 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%178 = load i64, i64* %177
%179 = icmp ne i64 %178, 0
br i1 %179, label %180, label %184
180: ; preds = %176
%181 = load i32, i32* %10
%182 = getelementptr inbounds [130 x i32], [130 x i32]* %12, i64 0, i64 0
%183 = call i32 @write(i32 %181, i32* %182, i32 2)
br label %184
184: ; preds = %180, %176
%185 = load i32, i32* %10
%186 = getelementptr inbounds [130 x i32], [130 x i32]* %12, i64 0, i64 0
%187 = getelementptr inbounds i32, i32* %186, i64 2
%188 = load i32, i32* %6
%189 = load i32, i32* %7
%190 = sub nsw i32 %188, %189
%191 = icmp sge i32 %190, 128
br i1 %191, label %192, label %193
192: ; preds = %184
br label %197
193: ; preds = %184
%194 = load i32, i32* %6
%195 = load i32, i32* %7
%196 = sub nsw i32 %194, %195
br label %197
197: ; preds = %193, %192
%198 = phi i32 [ 128, %192 ], [ %196, %193 ]
%199 = call i32 @read(i32 %185, i32* %187, i32 %198)
store i32 %199, i32* %14
%200 = load i32, i32* %14
%201 = icmp sge i32 %200, 0
br i1 %201, label %202, label %234
202: ; preds = %197
%203 = load i32, i32* %14
%204 = icmp eq i32 %203, 0
br i1 %204, label %205, label %206
205: ; preds = %202
br label %242
206: ; preds = %202
%207 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%208 = load i64, i64* %207
%209 = icmp ne i64 %208, 0
br i1 %209, label %210, label %222
210: ; preds = %206
%211 = load i32, i32* %11
%212 = getelementptr inbounds [130 x i32], [130 x i32]* %12, i64 0, i64 0
%213 = load i32, i32* %14
%214 = add nsw i32 2, %213
%215 = call i32 @write(i32 %211, i32* %212, i32 %214)
%216 = load i32, i32* %14
%217 = add nsw i32 2, %216
%218 = icmp slt i32 %215, %217
br i1 %218, label %219, label %221
219: ; preds = %210
%220 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
br label %221
221: ; preds = %219, %210
br label %233
222: ; preds = %206
%223 = load i32, i32* %11
%224 = getelementptr inbounds [130 x i32], [130 x i32]* %12, i64 0, i64 0
%225 = getelementptr inbounds i32, i32* %224, i64 2
%226 = load i32, i32* %14
%227 = call i32 @write(i32 %223, i32* %225, i32 %226)
%228 = load i32, i32* %14
%229 = icmp slt i32 %227, %228
br i1 %229, label %230, label %232
230: ; preds = %222
%231 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0))
br label %232
232: ; preds = %230, %222
br label %233
233: ; preds = %232, %221
br label %237
234: ; preds = %197
%235 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
%236 = call i32 @exit(i32 1)
unreachable
237: ; preds = %233
%238 = load i32, i32* %14
%239 = load i32, i32* %7
%240 = add nsw i32 %239, %238
store i32 %240, i32* %7
%241 = call i32 @usleep(i32 5000)
br label %126
242: ; preds = %205, %126
%243 = load i32, i32* %10
%244 = call i32 @close(i32 %243)
%245 = load i32, i32* %11
%246 = call i32 @close(i32 %245)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@stderr = global i32 0
@.str = [63 x i8] c"Usage: %s image.bin /dev/i2c-N\0A or: %s /dev/i2c-N image.bin\0A\00"
@.str.1 = [21 x i8] c"Failed to stat input\00"
@.str.2 = [38 x i8] c"Input image exceeds maximum possible\0A\00"
@.str.3 = [22 x i8] c"Failed to stat output\00"
@.str.4 = [39 x i8] c"Output image exceeds maximum possible\0A\00"
@O_RDWR = global i32 0
@.str.5 = [21 x i8] c"Failed to read input\00"
@.str.6 = [24 x i8] c"Failed to access output\00"
@I2C_TIMEOUT = global i32 0
@I2C_RETRIES = global i32 0
@I2C_SLAVE = global i32 0
@.str.7 = [34 x i8] c"Failed to set input slave address\00"
@.str.8 = [35 x i8] c"Failed to set output slave address\00"
@.str.9 = [23 x i8] c"Failed to write to i2c\00"
@.str.10 = [24 x i8] c"Failed to write to file\00"
@.str.11 = [15 x i8] c"Failed to read\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca [130 x i32]
%6 = icmp eq i32 %0, 3
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = bitcast %struct.stat* %3 to i8*
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @stat(i8* %15, %struct.stat* %3)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%27 = load i32, i32* %26
%28 = icmp sgt i32 %27, 524287
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %25, %21
%34 = phi i32 [ -1, %21 ], [ %27, %25 ]
%35 = bitcast %struct.stat* %4 to i8*
%36 = getelementptr inbounds i8*, i8** %1, i64 2
%37 = load i8*, i8** %36
%38 = call i32 @stat(i8* %37, %struct.stat* %4)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %33
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%45 = load i64, i64* %44
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %43
%48 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%49 = load i32, i32* %48
%50 = icmp sgt i32 %49, 524287
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = tail call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%54 = tail call i32 @exit(i32 1)
unreachable
55: ; preds = %47
%56 = icmp eq i32 %34, -1
%57 = select i1 %56, i32 %49, i32 %34
br label %58
58: ; preds = %55, %43
%59 = phi i32 [ %34, %43 ], [ %57, %55 ]
%60 = load i8*, i8** %14
%61 = load i32, i32* @O_RDWR
%62 = tail call i32 @open(i8* %60, i32 %61)
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %58
%65 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%66 = tail call i32 @exit(i32 1)
unreachable
67: ; preds = %58
%68 = load i8*, i8** %36
%69 = load i32, i32* @O_RDWR
%70 = tail call i32 @open(i8* %68, i32 %69)
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%74 = tail call i32 @exit(i32 1)
unreachable
75: ; preds = %67
br i1 %24, label %81, label %76
76: ; preds = %75
%77 = load i32, i32* @I2C_TIMEOUT
%78 = tail call i32 @ioctl(i32 %62, i32 %77, i32 1)
%79 = load i32, i32* @I2C_RETRIES
%80 = tail call i32 @ioctl(i32 %62, i32 %79, i32 2)
br label %81
81: ; preds = %76, %75
br i1 %46, label %87, label %82
82: ; preds = %81
%83 = load i32, i32* @I2C_TIMEOUT
%84 = tail call i32 @ioctl(i32 %70, i32 %83, i32 1)
%85 = load i32, i32* @I2C_RETRIES
%86 = tail call i32 @ioctl(i32 %70, i32 %85, i32 2)
br label %87
87: ; preds = %82, %81
%88 = icmp sgt i32 %59, 0
br i1 %88, label %89, label %150
89: ; preds = %87
%90 = bitcast [130 x i32]* %5 to i8*
%91 = getelementptr inbounds [130 x i32], [130 x i32]* %5, i64 0, i64 0
%92 = getelementptr inbounds [130 x i32], [130 x i32]* %5, i64 0, i64 1
%93 = getelementptr inbounds [130 x i32], [130 x i32]* %5, i64 0, i64 2
br label %94
94: ; preds = %89, %146
%95 = phi i32 [ 0, %89 ], [ %147, %146 ]
%96 = lshr i32 %95, 8
%97 = and i32 %96, 255
store i32 %97, i32* %91
%98 = and i32 %95, 255
store i32 %98, i32* %92
%99 = and i32 %95, 65535
%100 = icmp eq i32 %99, 0
br i1 %100, label %101, label %120
101: ; preds = %94
%102 = lshr i32 %95, 16
%103 = and i32 %102, 7
%104 = or i32 %103, 80
br i1 %24, label %112, label %105
105: ; preds = %101
%106 = load i32, i32* @I2C_SLAVE
%107 = tail call i32 @ioctl(i32 %62, i32 %106, i32 %104)
%108 = icmp eq i32 %107, -1
br i1 %108, label %109, label %112
109: ; preds = %105
%110 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%111 = tail call i32 @exit(i32 1)
unreachable
112: ; preds = %105, %101
br i1 %46, label %120, label %113
113: ; preds = %112
%114 = load i32, i32* @I2C_SLAVE
%115 = tail call i32 @ioctl(i32 %70, i32 %114, i32 %104)
%116 = icmp eq i32 %115, -1
br i1 %116, label %117, label %120
117: ; preds = %113
%118 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
%119 = tail call i32 @exit(i32 1)
unreachable
120: ; preds = %112, %113, %94
br i1 %24, label %123, label %121
121: ; preds = %120
%122 = call i32 @write(i32 %62, i32* %91, i32 2)
br label %123
123: ; preds = %121, %120
%124 = sub nsw i32 %59, %95
%125 = icmp slt i32 %124, 128
%126 = select i1 %125, i32 %124, i32 128
%127 = call i32 @read(i32 %62, i32* %93, i32 %126)
%128 = icmp sgt i32 %127, -1
br i1 %128, label %129, label %140
129: ; preds = %123
%130 = icmp eq i32 %127, 0
br i1 %130, label %131, label %132
131: ; preds = %129
br label %150
132: ; preds = %129
br i1 %46, label %137, label %133
133: ; preds = %132
%134 = add nuw nsw i32 %127, 2
%135 = call i32 @write(i32 %70, i32* %91, i32 %134)
%136 = icmp slt i32 %135, %134
br i1 %136, label %143, label %146
137: ; preds = %132
%138 = call i32 @write(i32 %70, i32* %93, i32 %127)
%139 = icmp slt i32 %138, %127
br i1 %139, label %143, label %146
140: ; preds = %123
%141 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
%142 = tail call i32 @exit(i32 1)
unreachable
143: ; preds = %137, %133
%144 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0), %133 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.10, i64 0, i64 0), %137 ]
%145 = tail call i32 @perror(i8* %144)
br label %146
146: ; preds = %143, %137, %133
%147 = add nsw i32 %127, %95
%148 = tail call i32 @usleep(i32 5000)
%149 = icmp sgt i32 %59, %147
br i1 %149, label %94, label %150
150: ; preds = %146, %87, %131
%151 = tail call i32 @close(i32 %62)
%152 = tail call i32 @close(i32 %70)
ret i32 0
}