#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*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ write_le_dword(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ write_le_word(int /*<<< orphan*/ *, int);
int write_bitmap_headers(FILE *f, int w, int h, int bpp) {
  int x;
  int r;
  int ret;
  if (bpp == 16)
    bpp = 24;
  fputc('B', f);
  fputc('M', f);
  if (bpp == 24)
    r = w * 3;
  else if (bpp == 8)
    r = w;
  else if (bpp == 4)
    r = w / 2;
  ret = r;
  if (r & 3) {
    r |= 3;
    r++;
  }
  ret = r - ret;
  x = r * h;
  x += 54;
  if (bpp == 8)
    x += 1024;
  else if (bpp == 4)
    x += 64;
  write_le_dword(f, x);
  write_le_dword(f, 0);
  x = 54;
  if (bpp == 8)
    x += 1024;
  else if (bpp == 4)
    x += 64;
  write_le_dword(f, x);
  write_le_dword(f, 40);
  write_le_dword(f, w);
  write_le_dword(f, h);
  write_le_word(f, 1);
  write_le_word(f, bpp);
  write_le_dword(f, 0);
  write_le_dword(f, r * h);
  write_le_dword(f, 0);
  write_le_dword(f, 0);
  write_le_dword(f, 0);
  write_le_dword(f, 0);
  return ret;
}


