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

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
void bwi_write(char *file, uint64_t *bmp, int H, int W) {
  int i, j, w = W * 64;
  uint8_t c;
  FILE *pf = fopen(file, "wb");
  assert(pf);
  fwrite(&H, sizeof(int), 1, pf);
  fwrite(&w, sizeof(int), 1, pf);
  for (i = 0; i < H * W; i++)
    for (j = 63; j >= 0; j--) {
      c = (bmp[i] >> j) & 1;
      fwrite(&c, sizeof(uint8_t), 1, pf);
    }
  fclose(pf);
}


x86 assembly (clang, -O0):
<bwi_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x6,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x30(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    -0x30(%rbp),%rcx
lea    -0x24(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
cmp    %ecx,%eax
jge    1222 <bwi_write+0xe2>
movl   $0x3f,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     120f <bwi_write+0xcf>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x20(%rbp),%ecx
sar    %cl,%eax
and    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    11ca <bwi_write+0x8a>
jmp    1214 <bwi_write+0xd4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b1 <bwi_write+0x71>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bwi_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rsi,%r14
mov    %edx,0xc(%rsp)
mov    %ecx,%eax
shl    $0x6,%eax
mov    %eax,0x14(%rsp)
lea    0xe99(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1040 <assert@plt>
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1060 <fwrite@plt>
lea    0x14(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1060 <fwrite@plt>
imul   0xc(%rsp),%ebp
test   %ebp,%ebp
jle    1202 <bwi_write+0xc2>
mov    %ebp,%r12d
xor    %ebx,%ebx
lea    0x10(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    $0x3f,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r14,%rbx,4),%eax
mov    %ebp,%ecx
shr    %cl,%eax
and    $0x1,%eax
mov    %eax,0x10(%rsp)
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1060 <fwrite@plt>
add    $0xffffffff,%ebp
jb     11d0 <bwi_write+0x90>
add    $0x1,%rbx
cmp    %r12,%rbx
jne    11c0 <bwi_write+0x80>
mov    %r13,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define void @bwi_write(i8* %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%14 = load i32, i32* %8
%15 = mul nsw i32 %14, 64
store i32 %15, i32* %11
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %13
%18 = load i32*, i32** %13
%19 = call i32 @assert(i32* %18)
%20 = load i32*, i32** %13
%21 = call i32 @fwrite(i32* %7, i32 4, i32 1, i32* %20)
%22 = load i32*, i32** %13
%23 = call i32 @fwrite(i32* %11, i32 4, i32 1, i32* %22)
store i32 0, i32* %9
br label %24
24: ; preds = %49, %4
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = mul nsw i32 %26, %27
%29 = icmp slt i32 %25, %28
br i1 %29, label %30, label %52
30: ; preds = %24
store i32 63, i32* %10
br label %31
31: ; preds = %45, %30
%32 = load i32, i32* %10
%33 = icmp sge i32 %32, 0
br i1 %33, label %34, label %48
34: ; preds = %31
%35 = load i32*, i32** %6
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %35, i64 %37
%39 = load i32, i32* %38
%40 = load i32, i32* %10
%41 = ashr i32 %39, %40
%42 = and i32 %41, 1
store i32 %42, i32* %12
%43 = load i32*, i32** %13
%44 = call i32 @fwrite(i32* %12, i32 4, i32 1, i32* %43)
br label %45
45: ; preds = %34
%46 = load i32, i32* %10
%47 = add nsw i32 %46, -1
store i32 %47, i32* %10
br label %31
48: ; preds = %31
br label %49
49: ; preds = %48
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %24
52: ; preds = %24
%53 = load i32*, i32** %13
%54 = call i32 @fclose(i32* %53)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define void @bwi_write(i8* nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %2, i32* %5
%8 = bitcast i32* %6 to i8*
%9 = shl nsw i32 %3, 6
store i32 %9, i32* %6
%10 = bitcast i32* %7 to i8*
%11 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @assert(i32* %11)
%13 = call i32 @fwrite(i32* %5, i32 4, i32 1, i32* %11)
%14 = call i32 @fwrite(i32* %6, i32 4, i32 1, i32* %11)
%15 = load i32, i32* %5
%16 = mul i32 %15, %3
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %4
%19 = zext i32 %16 to i64
br label %20
20: ; preds = %18, %31
%21 = phi i64 [ 0, %18 ], [ %32, %31 ]
%22 = getelementptr inbounds i32, i32* %1, i64 %21
br label %23
23: ; preds = %20, %23
%24 = phi i32 [ 63, %20 ], [ %29, %23 ]
%25 = load i32, i32* %22
%26 = lshr i32 %25, %24
%27 = and i32 %26, 1
store i32 %27, i32* %7
%28 = call i32 @fwrite(i32* %7, i32 4, i32 1, i32* %11)
%29 = add nsw i32 %24, -1
%30 = icmp eq i32 %24, 0
br i1 %30, label %31, label %23
31: ; preds = %23
%32 = add nuw nsw i64 %21, 1
%33 = icmp eq i64 %32, %19
br i1 %33, label %34, label %20
34: ; preds = %31, %4
%35 = tail call i32 @fclose(i32* %11)
ret void
}