#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*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
void bmp_write(char *dst, uint8_t *bmp, int H, int W) {
  int i;
  FILE *pf = fopen(dst, "wb");
  assert(pf);
  fwrite(&H, sizeof(int), 1, pf);
  fwrite(&W, sizeof(int), 1, pf);
  for (i = 0; i < H * W; i++)
    fwrite(bmp + i, sizeof(uint8_t), 1, pf);
  fclose(pf);
}


x86 assembly (clang, -O0):
<bmp_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    -0x8(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rcx
lea    -0x18(%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    11ea <bmp_write+0xaa>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a8 <bmp_write+0x68>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bmp_write>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %edx,0xc(%rsp)
mov    %ecx,0x8(%rsp)
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <assert@plt>
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
mov    0x8(%rsp),%eax
imul   0xc(%rsp),%eax
test   %eax,%eax
jle    11cf <bmp_write+0x8f>
mov    %eax,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
add    $0x4,%r15
add    $0xffffffffffffffff,%rbx
jne    11b0 <bmp_write+0x70>
mov    %r14,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define void @bmp_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*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32*, i32** %10
%14 = call i32 @assert(i32* %13)
%15 = load i32*, i32** %10
%16 = call i32 @fwrite(i32* %7, i32 4, i32 1, i32* %15)
%17 = load i32*, i32** %10
%18 = call i32 @fwrite(i32* %8, i32 4, i32 1, i32* %17)
store i32 0, i32* %9
br label %19
19: ; preds = %32, %4
%20 = load i32, i32* %9
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = mul nsw i32 %21, %22
%24 = icmp slt i32 %20, %23
br i1 %24, label %25, label %35
25: ; preds = %19
%26 = load i32*, i32** %6
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32*, i32** %10
%31 = call i32 @fwrite(i32* %29, i32 4, i32 1, i32* %30)
br label %32
32: ; preds = %25
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9
br label %19
35: ; preds = %19
%36 = load i32*, i32** %10
%37 = call i32 @fclose(i32* %36)
ret void
}

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