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

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int *malloc(int);
uint8_t *bmp_read(char *src, int *H, int *W, int *count) {
  int i;
  uint8_t *bmp;
  FILE *pf = fopen(src, "rb");
  assert(pf);
  fread(H, sizeof(int), 1, pf);
  fread(W, sizeof(int), 1, pf);
  bmp = malloc(sizeof(uint8_t) * (*H) * (*W));
  for (i = 0; i < (*W) * (*H); i++) {
    fread(bmp + i, sizeof(uint8_t), 1, pf);
    if (bmp[i] == 1)
      (*count)++;
  }
  fclose(pf);
  return bmp;
}


x86 assembly (clang, -O0):
<bmp_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x2,%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
mov    -0x10(%rbp),%rdx
imul   (%rdx),%ecx
cmp    %ecx,%eax
jge    1245 <bmp_read+0xf5>
mov    -0x30(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x1,(%rax,%rcx,4)
jne    1232 <bmp_read+0xe2>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
jmp    1237 <bmp_read+0xe7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11db <bmp_read+0x8b>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmp_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <assert@plt>
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    (%r12),%ebp
imul   (%r15),%ebp
lea    0x0(,%rbp,4),%edi
call   1060 <malloc@plt>
mov    %rax,(%rsp)
test   %ebp,%ebp
jle    1209 <bmp_read+0xb9>
xor    %r13d,%r13d
mov    (%rsp),%rbp
jmp    11e8 <bmp_read+0x98>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r13
movslq (%r15),%rax
movslq (%r12),%rcx
imul   %rax,%rcx
add    $0x4,%rbp
cmp    %rcx,%r13
jge    1209 <bmp_read+0xb9>
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmpl   $0x1,0x0(%rbp)
jne    11d0 <bmp_read+0x80>
addl   $0x1,(%r14)
jmp    11d0 <bmp_read+0x80>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32* @bmp_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*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = call i32 @assert(i32* %14)
%16 = load i32*, i32** %6
%17 = load i32*, i32** %11
%18 = call i32 @fread(i32* %16, i32 4, i32 1, i32* %17)
%19 = load i32*, i32** %7
%20 = load i32*, i32** %11
%21 = call i32 @fread(i32* %19, i32 4, i32 1, i32* %20)
%22 = load i32*, i32** %6
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = mul i64 4, %24
%26 = load i32*, i32** %7
%27 = load i32, i32* %26
%28 = sext i32 %27 to i64
%29 = mul i64 %25, %28
%30 = trunc i64 %29 to i32
%31 = call i32* @malloc(i32 %30)
store i32* %31, i32** %10
store i32 0, i32* %9
br label %32
32: ; preds = %58, %4
%33 = load i32, i32* %9
%34 = load i32*, i32** %7
%35 = load i32, i32* %34
%36 = load i32*, i32** %6
%37 = load i32, i32* %36
%38 = mul nsw i32 %35, %37
%39 = icmp slt i32 %33, %38
br i1 %39, label %40, label %61
40: ; preds = %32
%41 = load i32*, i32** %10
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32*, i32** %11
%46 = call i32 @fread(i32* %44, i32 4, i32 1, i32* %45)
%47 = load i32*, i32** %10
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %57
53: ; preds = %40
%54 = load i32*, i32** %8
%55 = load i32, i32* %54
%56 = add nsw i32 %55, 1
store i32 %56, i32* %54
br label %57
57: ; preds = %53, %40
br label %58
58: ; preds = %57
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
br label %32
61: ; preds = %32
%62 = load i32*, i32** %11
%63 = call i32 @fclose(i32* %62)
%64 = load i32*, i32** %10
ret i32* %64
}

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