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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
scalar_t__ bit_count(unsigned long long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
unsigned long long *malloc(int);
uint64_t *bwi_read(char *file, int *H, int *W, int *count) {
  int i = 0, j, w, k;
  uint8_t c;
  uint64_t num, *bmp;
  FILE *pf = fopen(file, "rb");
  assert(pf);
  fread(H, sizeof(int), 1, pf);
  fread(&w, sizeof(int), 1, pf);
  *W = w / 64;
  bmp = malloc(sizeof(uint64_t) * (*H) * (*W));
  for (i = 0; i < (*H); i++) {
    fseek(pf, 8 + i * w, SEEK_SET);
    for (j = 0; j < (*W); j++) {
      k = 63;
      num = 0;
      while (k >= 0) {
        fread(&c, sizeof(uint8_t), 1, pf);
        if (c & 1)
          num |= 1ULL << k;
        k--;
      }
      (*count) += bit_count(num);
      bmp[i * (*W) + j] = num;
    }
  }
  fclose(pf);
  return bmp;
}


x86 assembly (clang, -O0):
<bwi_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x50(%rbp),%rcx
lea    -0x2c(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x2c(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    130d <bwi_read+0x19d>
mov    -0x50(%rbp),%rdi
mov    -0x24(%rbp),%esi
imul   -0x2c(%rbp),%esi
add    $0x8,%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%eax
jge    12fa <bwi_read+0x18a>
movl   $0x3f,-0x30(%rbp)
movq   $0x0,-0x40(%rbp)
cmpl   $0x0,-0x30(%rbp)
jl     12b8 <bwi_read+0x148>
mov    -0x50(%rbp),%rcx
lea    -0x34(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x34(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     12aa <bwi_read+0x13a>
mov    -0x30(%rbp),%eax
mov    %eax,%ecx
mov    $0x1,%eax
shl    %cl,%rax
or     -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
jmp    1265 <bwi_read+0xf5>
mov    -0x40(%rbp),%rdi
call   1060 <bit_count@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq (%rax),%rcx
add    %rdx,%rcx
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rsi
imul   (%rsi),%ecx
add    -0x28(%rbp),%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1247 <bwi_read+0xd7>
jmp    12ff <bwi_read+0x18f>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1215 <bwi_read+0xa5>
mov    -0x50(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bwi_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x30(%rsp)
mov    %rdx,%r14
mov    %rsi,%rbp
lea    0xe70(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <assert@plt>
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    0xc(%rsp),%eax
lea    0x3f(%rax),%edi
test   %eax,%eax
mov    %rax,0x20(%rsp)
cmovns %eax,%edi
sar    $0x6,%edi
mov    %r14,0x18(%rsp)
mov    %edi,(%r14)
mov    %rbp,0x28(%rsp)
mov    0x0(%rbp),%ebp
imul   %ebp,%edi
shl    $0x3,%edi
call   1070 <malloc@plt>
mov    %rax,0x10(%rsp)
test   %ebp,%ebp
jle    12e0 <bwi_read+0x170>
xor    %r12d,%r12d
lea    0x8(%rsp),%rbp
jmp    1232 <bwi_read+0xc2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%r12d
mov    0x28(%rsp),%rax
cmp    (%rax),%r12d
jge    12e0 <bwi_read+0x170>
mov    %r12d,%esi
imul   0x20(%rsp),%esi
add    $0x8,%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1080 <fseek@plt>
mov    0x18(%rsp),%rax
cmpl   $0x0,(%rax)
jle    1220 <bwi_read+0xb0>
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
mov    $0x3f,%r14d
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    $0x1,%eax
mov    %r14d,%ecx
shl    %cl,%rax
testb  $0x1,0x8(%rsp)
mov    $0x0,%ecx
cmovne %rax,%rcx
or     %rcx,%r15
add    $0xffffffffffffffff,%r14
jb     1270 <bwi_read+0x100>
mov    %r15,%rdi
call   1060 <bit_count@plt>
mov    0x30(%rsp),%rcx
add    %eax,(%rcx)
mov    0x18(%rsp),%rax
mov    (%rax),%eax
mov    %eax,%ecx
imul   %r12d,%ecx
add    %r13d,%ecx
movslq %ecx,%rcx
mov    0x10(%rsp),%rdx
mov    %r15,(%rdx,%rcx,8)
add    $0x1,%r13d
cmp    %eax,%r13d
jl     1260 <bwi_read+0xf0>
jmp    1220 <bwi_read+0xb0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_SET = global i32 0
define i64* @bwi_read(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
%14 = alloca i64
%15 = alloca i64*
%16 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 0, i32* %9
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %16
%19 = load i32*, i32** %16
%20 = call i32 @assert(i32* %19)
%21 = load i32*, i32** %6
%22 = load i32*, i32** %16
%23 = call i32 @fread(i32* %21, i32 4, i32 1, i32* %22)
%24 = load i32*, i32** %16
%25 = call i32 @fread(i32* %11, i32 4, i32 1, i32* %24)
%26 = load i32, i32* %11
%27 = sdiv i32 %26, 64
%28 = load i32*, i32** %7
store i32 %27, i32* %28
%29 = load i32*, i32** %6
%30 = load i32, i32* %29
%31 = sext i32 %30 to i64
%32 = mul i64 8, %31
%33 = load i32*, i32** %7
%34 = load i32, i32* %33
%35 = sext i32 %34 to i64
%36 = mul i64 %32, %35
%37 = trunc i64 %36 to i32
%38 = call i64* @malloc(i32 %37)
store i64* %38, i64** %15
store i32 0, i32* %9
br label %39
39: ; preds = %98, %4
%40 = load i32, i32* %9
%41 = load i32*, i32** %6
%42 = load i32, i32* %41
%43 = icmp slt i32 %40, %42
br i1 %43, label %44, label %101
44: ; preds = %39
%45 = load i32*, i32** %16
%46 = load i32, i32* %9
%47 = load i32, i32* %11
%48 = mul nsw i32 %46, %47
%49 = add nsw i32 8, %48
%50 = load i32, i32* @SEEK_SET
%51 = call i32 @fseek(i32* %45, i32 %49, i32 %50)
store i32 0, i32* %10
br label %52
52: ; preds = %94, %44
%53 = load i32, i32* %10
%54 = load i32*, i32** %7
%55 = load i32, i32* %54
%56 = icmp slt i32 %53, %55
br i1 %56, label %57, label %97
57: ; preds = %52
store i32 63, i32* %12
store i64 0, i64* %14
br label %58
58: ; preds = %73, %57
%59 = load i32, i32* %12
%60 = icmp sge i32 %59, 0
br i1 %60, label %61, label %76
61: ; preds = %58
%62 = load i32*, i32** %16
%63 = call i32 @fread(i32* %13, i32 4, i32 1, i32* %62)
%64 = load i32, i32* %13
%65 = and i32 %64, 1
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %61
%68 = load i32, i32* %12
%69 = zext i32 %68 to i64
%70 = shl i64 1, %69
%71 = load i64, i64* %14
%72 = or i64 %71, %70
store i64 %72, i64* %14
br label %73
73: ; preds = %67, %61
%74 = load i32, i32* %12
%75 = add nsw i32 %74, -1
store i32 %75, i32* %12
br label %58
76: ; preds = %58
%77 = load i64, i64* %14
%78 = call i64 @bit_count(i64 %77)
%79 = load i32*, i32** %8
%80 = load i32, i32* %79
%81 = sext i32 %80 to i64
%82 = add nsw i64 %81, %78
%83 = trunc i64 %82 to i32
store i32 %83, i32* %79
%84 = load i64, i64* %14
%85 = load i64*, i64** %15
%86 = load i32, i32* %9
%87 = load i32*, i32** %7
%88 = load i32, i32* %87
%89 = mul nsw i32 %86, %88
%90 = load i32, i32* %10
%91 = add nsw i32 %89, %90
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i64, i64* %85, i64 %92
store i64 %84, i64* %93
br label %94
94: ; preds = %76
%95 = load i32, i32* %10
%96 = add nsw i32 %95, 1
store i32 %96, i32* %10
br label %52
97: ; preds = %52
br label %98
98: ; preds = %97
%99 = load i32, i32* %9
%100 = add nsw i32 %99, 1
store i32 %100, i32* %9
br label %39
101: ; preds = %39
%102 = load i32*, i32** %16
%103 = call i32 @fclose(i32* %102)
%104 = load i64*, i64** %15
ret i64* %104
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_SET = global i32 0
define noalias i64* @bwi_read(i8* nocapture %0, i32* nocapture %1, i32* nocapture %2, i32* nocapture %3) {
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @assert(i32* %9)
%11 = tail call i32 @fread(i32* %1, i32 4, i32 1, i32* %9)
%12 = call i32 @fread(i32* %5, i32 4, i32 1, i32* %9)
%13 = load i32, i32* %5
%14 = sdiv i32 %13, 64
store i32 %14, i32* %2
%15 = load i32, i32* %1
%16 = shl nsw i32 %14, 3
%17 = mul i32 %16, %15
%18 = tail call i64* @malloc(i32 %17)
%19 = icmp sgt i32 %15, 0
br i1 %19, label %20, label %58
20: ; preds = %4, %54
%21 = phi i32 [ %55, %54 ], [ 0, %4 ]
%22 = mul nsw i32 %21, %13
%23 = add nsw i32 %22, 8
%24 = load i32, i32* @SEEK_SET
%25 = tail call i32 @fseek(i32* %9, i32 %23, i32 %24)
%26 = load i32, i32* %2
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %54
28: ; preds = %20, %42
%29 = phi i32 [ %52, %42 ], [ 0, %20 ]
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ 63, %28 ], [ %40, %30 ]
%32 = phi i64 [ 0, %28 ], [ %39, %30 ]
%33 = call i32 @fread(i32* %6, i32 4, i32 1, i32* %9)
%34 = load i32, i32* %6
%35 = and i32 %34, 1
%36 = icmp eq i32 %35, 0
%37 = shl nuw i64 1, %31
%38 = select i1 %36, i64 0, i64 %37
%39 = or i64 %38, %32
%40 = add nsw i64 %31, -1
%41 = icmp eq i64 %31, 0
br i1 %41, label %42, label %30
42: ; preds = %30
%43 = tail call i64 @bit_count(i64 %39)
%44 = load i32, i32* %3
%45 = trunc i64 %43 to i32
%46 = add i32 %44, %45
store i32 %46, i32* %3
%47 = load i32, i32* %2
%48 = mul nsw i32 %47, %21
%49 = add nsw i32 %48, %29
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i64, i64* %18, i64 %50
store i64 %39, i64* %51
%52 = add nuw nsw i32 %29, 1
%53 = icmp slt i32 %52, %47
br i1 %53, label %28, label %54
54: ; preds = %42, %20
%55 = add nuw nsw i32 %21, 1
%56 = load i32, i32* %1
%57 = icmp slt i32 %55, %56
br i1 %57, label %20, label %58
58: ; preds = %54, %4
%59 = tail call i32 @fclose(i32* %9)
ret i64* %18
}