#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int calc_csum(unsigned char *, unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(unsigned char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, unsigned int, int,
                          int /*<<< orphan*/ *);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ *openinfile(char *);
int /*<<< orphan*/ *openoutfile(char *);
int /*<<< orphan*/ put_uint32le(int, unsigned char *);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ stderr;
int gigabeat_s_code(char *infile, char *outfile) {
  FILE *in, *out;
  unsigned int size;
  unsigned int newsize;
  unsigned char *buf;
  size_t rc;
  in = openinfile(infile);
  out = openoutfile(outfile);
  fseek(in, 0, SEEK_END);
  size = ftell(in);
  newsize = 15 + 16 + 12 + size + 12;
  buf = malloc(newsize);
  if (buf == NULL) {
    fprintf(stderr, "Not enough memory to perform the operation. Aborting.\n");
    return 1;
  }
  fseek(in, 0, SEEK_SET);
  rc = fread(buf + 43, 1, size, in);
  fclose(in);
  if (rc != size) {
    fprintf(stderr, "Failed reading from %s.\n", infile);
    return 2;
  }
  sprintf((char *)buf, "B000FF\n");
  put_uint32le(0x88200000, buf + 7);
  put_uint32le(0xCC0CD8, buf + 11);
  put_uint32le(0x88065A10, buf + 15);
  put_uint32le(4, buf + 19);
  put_uint32le(0xE3A00001, buf + 27);
  put_uint32le(calc_csum(buf + 27, 4), buf + 23);
  put_uint32le(0x8a000000, buf + 31);
  put_uint32le(size, buf + 35);
  put_uint32le(calc_csum(buf + 43, size), buf + 39);
  put_uint32le(0, buf + newsize - 12);
  put_uint32le(0x8a000000, buf + newsize - 8);
  put_uint32le(0, buf + newsize - 4);
  fwrite(buf, newsize, 1, out);
  fclose(out);
  fprintf(stderr, "File processed successfully\n");
  return (0);
}


