#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*/ _write_int(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ _write_word(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int boxen_write(char *name, unsigned char *d, int dx, int dy, int *size,
                int type, int number_mipmaps) {
  FILE *f;
  int i, n;
  if (name == NULL || d == NULL || dx <= 0 || dy <= 0)
    return 0;
  f = fopen(name, "wb");
  if (f == NULL) {
    fprintf(stderr, "BOXEN_WRITE: Could not open file \"%s\" for writing.\n",
            name);
    return 0;
  }
  fprintf(f, "BOXEM");
  _write_int(f, type);
  _write_word(f, dx);
  _write_word(f, dy);
  _write_int(f, number_mipmaps);
  for (i = 0; i < number_mipmaps; i++)
    _write_int(f, size[i]);
  for (n = 0, i = 0; i < number_mipmaps; i++) {
    fwrite(d + n, 1, size[i], f);
    n += size[i];
  }
  fclose(f);
  return 1;
}


x86 assembly (clang, -O0):
<boxen_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ab <boxen_write+0x4b>
cmpq   $0x0,-0x18(%rbp)
je     11ab <boxen_write+0x4b>
cmpl   $0x0,-0x1c(%rbp)
jle    11ab <boxen_write+0x4b>
cmpl   $0x0,-0x20(%rbp)
jg     11b7 <boxen_write+0x57>
movl   $0x0,-0x4(%rbp)
jmp    12e2 <boxen_write+0x182>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11fe <boxen_write+0x9e>
mov    0x2e03(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e2 <boxen_write+0x182>
mov    -0x38(%rbp),%rdi
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <_write_int@plt>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <_write_word@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <_write_word@plt>
mov    -0x38(%rbp),%rdi
mov    0x10(%rbp),%esi
call   1030 <_write_int@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    1275 <boxen_write+0x115>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1030 <_write_int@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1247 <boxen_write+0xe7>
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    0x10(%rbp),%eax
jge    12d2 <boxen_write+0x172>
mov    -0x18(%rbp),%rdi
movslq -0x40(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
add    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1283 <boxen_write+0x123>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<boxen_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     1281 <boxen_write+0x121>
mov    %rsi,%r14
test   %rsi,%rsi
je     1281 <boxen_write+0x121>
mov    %edx,%ebp
test   %edx,%edx
jle    1281 <boxen_write+0x121>
test   %ecx,%ecx
jle    1281 <boxen_write+0x121>
mov    %r8,%r15
mov    %rdi,%r12
mov    %r9d,%ebx
mov    %ecx,0x4(%rsp)
lea    0xe58(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1264 <boxen_write+0x104>
mov    %rax,%r13
mov    0x40(%rsp),%r12d
lea    0xe72(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %r13,%rdi
mov    %ebx,%esi
call   1030 <_write_int@plt>
mov    %r13,%rdi
mov    %ebp,%esi
mov    %r12d,%ebp
call   1060 <_write_word@plt>
mov    %r13,%rdi
mov    0x4(%rsp),%esi
call   1060 <_write_word@plt>
mov    %r13,%rdi
mov    %r12d,%esi
call   1030 <_write_int@plt>
test   %r12d,%r12d
jle    1255 <boxen_write+0xf5>
mov    %ebp,%r12d
xor    %ebx,%ebx
nop
mov    (%r15,%rbx,4),%esi
mov    %r13,%rdi
call   1030 <_write_int@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1210 <boxen_write+0xb0>
test   %ebp,%ebp
jle    1255 <boxen_write+0xf5>
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   (%rax)
movslq %ebp,%rbp
lea    (%r14,%rbp,1),%rdi
mov    (%r15,%rbx,4),%edx
mov    $0x1,%esi
mov    %r13,%rcx
call   1080 <fwrite@plt>
add    (%r15,%rbx,4),%ebp
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1230 <boxen_write+0xd0>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    $0x1,%ebx
jmp    1281 <boxen_write+0x121>
mov    0x2d75(%rip),%rax
movslq (%rax),%rdi
lea    0xd8e(%rip),%rsi
xor    %ebx,%ebx
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [52 x i8] c"BOXEN_WRITE: Could not open file \22%s\22 for writing.\0A\00"
@.str.2 = [6 x i8] c"BOXEM\00"
define i32 @boxen_write(i8* %0, i8* %1, i32 %2, i32 %3, i32* %4, i32 %5, i32 %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32* %4, i32** %13
store i32 %5, i32* %14
store i32 %6, i32* %15
%19 = load i8*, i8** %9
%20 = icmp eq i8* %19, null
br i1 %20, label %30, label %21
21: ; preds = %7
%22 = load i8*, i8** %10
%23 = icmp eq i8* %22, null
br i1 %23, label %30, label %24
24: ; preds = %21
%25 = load i32, i32* %11
%26 = icmp sle i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i32, i32* %12
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27, %24, %21, %7
store i32 0, i32* %8
br label %102
31: ; preds = %27
%32 = load i8*, i8** %9
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %33, i32** %16
%34 = load i32*, i32** %16
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = sext i32 %37 to i64
%39 = inttoptr i64 %38 to i32*
%40 = load i8*, i8** %9
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %40)
store i32 0, i32* %8
br label %102
42: ; preds = %31
%43 = load i32*, i32** %16
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32*, i32** %16
%46 = load i32, i32* %14
%47 = call i32 @_write_int(i32* %45, i32 %46)
%48 = load i32*, i32** %16
%49 = load i32, i32* %11
%50 = call i32 @_write_word(i32* %48, i32 %49)
%51 = load i32*, i32** %16
%52 = load i32, i32* %12
%53 = call i32 @_write_word(i32* %51, i32 %52)
%54 = load i32*, i32** %16
%55 = load i32, i32* %15
%56 = call i32 @_write_int(i32* %54, i32 %55)
store i32 0, i32* %17
br label %57
57: ; preds = %69, %42
%58 = load i32, i32* %17
%59 = load i32, i32* %15
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %72
61: ; preds = %57
%62 = load i32*, i32** %16
%63 = load i32*, i32** %13
%64 = load i32, i32* %17
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
%67 = load i32, i32* %66
%68 = call i32 @_write_int(i32* %62, i32 %67)
br label %69
69: ; preds = %61
%70 = load i32, i32* %17
%71 = add nsw i32 %70, 1
store i32 %71, i32* %17
br label %57
72: ; preds = %57
store i32 0, i32* %18
store i32 0, i32* %17
br label %73
73: ; preds = %96, %72
%74 = load i32, i32* %17
%75 = load i32, i32* %15
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %99
77: ; preds = %73
%78 = load i8*, i8** %10
%79 = load i32, i32* %18
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %78, i64 %80
%82 = load i32*, i32** %13
%83 = load i32, i32* %17
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds i32, i32* %82, i64 %84
%86 = load i32, i32* %85
%87 = load i32*, i32** %16
%88 = call i32 @fwrite(i8* %81, i32 1, i32 %86, i32* %87)
%89 = load i32*, i32** %13
%90 = load i32, i32* %17
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i32, i32* %89, i64 %91
%93 = load i32, i32* %92
%94 = load i32, i32* %18
%95 = add nsw i32 %94, %93
store i32 %95, i32* %18
br label %96
96: ; preds = %77
%97 = load i32, i32* %17
%98 = add nsw i32 %97, 1
store i32 %98, i32* %17
br label %73
99: ; preds = %73
%100 = load i32*, i32** %16
%101 = call i32 @fclose(i32* %100)
store i32 1, i32* %8
br label %102
102: ; preds = %99, %36, %30
%103 = load i32, i32* %8
ret i32 %103
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [52 x i8] c"BOXEN_WRITE: Could not open file \22%s\22 for writing.\0A\00"
@.str.2 = [6 x i8] c"BOXEM\00"
define i32 @boxen_write(i8* %0, i8* %1, i32 %2, i32 %3, i32* nocapture %4, i32 %5, i32 %6) {
%8 = icmp eq i8* %0, null
%9 = icmp eq i8* %1, null
%10 = or i1 %8, %9
%11 = icmp slt i32 %2, 1
%12 = or i1 %10, %11
%13 = icmp slt i32 %3, 1
%14 = or i1 %12, %13
br i1 %14, label %56, label %15
15: ; preds = %7
%16 = tail call i32* @fopen(i8* %0, 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 %23
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %56
23: ; preds = %15
%24 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 5, i64 1, i32* %16)
%25 = tail call i32 @_write_int(i32* %16, i32 %5)
%26 = tail call i32 @_write_word(i32* %16, i32 %2)
%27 = tail call i32 @_write_word(i32* %16, i32 %3)
%28 = tail call i32 @_write_int(i32* %16, i32 %6)
%29 = icmp sgt i32 %6, 0
br i1 %29, label %30, label %54
30: ; preds = %23
%31 = zext i32 %6 to i64
br label %35
32: ; preds = %35
br i1 %29, label %33, label %54
33: ; preds = %32
%34 = zext i32 %6 to i64
br label %42
35: ; preds = %30, %35
%36 = phi i64 [ 0, %30 ], [ %40, %35 ]
%37 = getelementptr inbounds i32, i32* %4, i64 %36
%38 = load i32, i32* %37
%39 = tail call i32 @_write_int(i32* %16, i32 %38)
%40 = add nuw nsw i64 %36, 1
%41 = icmp eq i64 %40, %31
br i1 %41, label %32, label %35
42: ; preds = %33, %42
%43 = phi i64 [ 0, %33 ], [ %52, %42 ]
%44 = phi i32 [ 0, %33 ], [ %51, %42 ]
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %1, i64 %45
%47 = getelementptr inbounds i32, i32* %4, i64 %43
%48 = load i32, i32* %47
%49 = tail call i32 @fwrite(i8* %46, i32 1, i32 %48, i32* %16)
%50 = load i32, i32* %47
%51 = add nsw i32 %50, %44
%52 = add nuw nsw i64 %43, 1
%53 = icmp eq i64 %52, %34
br i1 %53, label %54, label %42
54: ; preds = %42, %23, %32
%55 = tail call i32 @fclose(i32* %16)
br label %56
56: ; preds = %7, %54, %18
%57 = phi i32 [ 0, %18 ], [ 1, %54 ], [ 0, %7 ]
ret i32 %57
}