x86 assembly (clang, -O0):
<write_bitmap_headers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jne    1156 <write_bitmap_headers+0x26>
movl   $0x18,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
mov    $0x42,%edi
call   1040 <fputc@plt>
mov    -0x8(%rbp),%rsi
mov    $0x4d,%edi
call   1040 <fputc@plt>
cmpl   $0x18,-0x14(%rbp)
jne    1188 <write_bitmap_headers+0x58>
imul   $0x3,-0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11bf <write_bitmap_headers+0x8f>
cmpl   $0x8,-0x14(%rbp)
jne    119d <write_bitmap_headers+0x6d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11ba <write_bitmap_headers+0x8a>
cmpl   $0x4,-0x14(%rbp)
jne    11b5 <write_bitmap_headers+0x85>
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
jmp    11ba <write_bitmap_headers+0x8a>
jmp    11bf <write_bitmap_headers+0x8f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x3,%eax
cmp    $0x0,%eax
je     11e6 <write_bitmap_headers+0xb6>
mov    -0x1c(%rbp),%eax
or     $0x3,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x36,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x8,-0x14(%rbp)
jne    121c <write_bitmap_headers+0xec>
mov    -0x18(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x18(%rbp)
jmp    1234 <write_bitmap_headers+0x104>
cmpl   $0x4,-0x14(%rbp)
jne    122f <write_bitmap_headers+0xff>
mov    -0x18(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0x18(%rbp)
jmp    1234 <write_bitmap_headers+0x104>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
movl   $0x36,-0x18(%rbp)
cmpl   $0x8,-0x14(%rbp)
jne    126c <write_bitmap_headers+0x13c>
mov    -0x18(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x18(%rbp)
jmp    1284 <write_bitmap_headers+0x154>
cmpl   $0x4,-0x14(%rbp)
jne    127f <write_bitmap_headers+0x14f>
mov    -0x18(%rbp),%eax
add    $0x40,%eax
mov    %eax,-0x18(%rbp)
jmp    1284 <write_bitmap_headers+0x154>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
mov    $0x28,%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1050 <write_le_word@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <write_le_word@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
imul   -0x10(%rbp),%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bitmap_headers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %esi,%r12d
mov    %rdi,%rbx
cmp    $0x10,%ecx
mov    $0x18,%r14d
cmovne %ecx,%r14d
mov    $0x42,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    $0x4d,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    %r12d,%ebp
cmp    $0x4,%r14d
je     1183 <write_bitmap_headers+0x53>
cmp    $0x18,%r14d
jne    118b <write_bitmap_headers+0x5b>
lea    (%r12,%r12,2),%ebp
jmp    118b <write_bitmap_headers+0x5b>
shr    $0x1f,%ebp
add    %r12d,%ebp
sar    %ebp
mov    %r12,0x10(%rsp)
mov    %ebp,%r12d
or     $0x3,%r12d
add    $0x1,%r12d
test   $0x3,%bpl
cmove  %ebp,%r12d
mov    %r12d,%r13d
imul   %r15d,%r13d
xor    %eax,%eax
cmp    $0x4,%r14d
sete   %al
shl    $0x6,%eax
or     $0x36,%eax
cmp    $0x8,%r14d
mov    %r15d,0xc(%rsp)
mov    $0x436,%r15d
cmovne %eax,%r15d
lea    (%r15,%r13,1),%esi
mov    %rbx,%rdi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
sub    %ebp,%r12d
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
mov    $0x28,%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
mov    0xc(%rsp),%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <write_le_word@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <write_le_word@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <write_le_dword@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_bitmap_headers(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, 16
br i1 %13, label %14, label %15
14: ; preds = %4
store i32 24, i32* %8
br label %15
15: ; preds = %14, %4
%16 = load i32*, i32** %5
%17 = call i32 @fputc(i8 signext 66, i32* %16)
%18 = load i32*, i32** %5
%19 = call i32 @fputc(i8 signext 77, i32* %18)
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 24
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %6
%24 = mul nsw i32 %23, 3
store i32 %24, i32* %10
br label %38
25: ; preds = %15
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 8
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = load i32, i32* %6
store i32 %29, i32* %10
br label %37
30: ; preds = %25
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 4
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %6
%35 = sdiv i32 %34, 2
store i32 %35, i32* %10
br label %36
36: ; preds = %33, %30
br label %37
37: ; preds = %36, %28
br label %38
38: ; preds = %37, %22
%39 = load i32, i32* %10
store i32 %39, i32* %11
%40 = load i32, i32* %10
%41 = and i32 %40, 3
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load i32, i32* %10
%45 = or i32 %44, 3
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
store i32 %47, i32* %10
br label %48
48: ; preds = %43, %38
%49 = load i32, i32* %10
%50 = load i32, i32* %11
%51 = sub nsw i32 %49, %50
store i32 %51, i32* %11
%52 = load i32, i32* %10
%53 = load i32, i32* %7
%54 = mul nsw i32 %52, %53
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = add nsw i32 %55, 54
store i32 %56, i32* %9
%57 = load i32, i32* %8
%58 = icmp eq i32 %57, 8
br i1 %58, label %59, label %62
59: ; preds = %48
%60 = load i32, i32* %9
%61 = add nsw i32 %60, 1024
store i32 %61, i32* %9
br label %69
62: ; preds = %48
%63 = load i32, i32* %8
%64 = icmp eq i32 %63, 4
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32, i32* %9
%67 = add nsw i32 %66, 64
store i32 %67, i32* %9
br label %68
68: ; preds = %65, %62
br label %69
69: ; preds = %68, %59
%70 = load i32*, i32** %5
%71 = load i32, i32* %9
%72 = call i32 @write_le_dword(i32* %70, i32 %71)
%73 = load i32*, i32** %5
%74 = call i32 @write_le_dword(i32* %73, i32 0)
store i32 54, i32* %9
%75 = load i32, i32* %8
%76 = icmp eq i32 %75, 8
br i1 %76, label %77, label %80
77: ; preds = %69
%78 = load i32, i32* %9
%79 = add nsw i32 %78, 1024
store i32 %79, i32* %9
br label %87
80: ; preds = %69
%81 = load i32, i32* %8
%82 = icmp eq i32 %81, 4
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i32, i32* %9
%85 = add nsw i32 %84, 64
store i32 %85, i32* %9
br label %86
86: ; preds = %83, %80
br label %87
87: ; preds = %86, %77
%88 = load i32*, i32** %5
%89 = load i32, i32* %9
%90 = call i32 @write_le_dword(i32* %88, i32 %89)
%91 = load i32*, i32** %5
%92 = call i32 @write_le_dword(i32* %91, i32 40)
%93 = load i32*, i32** %5
%94 = load i32, i32* %6
%95 = call i32 @write_le_dword(i32* %93, i32 %94)
%96 = load i32*, i32** %5
%97 = load i32, i32* %7
%98 = call i32 @write_le_dword(i32* %96, i32 %97)
%99 = load i32*, i32** %5
%100 = call i32 @write_le_word(i32* %99, i32 1)
%101 = load i32*, i32** %5
%102 = load i32, i32* %8
%103 = call i32 @write_le_word(i32* %101, i32 %102)
%104 = load i32*, i32** %5
%105 = call i32 @write_le_dword(i32* %104, i32 0)
%106 = load i32*, i32** %5
%107 = load i32, i32* %10
%108 = load i32, i32* %7
%109 = mul nsw i32 %107, %108
%110 = call i32 @write_le_dword(i32* %106, i32 %109)
%111 = load i32*, i32** %5
%112 = call i32 @write_le_dword(i32* %111, i32 0)
%113 = load i32*, i32** %5
%114 = call i32 @write_le_dword(i32* %113, i32 0)
%115 = load i32*, i32** %5
%116 = call i32 @write_le_dword(i32* %115, i32 0)
%117 = load i32*, i32** %5
%118 = call i32 @write_le_dword(i32* %117, i32 0)
%119 = load i32, i32* %11
ret i32 %119
}

LLVM IR (-O2):
define i32 @write_bitmap_headers(i32* %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 16
%6 = select i1 %5, i32 24, i32 %3
%7 = tail call i32 @fputc(i8 signext 66, i32* %0)
%8 = tail call i32 @fputc(i8 signext 77, i32* %0)
switch i32 %6, label %13 [
i32 24, label %9
i32 4, label %11
]
9: ; preds = %4
%10 = mul nsw i32 %1, 3
br label %13
11: ; preds = %4
%12 = sdiv i32 %1, 2
br label %13
13: ; preds = %4, %11, %9
%14 = phi i32 [ %10, %9 ], [ %12, %11 ], [ %1, %4 ]
%15 = and i32 %14, 3
%16 = icmp eq i32 %15, 0
%17 = or i32 %14, 3
%18 = add nsw i32 %17, 1
%19 = select i1 %16, i32 %14, i32 %18
%20 = mul nsw i32 %19, %2
%21 = icmp eq i32 %6, 8
%22 = icmp eq i32 %6, 4
%23 = select i1 %22, i32 118, i32 54
%24 = select i1 %21, i32 1078, i32 %23
%25 = add nsw i32 %20, %24
%26 = tail call i32 @write_le_dword(i32* %0, i32 %25)
%27 = tail call i32 @write_le_dword(i32* %0, i32 0)
%28 = sub nsw i32 %19, %14
%29 = tail call i32 @write_le_dword(i32* %0, i32 %24)
%30 = tail call i32 @write_le_dword(i32* %0, i32 40)
%31 = tail call i32 @write_le_dword(i32* %0, i32 %1)
%32 = tail call i32 @write_le_dword(i32* %0, i32 %2)
%33 = tail call i32 @write_le_word(i32* %0, i32 1)
%34 = tail call i32 @write_le_word(i32* %0, i32 %6)
%35 = tail call i32 @write_le_dword(i32* %0, i32 0)
%36 = tail call i32 @write_le_dword(i32* %0, i32 %20)
%37 = tail call i32 @write_le_dword(i32* %0, i32 0)
%38 = tail call i32 @write_le_dword(i32* %0, i32 0)
%39 = tail call i32 @write_le_dword(i32* %0, i32 0)
%40 = tail call i32 @write_le_dword(i32* %0, i32 0)
ret i32 %28
}