x86 assembly (clang, -O0):
<gigabeat_s_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <openinfile@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <openoutfile@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x2b,%eax
add    $0xc,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1250 <gigabeat_s_code+0x90>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    140b <gigabeat_s_code+0x24b>
mov    -0x20(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x38(%rbp),%rdi
add    $0x2b,%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    -0x2c(%rbp),%ecx
cmp    %rcx,%rax
je     12c1 <gigabeat_s_code+0x101>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    140b <gigabeat_s_code+0x24b>
mov    -0x38(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x38(%rbp),%rsi
add    $0x7,%rsi
mov    $0x88200000,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
add    $0xb,%rsi
mov    $0xcc0cd8,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
add    $0xf,%rsi
mov    $0x88065a10,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
add    $0x13,%rsi
mov    $0x4,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
add    $0x1b,%rsi
mov    $0xe3a00001,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rdi
add    $0x1b,%rdi
mov    $0x4,%esi
call   1030 <calc_csum@plt>
mov    %eax,%edi
mov    -0x38(%rbp),%rsi
add    $0x17,%rsi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
add    $0x1f,%rsi
mov    $0x8a000000,%edi
call   1050 <put_uint32le@plt>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
add    $0x23,%rsi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rdi
add    $0x2b,%rdi
mov    -0x2c(%rbp),%esi
call   1030 <calc_csum@plt>
mov    %eax,%edi
mov    -0x38(%rbp),%rsi
add    $0x27,%rsi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%eax
add    %rax,%rsi
add    $0xfffffffffffffff4,%rsi
xor    %edi,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%eax
add    %rax,%rsi
add    $0xfffffffffffffff8,%rsi
mov    $0x8a000000,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%eax
add    %rax,%rsi
add    $0xfffffffffffffffc,%rsi
xor    %edi,%edi
call   1050 <put_uint32le@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   10e0 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    0x2be4(%rip),%rax
mov    (%rax),%edi
lea    0xc5b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gigabeat_s_code>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
call   1090 <openinfile@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   10a0 <openoutfile@plt>
mov    %rax,%r13
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r15,%rdi
call   1080 <ftell@plt>
mov    %eax,%r12d
lea    0x37(%r12),%ebx
mov    %ebx,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1369 <gigabeat_s_code+0x1b9>
mov    %rax,%rbp
mov    %ebx,0xc(%rsp)
mov    %r13,0x10(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
lea    0x2b(%rbp),%rdi
mov    %rdi,%rbx
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r12d,%eax
cmp    %rax,%r13
jne    1387 <gigabeat_s_code+0x1d7>
movabs $0xa464630303042,%rax
mov    %rax,0x0(%rbp)
lea    0x7(%rbp),%rsi
mov    $0x88200000,%edi
call   1050 <put_uint32le@plt>
lea    0xb(%rbp),%rsi
mov    $0xcc0cd8,%edi
call   1050 <put_uint32le@plt>
lea    0xf(%rbp),%rsi
mov    $0x88065a10,%edi
call   1050 <put_uint32le@plt>
lea    0x13(%rbp),%rsi
mov    $0x4,%edi
call   1050 <put_uint32le@plt>
lea    0x1b(%rbp),%r14
mov    $0xe3a00001,%edi
mov    %r14,%rsi
call   1050 <put_uint32le@plt>
mov    %r14,%rdi
mov    $0x4,%esi
call   1030 <calc_csum@plt>
lea    0x17(%rbp),%rsi
mov    %eax,%edi
call   1050 <put_uint32le@plt>
lea    0x1f(%rbp),%rsi
mov    $0x8a000000,%edi
call   1050 <put_uint32le@plt>
lea    0x23(%rbp),%rsi
mov    %r12d,%edi
call   1050 <put_uint32le@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <calc_csum@plt>
lea    0x27(%rbp),%rsi
mov    %eax,%edi
call   1050 <put_uint32le@plt>
mov    0xc(%rsp),%r14d
mov    %r14d,%ebx
lea    (%rbx,%rbp,1),%rsi
add    $0xfffffffffffffff4,%rsi
xor    %edi,%edi
call   1050 <put_uint32le@plt>
lea    (%rbx,%rbp,1),%rsi
add    $0xfffffffffffffff8,%rsi
mov    $0x8a000000,%edi
call   1050 <put_uint32le@plt>
lea    (%rbx,%rbp,1),%rsi
add    $0xfffffffffffffffc,%rsi
xor    %edi,%edi
call   1050 <put_uint32le@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    0x10(%rsp),%rbx
mov    %rbx,%rcx
call   10d0 <fwrite@plt>
mov    %rbx,%rdi
xor    %ebx,%ebx
call   1060 <fclose@plt>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
lea    0xcf0(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    13a6 <gigabeat_s_code+0x1f6>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
lea    0xc87(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebx
jmp    13a6 <gigabeat_s_code+0x1f6>
mov    0x2c4a(%rip),%rax
mov    (%rax),%edi
lea    0xca0(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [55 x i8] c"Not enough memory to perform the operation. Aborting.\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [25 x i8] c"Failed reading from %s.\0A\00"
@.str.2 = [8 x i8] c"B000FF\0A\00"
@.str.3 = [29 x i8] c"File processed successfully\0A\00"
define i32 @gigabeat_s_code(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = call i32* @openinfile(i8* %12)
store i32* %13, i32** %6
%14 = load i8*, i8** %5
%15 = call i32* @openoutfile(i8* %14)
store i32* %15, i32** %7
%16 = load i32*, i32** %6
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %6
%20 = call i32 @ftell(i32* %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = add i32 43, %21
%23 = add i32 %22, 12
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %10
%26 = load i8*, i8** %10
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %2
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %114
31: ; preds = %2
%32 = load i32*, i32** %6
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %32, i32 0, i32 %33)
%35 = load i8*, i8** %10
%36 = getelementptr inbounds i8, i8* %35, i64 43
%37 = load i32, i32* %8
%38 = load i32*, i32** %6
%39 = call i64 @fread(i8* %36, i32 1, i32 %37, i32* %38)
store i64 %39, i64* %11
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
%42 = load i64, i64* %11
%43 = load i32, i32* %8
%44 = zext i32 %43 to i64
%45 = icmp ne i64 %42, %44
br i1 %45, label %46, label %50
46: ; preds = %31
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %4
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %48)
store i32 2, i32* %3
br label %114
50: ; preds = %31
%51 = load i8*, i8** %10
%52 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%53 = load i8*, i8** %10
%54 = getelementptr inbounds i8, i8* %53, i64 7
%55 = call i32 @put_uint32le(i32 -2011168768, i8* %54)
%56 = load i8*, i8** %10
%57 = getelementptr inbounds i8, i8* %56, i64 11
%58 = call i32 @put_uint32le(i32 13372632, i8* %57)
%59 = load i8*, i8** %10
%60 = getelementptr inbounds i8, i8* %59, i64 15
%61 = call i32 @put_uint32le(i32 -2012849648, i8* %60)
%62 = load i8*, i8** %10
%63 = getelementptr inbounds i8, i8* %62, i64 19
%64 = call i32 @put_uint32le(i32 4, i8* %63)
%65 = load i8*, i8** %10
%66 = getelementptr inbounds i8, i8* %65, i64 27
%67 = call i32 @put_uint32le(i32 -476053503, i8* %66)
%68 = load i8*, i8** %10
%69 = getelementptr inbounds i8, i8* %68, i64 27
%70 = call i32 @calc_csum(i8* %69, i32 4)
%71 = load i8*, i8** %10
%72 = getelementptr inbounds i8, i8* %71, i64 23
%73 = call i32 @put_uint32le(i32 %70, i8* %72)
%74 = load i8*, i8** %10
%75 = getelementptr inbounds i8, i8* %74, i64 31
%76 = call i32 @put_uint32le(i32 -1979711488, i8* %75)
%77 = load i32, i32* %8
%78 = load i8*, i8** %10
%79 = getelementptr inbounds i8, i8* %78, i64 35
%80 = call i32 @put_uint32le(i32 %77, i8* %79)
%81 = load i8*, i8** %10
%82 = getelementptr inbounds i8, i8* %81, i64 43
%83 = load i32, i32* %8
%84 = call i32 @calc_csum(i8* %82, i32 %83)
%85 = load i8*, i8** %10
%86 = getelementptr inbounds i8, i8* %85, i64 39
%87 = call i32 @put_uint32le(i32 %84, i8* %86)
%88 = load i8*, i8** %10
%89 = load i32, i32* %9
%90 = zext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %88, i64 %90
%92 = getelementptr inbounds i8, i8* %91, i64 -12
%93 = call i32 @put_uint32le(i32 0, i8* %92)
%94 = load i8*, i8** %10
%95 = load i32, i32* %9
%96 = zext i32 %95 to i64
%97 = getelementptr inbounds i8, i8* %94, i64 %96
%98 = getelementptr inbounds i8, i8* %97, i64 -8
%99 = call i32 @put_uint32le(i32 -1979711488, i8* %98)
%100 = load i8*, i8** %10
%101 = load i32, i32* %9
%102 = zext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %100, i64 %102
%104 = getelementptr inbounds i8, i8* %103, i64 -4
%105 = call i32 @put_uint32le(i32 0, i8* %104)
%106 = load i8*, i8** %10
%107 = load i32, i32* %9
%108 = load i32*, i32** %7
%109 = call i32 @fwrite(i8* %106, i32 %107, i32 1, i32* %108)
%110 = load i32*, i32** %7
%111 = call i32 @fclose(i32* %110)
%112 = load i32, i32* @stderr
%113 = call i32 (i32, i8*, ...) @fprintf(i32 %112, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %114
114: ; preds = %50, %46, %28
%115 = load i32, i32* %3
ret i32 %115
}

LLVM IR (-O2):
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [55 x i8] c"Not enough memory to perform the operation. Aborting.\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [25 x i8] c"Failed reading from %s.\0A\00"
@.str.3 = [29 x i8] c"File processed successfully\0A\00"
define i32 @gigabeat_s_code(i8* %0, i8* %1) {
%3 = tail call i32* @openinfile(i8* %0)
%4 = tail call i32* @openoutfile(i8* %1)
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %3, i32 0, i32 %5)
%7 = tail call i32 @ftell(i32* %3)
%8 = add i32 %7, 55
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0))
br label %59
14: ; preds = %2
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %3, i32 0, i32 %15)
%17 = getelementptr inbounds i8, i8* %9, i64 43
%18 = tail call i64 @fread(i8* %17, i32 1, i32 %7, i32* %3)
%19 = tail call i32 @fclose(i32* %3)
%20 = zext i32 %7 to i64
%21 = icmp eq i64 %18, %20
br i1 %21, label %25, label %22
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %59
25: ; preds = %14
%26 = bitcast i8* %9 to i64*
store i64 2892017037226050, i64* %26
%27 = getelementptr inbounds i8, i8* %9, i64 7
%28 = tail call i32 @put_uint32le(i32 -2011168768, i8* %27)
%29 = getelementptr inbounds i8, i8* %9, i64 11
%30 = tail call i32 @put_uint32le(i32 13372632, i8* %29)
%31 = getelementptr inbounds i8, i8* %9, i64 15
%32 = tail call i32 @put_uint32le(i32 -2012849648, i8* %31)
%33 = getelementptr inbounds i8, i8* %9, i64 19
%34 = tail call i32 @put_uint32le(i32 4, i8* %33)
%35 = getelementptr inbounds i8, i8* %9, i64 27
%36 = tail call i32 @put_uint32le(i32 -476053503, i8* %35)
%37 = tail call i32 @calc_csum(i8* %35, i32 4)
%38 = getelementptr inbounds i8, i8* %9, i64 23
%39 = tail call i32 @put_uint32le(i32 %37, i8* %38)
%40 = getelementptr inbounds i8, i8* %9, i64 31
%41 = tail call i32 @put_uint32le(i32 -1979711488, i8* %40)
%42 = getelementptr inbounds i8, i8* %9, i64 35
%43 = tail call i32 @put_uint32le(i32 %7, i8* %42)
%44 = tail call i32 @calc_csum(i8* %17, i32 %7)
%45 = getelementptr inbounds i8, i8* %9, i64 39
%46 = tail call i32 @put_uint32le(i32 %44, i8* %45)
%47 = zext i32 %8 to i64
%48 = getelementptr inbounds i8, i8* %9, i64 %47
%49 = getelementptr inbounds i8, i8* %48, i64 -12
%50 = tail call i32 @put_uint32le(i32 0, i8* %49)
%51 = getelementptr inbounds i8, i8* %48, i64 -8
%52 = tail call i32 @put_uint32le(i32 -1979711488, i8* %51)
%53 = getelementptr inbounds i8, i8* %48, i64 -4
%54 = tail call i32 @put_uint32le(i32 0, i8* %53)
%55 = tail call i32 @fwrite(i8* %9, i32 %8, i32 1, i32* %4)
%56 = tail call i32 @fclose(i32* %4)
%57 = load i32, i32* @stderr
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %25, %22, %11
%60 = phi i32 [ 1, %11 ], [ 2, %22 ], [ 0, %25 ]
ret i32 %60
}