#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*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ write_le_int16(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ write_le_int32(int /*<<< orphan*/ *, int);
int writebmp(char *filename, int xs, int ys, unsigned char *img) {
  if (img != NULL) {
    FILE *dfile;
    if ((dfile = fopen(filename, "wb")) != NULL) {
      int i, y;
      int imgdataoffset = 14 + 40;
      int rowsz = ((xs * 3) + 3) & -4;
      int imgdatasize = rowsz * ys;
      int filesize = imgdataoffset + imgdatasize;
      unsigned char *rowbuf = NULL;
      fputc('B', dfile);
      fputc('M', dfile);
      write_le_int32(dfile, filesize);
      write_le_int16(dfile, 0);
      write_le_int16(dfile, 0);
      write_le_int32(dfile, imgdataoffset);
      write_le_int32(dfile, 40);
      write_le_int32(dfile, xs);
      write_le_int32(dfile, ys);
      write_le_int16(dfile, 1);
      write_le_int16(dfile, 24);
      write_le_int32(dfile, 0);
      write_le_int32(dfile, imgdatasize);
      write_le_int32(dfile, 11811);
      write_le_int32(dfile, 11811);
      write_le_int32(dfile, 0);
      write_le_int32(dfile, 0);
      rowbuf = (unsigned char *)malloc(rowsz);
      if (rowbuf != NULL) {
        memset(rowbuf, 0, rowsz);
        for (y = 0; y < ys; y++) {
          int addr = xs * 3 * y;
          for (i = 0; i < rowsz; i += 3) {
            rowbuf[i] = img[addr + i + 2];
            rowbuf[i + 1] = img[addr + i + 1];
            rowbuf[i + 2] = img[addr + i];
          }
          fwrite(rowbuf, rowsz, 1, dfile);
        }
        free(rowbuf);
      }
      fclose(dfile);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<writebmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     13d2 <writebmp+0x242>
mov    -0x8(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     13cd <writebmp+0x23d>
movl   $0x36,-0x2c(%rbp)
imul   $0x3,-0xc(%rbp),%eax
add    $0x3,%eax
and    $0xfffffffc,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rsi
mov    $0x42,%edi
call   1070 <fputc@plt>
mov    -0x20(%rbp),%rsi
mov    $0x4d,%edi
call   1070 <fputc@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1090 <write_le_int16@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1090 <write_le_int16@plt>
mov    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
mov    $0x28,%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1090 <write_le_int16@plt>
mov    -0x20(%rbp),%rdi
mov    $0x18,%esi
call   1090 <write_le_int16@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
mov    $0x2e23,%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
mov    $0x2e23,%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1050 <write_le_int32@plt>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
call   1050 <write_le_int32@plt>
mov    -0x30(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     13c4 <writebmp+0x234>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    13bb <writebmp+0x22b>
imul   $0x3,-0xc(%rbp),%eax
imul   -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1398 <writebmp+0x208>
mov    -0x18(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    -0x24(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x40(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x40(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    -0x24(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x40(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x24(%rbp)
jmp    131d <writebmp+0x18d>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12ff <writebmp+0x16f>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
jmp    13d2 <writebmp+0x242>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writebmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rcx,%rcx
je     136a <writebmp+0x1da>
mov    %rcx,%rbx
mov    %edx,%ebp
mov    %esi,%r12d
lea    0xe4a(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     136a <writebmp+0x1da>
mov    %rax,%r14
lea    (%r12,%r12,2),%r15d
add    $0x3,%r15d
and    $0xfffffffc,%r15d
mov    %r15d,%r13d
imul   %ebp,%r13d
mov    %ebp,0xc(%rsp)
lea    0x36(%r13),%ebp
mov    $0x42,%edi
mov    %rax,%rsi
call   1070 <fputc@plt>
mov    $0x4d,%edi
mov    %r14,%rsi
call   1070 <fputc@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <write_le_int16@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <write_le_int16@plt>
mov    %r14,%rdi
mov    $0x36,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
mov    $0x28,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
mov    %r12,(%rsp)
mov    %r12d,%esi
mov    0xc(%rsp),%r12d
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
mov    %r12d,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
mov    $0x1,%esi
call   1090 <write_le_int16@plt>
mov    %r14,%rdi
mov    $0x18,%esi
call   1090 <write_le_int16@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
mov    %r13d,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
mov    $0x2e23,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
mov    $0x2e23,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <write_le_int32@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <write_le_int32@plt>
mov    %r15d,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1362 <writebmp+0x1d2>
mov    %rax,%rbp
mov    %r14,0x10(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1060 <memset@plt>
test   %r12d,%r12d
jle    1355 <writebmp+0x1c5>
mov    (%rsp),%rax
lea    (%rax,%rax,2),%eax
movslq %r15d,%r13
cltq
mov    %rax,(%rsp)
mov    %r12d,%r12d
add    $0x2,%rbx
xor    %r14d,%r14d
jmp    1322 <writebmp+0x192>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    0x10(%rsp),%rcx
call   10b0 <fwrite@plt>
add    $0x1,%r14
add    (%rsp),%rbx
cmp    %r12,%r14
je     1355 <writebmp+0x1c5>
test   %r15d,%r15d
jle    1300 <writebmp+0x170>
xor    %eax,%eax
nopl   0x0(%rax)
movzbl (%rbx,%rax,1),%ecx
mov    %cl,0x0(%rbp,%rax,1)
movzbl -0x1(%rbx,%rax,1),%ecx
mov    %cl,0x1(%rbp,%rax,1)
movzbl -0x2(%rbx,%rax,1),%ecx
mov    %cl,0x2(%rbp,%rax,1)
add    $0x3,%rax
cmp    %r13,%rax
jl     1330 <writebmp+0x1a0>
jmp    1300 <writebmp+0x170>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%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 @writebmp(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%18 = load i8*, i8** %8
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %152
20: ; preds = %4
%21 = load i8*, i8** %5
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %9
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %151
24: ; preds = %20
store i32 54, i32* %12
%25 = load i32, i32* %6
%26 = mul nsw i32 %25, 3
%27 = add nsw i32 %26, 3
%28 = and i32 %27, -4
store i32 %28, i32* %13
%29 = load i32, i32* %13
%30 = load i32, i32* %7
%31 = mul nsw i32 %29, %30
store i32 %31, i32* %14
%32 = load i32, i32* %12
%33 = load i32, i32* %14
%34 = add nsw i32 %32, %33
store i32 %34, i32* %15
store i8* null, i8** %16
%35 = load i32*, i32** %9
%36 = call i32 @fputc(i8 signext 66, i32* %35)
%37 = load i32*, i32** %9
%38 = call i32 @fputc(i8 signext 77, i32* %37)
%39 = load i32*, i32** %9
%40 = load i32, i32* %15
%41 = call i32 @write_le_int32(i32* %39, i32 %40)
%42 = load i32*, i32** %9
%43 = call i32 @write_le_int16(i32* %42, i32 0)
%44 = load i32*, i32** %9
%45 = call i32 @write_le_int16(i32* %44, i32 0)
%46 = load i32*, i32** %9
%47 = load i32, i32* %12
%48 = call i32 @write_le_int32(i32* %46, i32 %47)
%49 = load i32*, i32** %9
%50 = call i32 @write_le_int32(i32* %49, i32 40)
%51 = load i32*, i32** %9
%52 = load i32, i32* %6
%53 = call i32 @write_le_int32(i32* %51, i32 %52)
%54 = load i32*, i32** %9
%55 = load i32, i32* %7
%56 = call i32 @write_le_int32(i32* %54, i32 %55)
%57 = load i32*, i32** %9
%58 = call i32 @write_le_int16(i32* %57, i32 1)
%59 = load i32*, i32** %9
%60 = call i32 @write_le_int16(i32* %59, i32 24)
%61 = load i32*, i32** %9
%62 = call i32 @write_le_int32(i32* %61, i32 0)
%63 = load i32*, i32** %9
%64 = load i32, i32* %14
%65 = call i32 @write_le_int32(i32* %63, i32 %64)
%66 = load i32*, i32** %9
%67 = call i32 @write_le_int32(i32* %66, i32 11811)
%68 = load i32*, i32** %9
%69 = call i32 @write_le_int32(i32* %68, i32 11811)
%70 = load i32*, i32** %9
%71 = call i32 @write_le_int32(i32* %70, i32 0)
%72 = load i32*, i32** %9
%73 = call i32 @write_le_int32(i32* %72, i32 0)
%74 = load i32, i32* %13
%75 = call i64 @malloc(i32 %74)
%76 = inttoptr i64 %75 to i8*
store i8* %76, i8** %16
%77 = load i8*, i8** %16
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %148
79: ; preds = %24
%80 = load i8*, i8** %16
%81 = load i32, i32* %13
%82 = call i32 @memset(i8* %80, i32 0, i32 %81)
store i32 0, i32* %11
br label %83
83: ; preds = %142, %79
%84 = load i32, i32* %11
%85 = load i32, i32* %7
%86 = icmp slt i32 %84, %85
br i1 %86, label %87, label %145
87: ; preds = %83
%88 = load i32, i32* %6
%89 = mul nsw i32 %88, 3
%90 = load i32, i32* %11
%91 = mul nsw i32 %89, %90
store i32 %91, i32* %17
store i32 0, i32* %10
br label %92
92: ; preds = %134, %87
%93 = load i32, i32* %10
%94 = load i32, i32* %13
%95 = icmp slt i32 %93, %94
br i1 %95, label %96, label %137
96: ; preds = %92
%97 = load i8*, i8** %8
%98 = load i32, i32* %17
%99 = load i32, i32* %10
%100 = add nsw i32 %98, %99
%101 = add nsw i32 %100, 2
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %97, i64 %102
%104 = load i8, i8* %103
%105 = load i8*, i8** %16
%106 = load i32, i32* %10
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %105, i64 %107
store i8 %104, i8* %108
%109 = load i8*, i8** %8
%110 = load i32, i32* %17
%111 = load i32, i32* %10
%112 = add nsw i32 %110, %111
%113 = add nsw i32 %112, 1
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds i8, i8* %109, i64 %114
%116 = load i8, i8* %115
%117 = load i8*, i8** %16
%118 = load i32, i32* %10
%119 = add nsw i32 %118, 1
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds i8, i8* %117, i64 %120
store i8 %116, i8* %121
%122 = load i8*, i8** %8
%123 = load i32, i32* %17
%124 = load i32, i32* %10
%125 = add nsw i32 %123, %124
%126 = sext i32 %125 to i64
%127 = getelementptr inbounds i8, i8* %122, i64 %126
%128 = load i8, i8* %127
%129 = load i8*, i8** %16
%130 = load i32, i32* %10
%131 = add nsw i32 %130, 2
%132 = sext i32 %131 to i64
%133 = getelementptr inbounds i8, i8* %129, i64 %132
store i8 %128, i8* %133
br label %134
134: ; preds = %96
%135 = load i32, i32* %10
%136 = add nsw i32 %135, 3
store i32 %136, i32* %10
br label %92
137: ; preds = %92
%138 = load i8*, i8** %16
%139 = load i32, i32* %13
%140 = load i32*, i32** %9
%141 = call i32 @fwrite(i8* %138, i32 %139, i32 1, i32* %140)
br label %142
142: ; preds = %137
%143 = load i32, i32* %11
%144 = add nsw i32 %143, 1
store i32 %144, i32* %11
br label %83
145: ; preds = %83
%146 = load i8*, i8** %16
%147 = call i32 @free(i8* %146)
br label %148
148: ; preds = %145, %24
%149 = load i32*, i32** %9
%150 = call i32 @fclose(i32* %149)
br label %151
151: ; preds = %148, %20
br label %152
152: ; preds = %151, %4
ret i32 0
}

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