#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stbiw__write_hdr_scanline(int /*<<< orphan*/ *, int, int,
                                             unsigned char *, float const *);
int stbi_write_hdr(char const *filename, int x, int y, int comp,
                   const float *data) {
  int i;
  FILE *f;
  if (y <= 0 || x <= 0 || data == NULL)
    return 0;
  f = fopen(filename, "wb");
  if (f) {
    unsigned char *scratch = (unsigned char *)malloc(x * 4);
    fprintf(
        f,
        "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n");
    fprintf(f, "EXPOSURE=          1.0000000000000\n\n-Y %d +X %d\n", y, x);
    for (i = 0; i < y; i++)
      stbiw__write_hdr_scanline(f, x, comp, scratch, data + comp * i * x);
    free(scratch);
    fclose(f);
  }
  return f != NULL;
}


x86 assembly (clang, -O0):
<stbi_write_hdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1198 <stbi_write_hdr+0x38>
cmpl   $0x0,-0x14(%rbp)
jle    1198 <stbi_write_hdr+0x38>
cmpq   $0x0,-0x28(%rbp)
jne    11a4 <stbi_write_hdr+0x44>
movl   $0x0,-0x4(%rbp)
jmp    126a <stbi_write_hdr+0x10a>
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     125a <stbi_write_hdr+0xfa>
mov    -0x14(%rbp),%edi
shl    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1248 <stbi_write_hdr+0xe8>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x1c(%rbp),%eax
imul   -0x2c(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%r8
call   1050 <stbiw__write_hdr_scanline@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1203 <stbi_write_hdr+0xa3>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x38(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stbi_write_hdr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
test   %esi,%esi
jle    1269 <stbi_write_hdr+0xf9>
mov    %edx,%ebp
test   %edx,%edx
jle    1269 <stbi_write_hdr+0xf9>
mov    %r8,%r14
test   %r8,%r8
je     1269 <stbi_write_hdr+0xf9>
mov    %ecx,%r15d
mov    %esi,%ebx
lea    0xe56(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     125e <stbi_write_hdr+0xee>
lea    0x0(,%rbx,4),%edi
call   1070 <malloc@plt>
mov    %rax,0x10(%rsp)
lea    0xe2e(%rip),%rdi
mov    $0x41,%esi
mov    $0x1,%edx
mov    0x8(%rsp),%r13
mov    %r13,%rcx
call   1090 <fwrite@plt>
lea    0xe52(%rip),%rsi
xor    %r12d,%r12d
mov    %r13,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
mov    %r14,%r13
mov    %r15d,%r14d
imul   %ebx,%r14d
mov    %rbx,%rbp
mov    %r15d,%ebx
mov    %eax,%r15d
nopl   0x0(%rax)
movslq %r12d,%r12
lea    0x0(,%r12,4),%r8
add    %r13,%r8
mov    0x8(%rsp),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    0x10(%rsp),%rcx
call   1050 <stbiw__write_hdr_scanline@plt>
add    %r14d,%r12d
add    $0xffffffffffffffff,%r15
jne    1220 <stbi_write_hdr+0xb0>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
cmpq   $0x0,0x8(%rsp)
setne  %al
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [66 x i8] c"#?RADIANCE\0A# Written by stb_image_write.h\0AFORMAT=32-bit_rle_rgbe\0A\00"
@.str.2 = [49 x i8] c"EXPOSURE= 1.0000000000000\0A\0A-Y %d +X %d\0A\00"
define i32 @stbi_write_hdr(i8* %0, i32 %1, i32 %2, i32 %3, float* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca float*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i8*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store float* %4, float** %11
%15 = load i32, i32* %9
%16 = icmp sle i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %5
%18 = load i32, i32* %8
%19 = icmp sle i32 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load float*, float** %11
%22 = icmp eq float* %21, null
br i1 %22, label %23, label %24
23: ; preds = %20, %17, %5
store i32 0, i32* %6
br label %70
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %26, i32** %13
%27 = load i32*, i32** %13
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %66
29: ; preds = %24
%30 = load i32, i32* %8
%31 = mul nsw i32 %30, 4
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %14
%34 = load i32*, i32** %13
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32*, i32** %13
%37 = load i32, i32* %9
%38 = load i32, i32* %8
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %36, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %38)
store i32 0, i32* %12
br label %40
40: ; preds = %58, %29
%41 = load i32, i32* %12
%42 = load i32, i32* %9
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %61
44: ; preds = %40
%45 = load i32*, i32** %13
%46 = load i32, i32* %8
%47 = load i32, i32* %10
%48 = load i8*, i8** %14
%49 = load float*, float** %11
%50 = load i32, i32* %10
%51 = load i32, i32* %12
%52 = mul nsw i32 %50, %51
%53 = load i32, i32* %8
%54 = mul nsw i32 %52, %53
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds float, float* %49, i64 %55
%57 = call i32 @stbiw__write_hdr_scanline(i32* %45, i32 %46, i32 %47, i8* %48, float* %56)
br label %58
58: ; preds = %44
%59 = load i32, i32* %12
%60 = add nsw i32 %59, 1
store i32 %60, i32* %12
br label %40
61: ; preds = %40
%62 = load i8*, i8** %14
%63 = call i32 @free(i8* %62)
%64 = load i32*, i32** %13
%65 = call i32 @fclose(i32* %64)
br label %66
66: ; preds = %61, %24
%67 = load i32*, i32** %13
%68 = icmp ne i32* %67, null
%69 = zext i1 %68 to i32
store i32 %69, i32* %6
br label %70
70: ; preds = %66, %23
%71 = load i32, i32* %6
ret i32 %71
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [66 x i8] c"#?RADIANCE\0A# Written by stb_image_write.h\0AFORMAT=32-bit_rle_rgbe\0A\00"
@.str.2 = [49 x i8] c"EXPOSURE= 1.0000000000000\0A\0A-Y %d +X %d\0A\00"
define i32 @stbi_write_hdr(i8* nocapture %0, i32 %1, i32 %2, i32 %3, float* %4) {
%6 = icmp slt i32 %2, 1
%7 = icmp slt i32 %1, 1
%8 = or i1 %7, %6
%9 = icmp eq float* %4, null
%10 = or i1 %8, %9
br i1 %10, label %36, label %11
11: ; preds = %5
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %34
14: ; preds = %11
%15 = shl nsw i32 %1, 2
%16 = tail call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i64 @fwrite(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0), i64 65, i64 1, i32* %12)
%19 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %2, i32 %1)
%20 = mul i32 %3, %1
%21 = zext i32 %2 to i64
br label %22
22: ; preds = %14, %22
%23 = phi i64 [ 0, %14 ], [ %29, %22 ]
%24 = trunc i64 %23 to i32
%25 = mul i32 %20, %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds float, float* %4, i64 %26
%28 = tail call i32 @stbiw__write_hdr_scanline(i32* %12, i32 %1, i32 %3, i8* %17, float* %27)
%29 = add nuw nsw i64 %23, 1
%30 = icmp eq i64 %29, %21
br i1 %30, label %31, label %22
31: ; preds = %22
%32 = tail call i32 @free(i8* %17)
%33 = tail call i32 @fclose(i32* %12)
br label %34
34: ; preds = %31, %11
%35 = zext i1 %13 to i32
br label %36
36: ; preds = %5, %34
%37 = phi i32 [ %35, %34 ], [ 0, %5 ]
ret i32 %37
}