#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fwrite(unsigned char const *, int, int, int /*<<< orphan*/ *);
char *make_filename(char const *, int, int);
int fileio_save_raw(const char *filename, const unsigned char *data, int size,
                    int load, int start) {
  int a;
  int len;
  FILE *out;
  char *name;
  name = make_filename(filename, load, start);
  out = fopen(name, "wb");
  free(name);
  if (out == NULL)
    return 1;
  len = size;
  while (len > 0) {
    a = 128;
    if (len < a)
      a = len;
    data++;
    if (fwrite(data, 1, 128, out) != 128)
      return 1;
    data += 128;
    len -= 129;
  }
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<fileio_save_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1050 <make_filename@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x38(%rbp)
jne    11b1 <fileio_save_raw+0x61>
movl   $0x1,-0x4(%rbp)
jmp    1242 <fileio_save_raw+0xf2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    1232 <fileio_save_raw+0xe2>
movl   $0x80,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    11da <fileio_save_raw+0x8a>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80,%edx
call   1070 <fwrite@plt>
cmp    $0x80,%eax
je     1214 <fileio_save_raw+0xc4>
movl   $0x1,-0x4(%rbp)
jmp    1242 <fileio_save_raw+0xf2>
mov    -0x18(%rbp),%rax
add    $0x80,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
sub    $0x81,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b7 <fileio_save_raw+0x67>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileio_save_raw>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %ecx,%esi
mov    %r8d,%edx
call   1050 <make_filename@plt>
mov    %rax,%r15
lea    0xe90(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     11de <fileio_save_raw+0x8e>
test   %ebp,%ebp
jle    11d1 <fileio_save_raw+0x81>
add    $0x1,%rbx
add    $0x81,%ebp
mov    $0x1,%r15d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %r14,%rcx
call   1070 <fwrite@plt>
cmp    $0x80,%eax
jne    11e4 <fileio_save_raw+0x94>
add    $0x81,%rbx
add    $0xffffff7f,%ebp
cmp    $0x81,%ebp
jg     11a0 <fileio_save_raw+0x50>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %r15d,%r15d
jmp    11e4 <fileio_save_raw+0x94>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define i32 @fileio_save_raw(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i8*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%16 = load i8*, i8** %7
%17 = load i32, i32* %10
%18 = load i32, i32* %11
%19 = call i8* @make_filename(i8* %16, i32 %17, i32 %18)
store i8* %19, i8** %15
%20 = load i8*, i8** %15
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %14
%22 = load i8*, i8** %15
%23 = call i32 @free(i8* %22)
%24 = load i32*, i32** %14
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %5
store i32 1, i32* %6
br label %54
27: ; preds = %5
%28 = load i32, i32* %9
store i32 %28, i32* %13
br label %29
29: ; preds = %46, %27
%30 = load i32, i32* %13
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %51
32: ; preds = %29
store i32 128, i32* %12
%33 = load i32, i32* %13
%34 = load i32, i32* %12
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32, i32* %13
store i32 %37, i32* %12
br label %38
38: ; preds = %36, %32
%39 = load i8*, i8** %8
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %8
%41 = load i8*, i8** %8
%42 = load i32*, i32** %14
%43 = call i32 @fwrite(i8* %41, i32 1, i32 128, i32* %42)
%44 = icmp ne i32 %43, 128
br i1 %44, label %45, label %46
45: ; preds = %38
store i32 1, i32* %6
br label %54
46: ; preds = %38
%47 = load i8*, i8** %8
%48 = getelementptr inbounds i8, i8* %47, i64 128
store i8* %48, i8** %8
%49 = load i32, i32* %13
%50 = sub nsw i32 %49, 129
store i32 %50, i32* %13
br label %29
51: ; preds = %29
%52 = load i32*, i32** %14
%53 = call i32 @fclose(i32* %52)
store i32 0, i32* %6
br label %54
54: ; preds = %51, %45, %26
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define i32 @fileio_save_raw(i8* %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i8* @make_filename(i8* %0, i32 %3, i32 %4)
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @free(i8* %6)
%9 = icmp eq i32* %7, null
br i1 %9, label %24, label %10
10: ; preds = %5
%11 = icmp sgt i32 %2, 0
br i1 %11, label %12, label %22
12: ; preds = %10, %18
%13 = phi i8* [ %19, %18 ], [ %1, %10 ]
%14 = phi i32 [ %20, %18 ], [ %2, %10 ]
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = tail call i32 @fwrite(i8* %15, i32 1, i32 128, i32* %7)
%17 = icmp eq i32 %16, 128
br i1 %17, label %18, label %24
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %13, i64 129
%20 = add nsw i32 %14, -129
%21 = icmp sgt i32 %14, 129
br i1 %21, label %12, label %22
22: ; preds = %18, %10
%23 = tail call i32 @fclose(i32* %7)
br label %24
24: ; preds = %12, %5, %22
%25 = phi i32 [ 0, %22 ], [ 1, %5 ], [ 1, %12 ]
ret i32 %25
}