#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*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fread(char *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(unsigned int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, unsigned int);
int main(int argc, char *argv[]) {
  FILE *file;
  int deviceFileDescriptor;
  char *buffer;
  unsigned int fileLen;
  char fileSizeBuff[4];
  printf("%d\n", sizeof(unsigned int));
  deviceFileDescriptor = open((char *)"/dev/andromon0", O_RDWR);
  if (deviceFileDescriptor == -1) {
    perror("open");
    exit(1);
  }
  file = fopen((char *)"test.png", "rb");
  if (!file) {
    fprintf(stderr, "Unable to open file %s\n", (char *)"test.png");
    close(deviceFileDescriptor);
    exit(1);
  }
  fseek(file, 0, SEEK_END);
  fileLen = ftell(file);
  fseek(file, 0, SEEK_SET);
  printf("%d bytes\n", fileLen);
  buffer = (char *)malloc(fileLen + 1);
  if (!buffer) {
    fprintf(stderr, "Memory error!");
    fclose(file);
    close(deviceFileDescriptor);
  }
  fileSizeBuff[0] = 0xFF & fileLen;
  fileSizeBuff[1] = 0xFF & (fileLen >> 8);
  fileSizeBuff[2] = 0xFF & (fileLen >> 16);
  fileSizeBuff[3] = 0xFF & (fileLen >> 24);
  printf("%d\n", fileSizeBuff[0]);
  printf("%d\n", fileSizeBuff[1]);
  printf("%d\n", fileSizeBuff[2]);
  printf("%d\n", fileSizeBuff[3]);
  write(deviceFileDescriptor, fileSizeBuff, 4);
  fread(buffer, fileLen, 1, file);
  fclose(file);
  write(deviceFileDescriptor, buffer, fileLen + 1);
  close(deviceFileDescriptor);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe03(%rip),%rdi
mov    $0x4,%esi
call   1070 <printf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xded(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    123f <main+0x5f>
lea    0xde3(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
lea    0xdd2(%rip),%rdi
lea    0xdd4(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1291 <main+0xb1>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
lea    0xda0(%rip),%rdx
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x2c(%rbp),%eax
lea    0xd6d(%rip),%rdi
movsbl %al,%esi
call   1070 <printf@plt>
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1319 <main+0x139>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd45(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x2f(%rbp)
mov    -0x2c(%rbp),%eax
shr    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x2e(%rbp)
mov    -0x2c(%rbp),%eax
shr    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x2d(%rbp)
lea    0xcab(%rip),%rdi
movsbl -0x30(%rbp),%esi
call   1070 <printf@plt>
lea    0xc9b(%rip),%rdi
movsbl -0x2f(%rbp),%esi
call   1070 <printf@plt>
lea    0xc8b(%rip),%rdi
movsbl -0x2e(%rbp),%esi
call   1070 <printf@plt>
lea    0xc7b(%rip),%rdi
movsbl -0x2d(%rbp),%esi
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1050 <write@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
add    $0x1,%edx
call   1050 <write@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe0b(%rip),%rdi
mov    $0x4,%esi
call   1070 <printf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xdf5(%rip),%rdi
call   10d0 <open@plt>
cmp    $0xffffffff,%eax
je     138f <main+0x1af>
mov    %eax,%r14d
lea    0xdf1(%rip),%rdi
lea    0xdf3(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     13a5 <main+0x1c5>
mov    %rax,%rbp
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %rbp,%rdi
call   10a0 <ftell@plt>
mov    %eax,%r13d
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
movsbl %r13b,%ebx
lea    0xdc1(%rip),%rdi
mov    %ebx,%esi
call   1070 <printf@plt>
lea    0x1(%r13),%edi
mov    %edi,0x14(%rsp)
call   10b0 <malloc@plt>
mov    %rax,0x20(%rsp)
test   %rax,%rax
jne    12c0 <main+0xe0>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %bl,0xc(%rsp)
mov    %r14d,0x10(%rsp)
mov    %r13d,%r14d
shr    $0x8,%r14d
mov    %r14b,0xd(%rsp)
mov    %rbp,0x18(%rsp)
mov    %r13d,%r15d
shr    $0x10,%r15d
mov    %r15b,0xe(%rsp)
mov    %r13d,%eax
shr    $0x18,%eax
mov    %al,0xf(%rsp)
lea    0xd09(%rip),%r12
mov    %r12,%rdi
mov    %ebx,%esi
call   1070 <printf@plt>
movsbl %r14b,%esi
mov    %r12,%rdi
call   1070 <printf@plt>
movsbl %r15b,%esi
mov    %r12,%rdi
call   1070 <printf@plt>
mov    %r13d,%esi
sar    $0x18,%esi
mov    %r12,%rdi
call   1070 <printf@plt>
lea    0xc(%rsp),%rsi
mov    0x10(%rsp),%ebp
mov    %ebp,%edi
mov    $0x4,%edx
call   1050 <write@plt>
mov    0x20(%rsp),%r14
mov    %r14,%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    0x18(%rsp),%rbx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    0x14(%rsp),%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc7d(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2c24(%rip),%rax
mov    (%rax),%edi
lea    0xc6f(%rip),%rsi
lea    0xc5c(%rip),%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [15 x i8] c"/dev/andromon0\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [9 x i8] c"test.png\00"
@.str.4 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.5 = [24 x i8] c"Unable to open file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.6 = [10 x i8] c"%d bytes\0A\00"
@.str.7 = [14 x i8] c"Memory error!\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 i8*
%9 = alloca i32
%10 = alloca [4 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext 4)
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %7
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = load i32*, i32** %6
%31 = load i32, i32* @SEEK_END
%32 = call i32 @fseek(i32* %30, i32 0, i32 %31)
%33 = load i32*, i32** %6
%34 = call i32 @ftell(i32* %33)
store i32 %34, i32* %9
%35 = load i32*, i32** %6
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %35, i32 0, i32 %36)
%38 = load i32, i32* %9
%39 = trunc i32 %38 to i8
%40 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8 signext %39)
%41 = load i32, i32* %9
%42 = add i32 %41, 1
%43 = call i64 @malloc(i32 %42)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = icmp ne i8* %45, null
br i1 %46, label %54, label %47
47: ; preds = %29
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* %7
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %47, %29
%55 = load i32, i32* %9
%56 = and i32 255, %55
%57 = trunc i32 %56 to i8
%58 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
store i8 %57, i8* %58
%59 = load i32, i32* %9
%60 = lshr i32 %59, 8
%61 = and i32 255, %60
%62 = trunc i32 %61 to i8
%63 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
store i8 %62, i8* %63
%64 = load i32, i32* %9
%65 = lshr i32 %64, 16
%66 = and i32 255, %65
%67 = trunc i32 %66 to i8
%68 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
store i8 %67, i8* %68
%69 = load i32, i32* %9
%70 = lshr i32 %69, 24
%71 = and i32 255, %70
%72 = trunc i32 %71 to i8
%73 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 3
store i8 %72, i8* %73
%74 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%75 = load i8, i8* %74
%76 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %75)
%77 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%78 = load i8, i8* %77
%79 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %78)
%80 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
%81 = load i8, i8* %80
%82 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %81)
%83 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 3
%84 = load i8, i8* %83
%85 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %84)
%86 = load i32, i32* %7
%87 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%88 = call i32 @write(i32 %86, i8* %87, i32 4)
%89 = load i8*, i8** %8
%90 = load i32, i32* %9
%91 = load i32*, i32** %6
%92 = call i32 @fread(i8* %89, i32 %90, i32 1, i32* %91)
%93 = load i32*, i32** %6
%94 = call i32 @fclose(i32* %93)
%95 = load i32, i32* %7
%96 = load i8*, i8** %8
%97 = load i32, i32* %9
%98 = add i32 %97, 1
%99 = call i32 @write(i32 %95, i8* %96, i32 %98)
%100 = load i32, i32* %7
%101 = call i32 @close(i32 %100)
%102 = load i8*, i8** %8
%103 = call i32 @free(i8* %102)
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [15 x i8] c"/dev/andromon0\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [9 x i8] c"test.png\00"
@.str.4 = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.5 = [24 x i8] c"Unable to open file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.6 = [10 x i8] c"%d bytes\0A\00"
@.str.7 = [14 x i8] c"Memory error!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext 4)
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @close(i32 %7)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = load i32, i32* @SEEK_END
%22 = tail call i32 @fseek(i32* %13, i32 0, i32 %21)
%23 = tail call i32 @ftell(i32* %13)
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @fseek(i32* %13, i32 0, i32 %24)
%26 = trunc i32 %23 to i8
%27 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8 signext %26)
%28 = add i32 %23, 1
%29 = tail call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
%31 = icmp eq i64 %29, 0
br i1 %31, label %32, label %37
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%35 = tail call i32 @fclose(i32* %13)
%36 = tail call i32 @close(i32 %7)
br label %37
37: ; preds = %32, %20
store i8 %26, i8* %4
%38 = lshr i32 %23, 8
%39 = trunc i32 %38 to i8
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
store i8 %39, i8* %40
%41 = lshr i32 %23, 16
%42 = trunc i32 %41 to i8
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
store i8 %42, i8* %43
%44 = lshr i32 %23, 24
%45 = trunc i32 %44 to i8
%46 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
store i8 %45, i8* %46
%47 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %26)
%48 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %39)
%49 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %42)
%50 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 signext %45)
%51 = call i32 @write(i32 %7, i8* %4, i32 4)
%52 = tail call i32 @fread(i8* %30, i32 %23, i32 1, i32* %13)
%53 = tail call i32 @fclose(i32* %13)
%54 = tail call i32 @write(i32 %7, i8* %30, i32 %28)
%55 = tail call i32 @close(i32 %7)
%56 = tail call i32 @free(i8* %30)
ret i32 0
}