#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t full_read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, size_t,
                          int /*<<< orphan*/ *);
int init();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ system(char *);
int main() {
  system("killall -9 program.elf.respawner.sh; killall -9 program.elf");
  uint8_t buf[256];
  int fd = init();
  if (fd == 0) {
    fprintf(stderr, "ERROR opening device\n");
    return -1;
  }
  int i;
  FILE *out = fopen("navdata.bin", "wb");
  for (i = 0; i < 100; i++) {
    size_t num = full_read(fd, buf, 256);
    fwrite(buf, sizeof(uint8_t), num, out);
  }
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
call   1050 <system@plt>
mov    $0x0,%al
call   1070 <init@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jne    11c9 <main+0x59>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <main+0xee>
lea    0xe82(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x420(%rbp)
movl   $0x0,-0x418(%rbp)
cmpl   $0x64,-0x418(%rbp)
jge    124b <main+0xdb>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x100,%edx
call   1030 <full_read@plt>
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    -0x420(%rbp),%rcx
mov    $0x4,%esi
call   1090 <fwrite@plt>
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
jmp    11ed <main+0x7d>
mov    -0x420(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
lea    0xe7c(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
call   1070 <init@plt>
test   %eax,%eax
je     11f4 <main+0x84>
mov    %eax,%r14d
lea    0xeb4(%rip),%rdi
lea    0xeb9(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    $0x64,%ebx
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1030 <full_read@plt>
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   1090 <fwrite@plt>
add    $0xffffffff,%ebx
jne    11c0 <main+0x50>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    120e <main+0x9e>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [60 x i8] c"killall -9 program.elf.respawner.sh; killall -9 program.elf\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"ERROR opening device\0A\00"
@.str.2 = [12 x i8] c"navdata.bin\00"
@.str.3 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
store i32 0, i32* %1
%7 = call i32 @system(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0))
%8 = call i32 (...) @init()
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
14: ; preds = %0
%15 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %15, i32** %5
store i32 0, i32* %4
br label %16
16: ; preds = %27, %14
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 100
br i1 %18, label %19, label %30
19: ; preds = %16
%20 = load i32, i32* %3
%21 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 0
%22 = call i64 @full_read(i32 %20, i32* %21, i32 256)
store i64 %22, i64* %6
%23 = getelementptr inbounds [256 x i32], [256 x i32]* %2, i64 0, i64 0
%24 = load i64, i64* %6
%25 = load i32*, i32** %5
%26 = call i32 @fwrite(i32* %23, i32 4, i64 %24, i32* %25)
br label %27
27: ; preds = %19
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %16
30: ; preds = %16
%31 = load i32*, i32** %5
%32 = call i32 @fclose(i32* %31)
store i32 0, i32* %1
br label %33
33: ; preds = %30, %11
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [60 x i8] c"killall -9 program.elf.respawner.sh; killall -9 program.elf\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"ERROR opening device\0A\00"
@.str.2 = [12 x i8] c"navdata.bin\00"
@.str.3 = [3 x i8] c"wb\00"
define i32 @main() {
%1 = alloca [256 x i32]
%2 = tail call i32 @system(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str, i64 0, i64 0))
%3 = bitcast [256 x i32]* %1 to i8*
%4 = tail call i32 (...) @init()
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %20
9: ; preds = %0
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%11 = getelementptr inbounds [256 x i32], [256 x i32]* %1, i64 0, i64 0
br label %12
12: ; preds = %9, %12
%13 = phi i32 [ 0, %9 ], [ %16, %12 ]
%14 = call i64 @full_read(i32 %4, i32* %11, i32 256)
%15 = call i32 @fwrite(i32* %11, i32 4, i64 %14, i32* %10)
%16 = add nuw nsw i32 %13, 1
%17 = icmp eq i32 %16, 100
br i1 %17, label %18, label %12
18: ; preds = %12
%19 = call i32 @fclose(i32* %10)
br label %20
20: ; preds = %18, %6
%21 = phi i32 [ -1, %6 ], [ 0, %18 ]
ret i32 %21
}