#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 ulong;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ bwrite_endian(int, int, unsigned char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int WriteBMP24(const char *fname, unsigned char (*data)[3], int w, int h) {
  FILE *fp = fopen(fname, "wb");
  int Endian = 1;
  unsigned char buffer[40];
  ulong fileSize;
  ulong imageSize;
  int x, y;
  if (!fp)
    return -1;
  imageSize = 3 * w * h;
  fileSize = 14 + 40 + imageSize;
  memset(buffer, 0x00, 14);
  buffer[0] = 66;
  buffer[1] = 77;
  bwrite_endian(fileSize, 4, buffer + 2, Endian);
  bwrite_endian(0, 4, buffer + 6, Endian);
  bwrite_endian(54, 4, buffer + 10, Endian);
  fwrite(buffer, 1, 14, fp);
  memset(buffer, 0x00, 40);
  bwrite_endian(40, 4, buffer + 0, Endian);
  bwrite_endian(w, 4, buffer + 4, Endian);
  bwrite_endian(h, 4, buffer + 8, Endian);
  bwrite_endian(1, 2, buffer + 12, Endian);
  bwrite_endian(24, 2, buffer + 14, Endian);
  bwrite_endian(0, 4, buffer + 16, Endian);
  bwrite_endian(imageSize, 4, buffer + 20, Endian);
  bwrite_endian(0x10000, 4, buffer + 24, Endian);
  bwrite_endian(0x10000, 4, buffer + 28, Endian);
  bwrite_endian(0x1000000, 4, buffer + 32, Endian);
  bwrite_endian(0, 4, buffer + 36, Endian);
  fwrite(buffer, 1, 40, fp);
  memset(buffer, 0x00, 4);
  for (y = h - 1; y >= 0; --y) {
    for (x = 0; x < w; ++x) {
      fwrite(&data[x + y * w][2], 1, 1, fp);
      fwrite(&data[x + y * w][1], 1, 1, fp);
      fwrite(&data[x + y * w][0], 1, 1, fp);
    }
    if (x % 4 != 0)
      fwrite(buffer, 1, x % 4, fp);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<WriteBMP24>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1198 <WriteBMP24+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1474 <WriteBMP24+0x324>
imul   $0x3,-0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%eax
add    $0x36,%eax
mov    %eax,-0x64(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0xe,%edx
call   1050 <memset@plt>
movb   $0x42,-0x60(%rbp)
movb   $0x4d,-0x5f(%rbp)
mov    -0x64(%rbp),%edi
lea    -0x60(%rbp),%rdx
add    $0x2,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0x6,%rdx
mov    -0x2c(%rbp),%ecx
xor    %edi,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0xa,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x36,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0xe,%edx
call   1070 <fwrite@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
lea    -0x60(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x28,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
mov    -0x1c(%rbp),%edi
lea    -0x60(%rbp),%rdx
add    $0x4,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
mov    -0x20(%rbp),%edi
lea    -0x60(%rbp),%rdx
add    $0x8,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0xc,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x1,%edi
mov    $0x2,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0xe,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x18,%edi
mov    $0x2,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0x10,%rdx
mov    -0x2c(%rbp),%ecx
xor    %edi,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
mov    -0x68(%rbp),%edi
lea    -0x60(%rbp),%rdx
add    $0x14,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0x18,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x10000,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0x1c,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x10000,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0x20,%rdx
mov    -0x2c(%rbp),%ecx
mov    $0x1000000,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdx
add    $0x24,%rdx
mov    -0x2c(%rbp),%ecx
xor    %edi,%edi
mov    $0x4,%esi
call   1030 <bwrite_endian@plt>
lea    -0x60(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x28,%edx
call   1070 <fwrite@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jl     1464 <WriteBMP24+0x314>
movl   $0x0,-0x6c(%rbp)
mov    -0x6c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1420 <WriteBMP24+0x2d0>
mov    -0x18(%rbp),%rdi
mov    -0x6c(%rbp),%eax
mov    -0x70(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
add    %ecx,%eax
cltq
imul   $0x3,%rax,%rax
add    %rax,%rdi
add    $0x2,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x18(%rbp),%rdi
mov    -0x6c(%rbp),%eax
mov    -0x70(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
add    %ecx,%eax
cltq
imul   $0x3,%rax,%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x18(%rbp),%rdi
mov    -0x6c(%rbp),%eax
mov    -0x70(%rbp),%ecx
imul   -0x1c(%rbp),%ecx
add    %ecx,%eax
cltq
imul   $0x3,%rax,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
jmp    1383 <WriteBMP24+0x233>
mov    -0x6c(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1451 <WriteBMP24+0x301>
lea    -0x60(%rbp),%rdi
mov    -0x6c(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
jmp    1456 <WriteBMP24+0x306>
mov    -0x70(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x70(%rbp)
jmp    1372 <WriteBMP24+0x222>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteBMP24>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %ecx,%r12d
mov    %edx,%r14d
mov    %rsi,%rbp
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     141e <WriteBMP24+0x2be>
mov    %rax,%rbx
mov    %rbp,0x40(%rsp)
mov    %r14d,%eax
imul   %r12d,%eax
lea    (%rax,%rax,2),%r15d
lea    (%rax,%rax,2),%ebp
add    $0x36,%ebp
lea    0x10(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0xe,%edx
call   1050 <memset@plt>
movw   $0x4d42,0x10(%rsp)
lea    0x12(%rsp),%rdx
mov    %ebp,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x16(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x1a(%rsp),%rdx
mov    $0x36,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0xe,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    $0x28,%edi
mov    $0x4,%esi
mov    %r13,%rdx
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x14(%rsp),%rdx
mov    %r14d,0xc(%rsp)
mov    %r14d,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x18(%rsp),%rdx
mov    %r12d,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x1c(%rsp),%rdx
mov    $0x1,%edi
mov    $0x2,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x1e(%rsp),%rdx
mov    $0x18,%edi
mov    $0x2,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x20(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x24(%rsp),%rdx
mov    %r15d,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x28(%rsp),%rdx
mov    $0x10000,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x2c(%rsp),%rdx
mov    $0x10000,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x30(%rsp),%rdx
mov    $0x1000000,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
lea    0x34(%rsp),%rdx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0x1,%ecx
call   1030 <bwrite_endian@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x28,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
test   %r12d,%r12d
jle    1412 <WriteBMP24+0x2b2>
mov    %r12d,%r13d
mov    0xc(%rsp),%edx
movslq %edx,%rax
mov    %edx,%ecx
and    $0x3,%edx
mov    %edx,0x8(%rsp)
lea    -0x1(%r13),%rdx
imul   %rax,%rdx
lea    (%rdx,%rdx,2),%rdx
mov    0x40(%rsp),%rsi
lea    (%rsi,%rdx,1),%rbp
add    $0x2,%rbp
lea    (%rax,%rax,2),%r15
neg    %r15
lea    (%rcx,%rcx,2),%r14
jmp    13b0 <WriteBMP24+0x250>
nopl   0x0(%rax)
lea    -0x1(%r13),%rax
add    %r15,%rbp
cmp    $0x1,%r13
mov    %rax,%r13
jle    1412 <WriteBMP24+0x2b2>
cmpl   $0x0,0xc(%rsp)
jle    13a0 <WriteBMP24+0x240>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
movsbl 0x0(%rbp,%r12,1),%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
movsbl -0x1(%rbp,%r12,1),%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
movsbl -0x2(%rbp,%r12,1),%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
add    $0x3,%r12
cmp    %r12,%r14
jne    13c0 <WriteBMP24+0x260>
cmpl   $0x0,0x8(%rsp)
je     13a0 <WriteBMP24+0x240>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    0x8(%rsp),%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
jmp    13a0 <WriteBMP24+0x240>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1423 <WriteBMP24+0x2c3>
mov    $0xffffffff,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define i32 @WriteBMP24(i8* %0, [3 x i8]* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [3 x i8]*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca [40 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %6
store [3 x i8]* %1, [3 x i8]** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%17 = load i8*, i8** %6
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %10
store i32 1, i32* %11
%19 = load i32*, i32** %10
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %4
store i32 -1, i32* %5
br label %167
22: ; preds = %4
%23 = load i32, i32* %8
%24 = mul nsw i32 3, %23
%25 = load i32, i32* %9
%26 = mul nsw i32 %24, %25
store i32 %26, i32* %14
%27 = load i32, i32* %14
%28 = add nsw i32 54, %27
store i32 %28, i32* %13
%29 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 14)
%31 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
store i8 66, i8* %31
%32 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 1
store i8 77, i8* %32
%33 = load i32, i32* %13
%34 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%35 = getelementptr inbounds i8, i8* %34, i64 2
%36 = load i32, i32* %11
%37 = call i32 @bwrite_endian(i32 %33, i32 4, i8* %35, i32 %36)
%38 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%39 = getelementptr inbounds i8, i8* %38, i64 6
%40 = load i32, i32* %11
%41 = call i32 @bwrite_endian(i32 0, i32 4, i8* %39, i32 %40)
%42 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%43 = getelementptr inbounds i8, i8* %42, i64 10
%44 = load i32, i32* %11
%45 = call i32 @bwrite_endian(i32 54, i32 4, i8* %43, i32 %44)
%46 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%47 = load i32*, i32** %10
%48 = call i32 @fwrite(i8* %46, i32 1, i32 14, i32* %47)
%49 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%50 = call i32 @memset(i8* %49, i32 0, i32 40)
%51 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%52 = getelementptr inbounds i8, i8* %51, i64 0
%53 = load i32, i32* %11
%54 = call i32 @bwrite_endian(i32 40, i32 4, i8* %52, i32 %53)
%55 = load i32, i32* %8
%56 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%57 = getelementptr inbounds i8, i8* %56, i64 4
%58 = load i32, i32* %11
%59 = call i32 @bwrite_endian(i32 %55, i32 4, i8* %57, i32 %58)
%60 = load i32, i32* %9
%61 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%62 = getelementptr inbounds i8, i8* %61, i64 8
%63 = load i32, i32* %11
%64 = call i32 @bwrite_endian(i32 %60, i32 4, i8* %62, i32 %63)
%65 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%66 = getelementptr inbounds i8, i8* %65, i64 12
%67 = load i32, i32* %11
%68 = call i32 @bwrite_endian(i32 1, i32 2, i8* %66, i32 %67)
%69 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%70 = getelementptr inbounds i8, i8* %69, i64 14
%71 = load i32, i32* %11
%72 = call i32 @bwrite_endian(i32 24, i32 2, i8* %70, i32 %71)
%73 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%74 = getelementptr inbounds i8, i8* %73, i64 16
%75 = load i32, i32* %11
%76 = call i32 @bwrite_endian(i32 0, i32 4, i8* %74, i32 %75)
%77 = load i32, i32* %14
%78 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%79 = getelementptr inbounds i8, i8* %78, i64 20
%80 = load i32, i32* %11
%81 = call i32 @bwrite_endian(i32 %77, i32 4, i8* %79, i32 %80)
%82 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%83 = getelementptr inbounds i8, i8* %82, i64 24
%84 = load i32, i32* %11
%85 = call i32 @bwrite_endian(i32 65536, i32 4, i8* %83, i32 %84)
%86 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%87 = getelementptr inbounds i8, i8* %86, i64 28
%88 = load i32, i32* %11
%89 = call i32 @bwrite_endian(i32 65536, i32 4, i8* %87, i32 %88)
%90 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%91 = getelementptr inbounds i8, i8* %90, i64 32
%92 = load i32, i32* %11
%93 = call i32 @bwrite_endian(i32 16777216, i32 4, i8* %91, i32 %92)
%94 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%95 = getelementptr inbounds i8, i8* %94, i64 36
%96 = load i32, i32* %11
%97 = call i32 @bwrite_endian(i32 0, i32 4, i8* %95, i32 %96)
%98 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%99 = load i32*, i32** %10
%100 = call i32 @fwrite(i8* %98, i32 1, i32 40, i32* %99)
%101 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%102 = call i32 @memset(i8* %101, i32 0, i32 4)
%103 = load i32, i32* %9
%104 = sub nsw i32 %103, 1
store i32 %104, i32* %16
br label %105
105: ; preds = %161, %22
%106 = load i32, i32* %16
%107 = icmp sge i32 %106, 0
br i1 %107, label %108, label %164
108: ; preds = %105
store i32 0, i32* %15
br label %109
109: ; preds = %147, %108
%110 = load i32, i32* %15
%111 = load i32, i32* %8
%112 = icmp slt i32 %110, %111
br i1 %112, label %113, label %150
113: ; preds = %109
%114 = load [3 x i8]*, [3 x i8]** %7
%115 = load i32, i32* %15
%116 = load i32, i32* %16
%117 = load i32, i32* %8
%118 = mul nsw i32 %116, %117
%119 = add nsw i32 %115, %118
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds [3 x i8], [3 x i8]* %114, i64 %120
%122 = getelementptr inbounds [3 x i8], [3 x i8]* %121, i64 0, i64 2
%123 = load i32*, i32** %10
%124 = call i32 @fwrite(i8* %122, i32 1, i32 1, i32* %123)
%125 = load [3 x i8]*, [3 x i8]** %7
%126 = load i32, i32* %15
%127 = load i32, i32* %16
%128 = load i32, i32* %8
%129 = mul nsw i32 %127, %128
%130 = add nsw i32 %126, %129
%131 = sext i32 %130 to i64
%132 = getelementptr inbounds [3 x i8], [3 x i8]* %125, i64 %131
%133 = getelementptr inbounds [3 x i8], [3 x i8]* %132, i64 0, i64 1
%134 = load i32*, i32** %10
%135 = call i32 @fwrite(i8* %133, i32 1, i32 1, i32* %134)
%136 = load [3 x i8]*, [3 x i8]** %7
%137 = load i32, i32* %15
%138 = load i32, i32* %16
%139 = load i32, i32* %8
%140 = mul nsw i32 %138, %139
%141 = add nsw i32 %137, %140
%142 = sext i32 %141 to i64
%143 = getelementptr inbounds [3 x i8], [3 x i8]* %136, i64 %142
%144 = getelementptr inbounds [3 x i8], [3 x i8]* %143, i64 0, i64 0
%145 = load i32*, i32** %10
%146 = call i32 @fwrite(i8* %144, i32 1, i32 1, i32* %145)
br label %147
147: ; preds = %113
%148 = load i32, i32* %15
%149 = add nsw i32 %148, 1
store i32 %149, i32* %15
br label %109
150: ; preds = %109
%151 = load i32, i32* %15
%152 = srem i32 %151, 4
%153 = icmp ne i32 %152, 0
br i1 %153, label %154, label %160
154: ; preds = %150
%155 = getelementptr inbounds [40 x i8], [40 x i8]* %12, i64 0, i64 0
%156 = load i32, i32* %15
%157 = srem i32 %156, 4
%158 = load i32*, i32** %10
%159 = call i32 @fwrite(i8* %155, i32 1, i32 %157, i32* %158)
br label %160
160: ; preds = %154, %150
br label %161
161: ; preds = %160
%162 = load i32, i32* %16
%163 = add nsw i32 %162, -1
store i32 %163, i32* %16
br label %105
164: ; preds = %105
%165 = load i32*, i32** %10
%166 = call i32 @fclose(i32* %165)
store i32 0, i32* %5
br label %167
167: ; preds = %164, %21
%168 = load i32, i32* %5
ret i32 %168
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define i32 @WriteBMP24(i8* nocapture %0, [3 x i8]* nocapture %1, i32 %2, i32 %3) {
%5 = alloca [40 x i8]
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%8 = icmp eq i32* %6, null
br i1 %8, label %83, label %9
9: ; preds = %4
%10 = mul nsw i32 %2, 3
%11 = mul nsw i32 %10, %3
%12 = add nsw i32 %11, 54
%13 = call i32 @memset(i8* %7, i32 0, i32 14)
store i8 66, i8* %7
%14 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 1
store i8 77, i8* %14
%15 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 2
%16 = call i32 @bwrite_endian(i32 %12, i32 4, i8* %15, i32 1)
%17 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 6
%18 = call i32 @bwrite_endian(i32 0, i32 4, i8* %17, i32 1)
%19 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 10
%20 = call i32 @bwrite_endian(i32 54, i32 4, i8* %19, i32 1)
%21 = call i32 @fwrite(i8* %7, i32 1, i32 14, i32* %6)
%22 = call i32 @memset(i8* %7, i32 0, i32 40)
%23 = call i32 @bwrite_endian(i32 40, i32 4, i8* %7, i32 1)
%24 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 4
%25 = call i32 @bwrite_endian(i32 %2, i32 4, i8* %24, i32 1)
%26 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 8
%27 = call i32 @bwrite_endian(i32 %3, i32 4, i8* %26, i32 1)
%28 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 12
%29 = call i32 @bwrite_endian(i32 1, i32 2, i8* %28, i32 1)
%30 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 14
%31 = call i32 @bwrite_endian(i32 24, i32 2, i8* %30, i32 1)
%32 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 16
%33 = call i32 @bwrite_endian(i32 0, i32 4, i8* %32, i32 1)
%34 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 20
%35 = call i32 @bwrite_endian(i32 %11, i32 4, i8* %34, i32 1)
%36 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 24
%37 = call i32 @bwrite_endian(i32 65536, i32 4, i8* %36, i32 1)
%38 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 28
%39 = call i32 @bwrite_endian(i32 65536, i32 4, i8* %38, i32 1)
%40 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 32
%41 = call i32 @bwrite_endian(i32 16777216, i32 4, i8* %40, i32 1)
%42 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 36
%43 = call i32 @bwrite_endian(i32 0, i32 4, i8* %42, i32 1)
%44 = call i32 @fwrite(i8* %7, i32 1, i32 40, i32* %6)
%45 = call i32 @memset(i8* %7, i32 0, i32 4)
%46 = icmp sgt i32 %3, 0
br i1 %46, label %47, label %81
47: ; preds = %9
%48 = icmp sgt i32 %2, 0
%49 = zext i32 %3 to i64
%50 = sext i32 %2 to i64
%51 = zext i32 %2 to i64
%52 = and i32 %2, 3
%53 = icmp eq i32 %52, 0
br label %54
54: ; preds = %47, %79
%55 = phi i64 [ %49, %47 ], [ %56, %79 ]
%56 = add nsw i64 %55, -1
br i1 %48, label %57, label %79
57: ; preds = %54
%58 = mul nsw i64 %56, %50
br label %59
59: ; preds = %57, %59
%60 = phi i64 [ 0, %57 ], [ %74, %59 ]
%61 = add nsw i64 %60, %58
%62 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 %61, i64 2
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = call i32 @fputc(i32 %64, i32* %6)
%66 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 %61, i64 1
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = call i32 @fputc(i32 %68, i32* %6)
%70 = getelementptr inbounds [3 x i8], [3 x i8]* %1, i64 %61, i64 0
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = call i32 @fputc(i32 %72, i32* %6)
%74 = add nuw nsw i64 %60, 1
%75 = icmp eq i64 %74, %51
br i1 %75, label %76, label %59
76: ; preds = %59
br i1 %53, label %79, label %77
77: ; preds = %76
%78 = call i32 @fwrite(i8* %7, i32 1, i32 %52, i32* %6)
br label %79
79: ; preds = %54, %76, %77
%80 = icmp sgt i64 %55, 1
br i1 %80, label %54, label %81
81: ; preds = %79, %9
%82 = call i32 @fclose(i32* %6)
br label %83
83: ; preds = %4, %81
%84 = phi i32 [ 0, %81 ], [ -1, %4 ]
ret i32 %84
}