#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 fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int main(int argc, char *argv[]) {
  for (int i = 1; i < argc; i++) {
    FILE *in = fopen(argv[i], "rb");
    if (!in) {
      printf("E: Couldn't open %s.\n", argv[i]);
      continue;
    }
    char outfn[256];
    sprintf(outfn, "%s-trim", argv[i]);
    FILE *out = fopen(outfn, "wb");
    if (!out) {
      printf("E: Couldn't create %s.\n", outfn);
      continue;
      fclose(in);
    }
    uint8_t sram[0x500];
    if (fread(sram, 1, 0x500, in) < 0x500) {
      printf("E: %s shorter than %d bytes.\n", argv[i], 0x500);
    }
    fclose(in);
    if (fwrite(sram, 1, 0x500, out) < 0x500) {
      printf("E: Wrote less than %d bytes to %s.\n", 0x500, outfn);
    }
    fclose(out);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1530,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12dc <main+0x17c>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe61(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d2 <main+0x72>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ce <main+0x16e>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe2d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    1233 <main+0xd3>
lea    -0x120(%rbp),%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ce <main+0x16e>
lea    -0x1530(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x500,%edx
call   1030 <fread@plt>
cmp    $0x500,%eax
jge    1277 <main+0x117>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdd1(%rip),%rdi
mov    $0x500,%edx
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x1530(%rbp),%rdi
mov    -0x128(%rbp),%rcx
mov    $0x1,%esi
mov    $0x500,%edx
call   1080 <fwrite@plt>
cmp    $0x500,%eax
jge    12c2 <main+0x162>
lea    -0x120(%rbp),%rdx
lea    0xda4(%rip),%rdi
mov    $0x500,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x128(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1180 <main+0x20>
mov    -0x4(%rbp),%eax
add    $0x1530,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1518,%rsp
cmp    $0x2,%edi
jl     12a6 <main+0x146>
mov    %rsi,%r15
mov    %edi,%eax
mov    %rax,0x8(%rsp)
mov    $0x1,%r14d
lea    0x10(%rsp),%rbx
lea    0x110(%rsp),%r13
jmp    11b7 <main+0x57>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1040 <fclose@plt>
add    $0x1,%r14
cmp    %r14,0x8(%rsp)
je     12a6 <main+0x146>
mov    (%r15,%r14,8),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1280 <main+0x120>
mov    %rax,%rbp
mov    (%r15,%r14,8),%rdx
mov    %rbx,%rdi
lea    0xe38(%rip),%rsi
call   1070 <sprintf@plt>
mov    %rbx,%rdi
lea    0xe31(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1290 <main+0x130>
mov    %rax,%r12
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x500,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
cmp    $0x4ff,%eax
jg     1234 <main+0xd4>
mov    (%r15,%r14,8),%rsi
lea    0xe14(%rip),%rdi
mov    $0x500,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x500,%edx
mov    %r12,%rcx
call   1080 <fwrite@plt>
cmp    $0x4ff,%eax
jg     11a0 <main+0x40>
lea    0xdf7(%rip),%rdi
mov    $0x500,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a0 <main+0x40>
nopw   0x0(%rax,%rax,1)
mov    (%r15,%r14,8),%rsi
lea    0xd78(%rip),%rdi
jmp    129a <main+0x13a>
nopl   (%rax)
lea    0xd8d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a8 <main+0x48>
xor    %eax,%eax
add    $0x1518,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [22 x i8] c"E: Couldn't open %s.\0A\00"
@.str.2 = [8 x i8] c"%s-trim\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [24 x i8] c"E: Couldn't create %s.\0A\00"
@.str.5 = [30 x i8] c"E: %s shorter than %d bytes.\0A\00"
@.str.6 = [36 x i8] c"E: Wrote less than %d bytes to %s.\0A\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 [256 x i8]
%9 = alloca i32*
%10 = alloca [1280 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
br label %11
11: ; preds = %71, %2
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %74
15: ; preds = %11
%16 = load i8**, i8*** %5
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8*, i8** %16, i64 %18
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %7
%22 = load i32*, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %31, label %24
24: ; preds = %15
%25 = load i8**, i8*** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %29)
br label %71
31: ; preds = %15
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%33 = load i8**, i8*** %5
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = icmp ne i32* %41, null
br i1 %42, label %46, label %43
43: ; preds = %31
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %71
46: ; preds = %31
%47 = getelementptr inbounds [1280 x i32], [1280 x i32]* %10, i64 0, i64 0
%48 = load i32*, i32** %7
%49 = call i32 @fread(i32* %47, i32 1, i32 1280, i32* %48)
%50 = icmp slt i32 %49, 1280
br i1 %50, label %51, label %58
51: ; preds = %46
%52 = load i8**, i8*** %5
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8*, i8** %52, i64 %54
%56 = load i8*, i8** %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %56, i32 1280)
br label %58
58: ; preds = %51, %46
%59 = load i32*, i32** %7
%60 = call i32 @fclose(i32* %59)
%61 = getelementptr inbounds [1280 x i32], [1280 x i32]* %10, i64 0, i64 0
%62 = load i32*, i32** %9
%63 = call i32 @fwrite(i32* %61, i32 1, i32 1280, i32* %62)
%64 = icmp slt i32 %63, 1280
br i1 %64, label %65, label %68
65: ; preds = %58
%66 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 1280, i8* %66)
br label %68
68: ; preds = %65, %58
%69 = load i32*, i32** %9
%70 = call i32 @fclose(i32* %69)
br label %71
71: ; preds = %68, %43, %24
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %11
74: ; preds = %11
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [22 x i8] c"E: Couldn't open %s.\0A\00"
@.str.2 = [8 x i8] c"%s-trim\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [24 x i8] c"E: Couldn't create %s.\0A\00"
@.str.5 = [30 x i8] c"E: %s shorter than %d bytes.\0A\00"
@.str.6 = [36 x i8] c"E: Wrote less than %d bytes to %s.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca [1280 x i32]
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%8 = bitcast [1280 x i32]* %4 to i8*
%9 = getelementptr inbounds [1280 x i32], [1280 x i32]* %4, i64 0, i64 0
%10 = zext i32 %0 to i64
br label %12
11: ; preds = %43, %2
ret i32 0
12: ; preds = %6, %43
%13 = phi i64 [ 1, %6 ], [ %44, %43 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 %13
%15 = load i8*, i8** %14
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i8*, i8** %14
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %43
21: ; preds = %12
%22 = load i8*, i8** %14
%23 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %7)
br label %42
28: ; preds = %21
%29 = call i32 @fread(i32* %9, i32 1, i32 1280, i32* %16)
%30 = icmp slt i32 %29, 1280
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i8*, i8** %14
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %32, i32 1280)
br label %34
34: ; preds = %31, %28
%35 = call i32 @fclose(i32* %16)
%36 = call i32 @fwrite(i32* %9, i32 1, i32 1280, i32* %24)
%37 = icmp slt i32 %36, 1280
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 1280, i8* %7)
br label %40
40: ; preds = %38, %34
%41 = call i32 @fclose(i32* %24)
br label %42
42: ; preds = %40, %26
br label %43
43: ; preds = %42, %18
%44 = add nuw nsw i64 %13, 1
%45 = icmp eq i64 %44, %10
br i1 %45, label %11, label %12
}