#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 const *, char *);
int fwrite(unsigned char const *, int, int, int /*<<< orphan*/ *);
int fileio_save_img(const char *filename, const unsigned char *data, int size) {
  int a;
  int len;
  FILE *out;
  out = fopen(filename, "wb");
  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_img>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    116e <fileio_save_img+0x3e>
movl   $0x1,-0x4(%rbp)
jmp    11ff <fileio_save_img+0xcf>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11ef <fileio_save_img+0xbf>
movl   $0x80,-0x20(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1197 <fileio_save_img+0x67>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
mov    $0x80,%edx
call   1050 <fwrite@plt>
cmp    $0x80,%eax
je     11d1 <fileio_save_img+0xa1>
movl   $0x1,-0x4(%rbp)
jmp    11ff <fileio_save_img+0xcf>
mov    -0x18(%rbp),%rax
add    $0x80,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
sub    $0x81,%eax
mov    %eax,-0x24(%rbp)
jmp    1174 <fileio_save_img+0x44>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileio_save_img>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
lea    0xebd(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     11ae <fileio_save_img+0x7e>
mov    %rax,%r14
test   %ebp,%ebp
jle    11a1 <fileio_save_img+0x71>
add    $0x81,%ebp
add    $0x1,%rbx
mov    $0x1,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x80,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
cmp    $0x80,%eax
jne    11b4 <fileio_save_img+0x84>
add    $0xffffff7f,%ebp
add    $0x81,%rbx
cmp    $0x81,%ebp
jg     1170 <fileio_save_img+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %r15d,%r15d
jmp    11b4 <fileio_save_img+0x84>
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_img(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 1, i32* %4
br label %43
16: ; preds = %3
%17 = load i32, i32* %7
store i32 %17, i32* %9
br label %18
18: ; preds = %35, %16
%19 = load i32, i32* %9
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %40
21: ; preds = %18
store i32 128, i32* %8
%22 = load i32, i32* %9
%23 = load i32, i32* %8
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* %9
store i32 %26, i32* %8
br label %27
27: ; preds = %25, %21
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = load i32*, i32** %10
%32 = call i32 @fwrite(i8* %30, i32 1, i32 128, i32* %31)
%33 = icmp ne i32 %32, 128
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 1, i32* %4
br label %43
35: ; preds = %27
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i64 128
store i8* %37, i8** %6
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, 129
store i32 %39, i32* %9
br label %18
40: ; preds = %18
%41 = load i32*, i32** %10
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %4
br label %43
43: ; preds = %40, %34, %15
%44 = load i32, i32* %4
ret i32 %44
}

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