#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*/ fgetc(int /*<<< orphan*/ *);
float fmaxf(float, float);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(float *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
scalar_t__ malloc(int);
float *read_pfm(const char *filename, int *wd, int *ht) {
  FILE *f = fopen(filename, "rb");
  if (!f)
    return 0;
  fscanf(f, "PF\n%d %d\n%*[^\n]", wd, ht);
  fgetc(f);
  float *p = (float *)malloc(sizeof(float) * 3 * (*wd) * (*ht));
  fread(p, sizeof(float) * 3, (*wd) * (*ht), f);
  for (int k = 0; k < 3 * (*wd) * (*ht); k++)
    p[k] = fmaxf(0.0f, p[k]);
  fclose(f);
  return p;
}


x86 assembly (clang, -O0):
<read_pfm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a0 <read_pfm+0x40>
movq   $0x0,-0x8(%rbp)
jmp    1261 <read_pfm+0x101>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe50(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fgetc@plt>
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
imul   $0xc,%rax,%rax
mov    -0x20(%rbp),%rcx
movslq (%rcx),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rax
imul   (%rax),%edx
mov    -0x28(%rbp),%rcx
mov    $0xc,%esi
call   1030 <fread@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x18(%rbp),%rcx
imul   $0x3,(%rcx),%ecx
mov    -0x20(%rbp),%rdx
imul   (%rdx),%ecx
cmp    %ecx,%eax
jge    1250 <read_pfm+0xf0>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
xorps  %xmm1,%xmm1
maxss  %xmm1,%xmm0
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1208 <read_pfm+0xa8>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pfm>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ee <read_pfm+0x8e>
mov    %rax,%r14
lea    0xe7a(%rip),%rsi
mov    %rax,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
call   1050 <fscanf@plt>
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    (%r12),%eax
imul   (%r15),%eax
shl    $0x2,%eax
lea    (%rax,%rax,2),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    (%r15),%edx
imul   (%r12),%edx
mov    %rax,%rdi
mov    $0xc,%esi
mov    %r14,%rcx
call   1030 <fread@plt>
mov    (%r12),%eax
imul   (%r15),%eax
lea    (%rax,%rax,2),%ecx
test   %ecx,%ecx
jle    12d7 <read_pfm+0x177>
mov    %ecx,%eax
cmp    $0x8,%ecx
jae    11f5 <read_pfm+0x95>
xor    %ecx,%ecx
jmp    12bb <read_pfm+0x15b>
xor    %ebx,%ebx
jmp    12df <read_pfm+0x17f>
mov    %eax,%ecx
and    $0xfffffff8,%ecx
lea    -0x8(%rcx),%rdx
mov    %rdx,%rsi
shr    $0x3,%rsi
add    $0x1,%rsi
test   %rdx,%rdx
je     12ee <read_pfm+0x18e>
mov    %rsi,%rdi
and    $0xfffffffffffffffe,%rdi
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
movups (%rbx,%rdx,4),%xmm0
movups 0x10(%rbx,%rdx,4),%xmm1
movups 0x20(%rbx,%rdx,4),%xmm2
movups 0x30(%rbx,%rdx,4),%xmm3
xorps  %xmm4,%xmm4
maxps  %xmm0,%xmm4
cmpordps %xmm0,%xmm0
andps  %xmm4,%xmm0
xorps  %xmm4,%xmm4
maxps  %xmm1,%xmm4
cmpordps %xmm1,%xmm1
andps  %xmm4,%xmm1
movups %xmm0,(%rbx,%rdx,4)
movups %xmm1,0x10(%rbx,%rdx,4)
xorps  %xmm0,%xmm0
maxps  %xmm2,%xmm0
cmpordps %xmm2,%xmm2
andps  %xmm0,%xmm2
xorps  %xmm0,%xmm0
maxps  %xmm3,%xmm0
cmpordps %xmm3,%xmm3
andps  %xmm0,%xmm3
movups %xmm2,0x20(%rbx,%rdx,4)
movups %xmm3,0x30(%rbx,%rdx,4)
add    $0x10,%rdx
add    $0xfffffffffffffffe,%rdi
jne    1220 <read_pfm+0xc0>
test   $0x1,%sil
je     12b6 <read_pfm+0x156>
movups (%rbx,%rdx,4),%xmm0
movups 0x10(%rbx,%rdx,4),%xmm1
xorps  %xmm2,%xmm2
xorps  %xmm3,%xmm3
maxps  %xmm0,%xmm3
cmpordps %xmm0,%xmm0
andps  %xmm3,%xmm0
maxps  %xmm1,%xmm2
cmpordps %xmm1,%xmm1
andps  %xmm2,%xmm1
movups %xmm0,(%rbx,%rdx,4)
movups %xmm1,0x10(%rbx,%rdx,4)
cmp    %rax,%rcx
je     12d7 <read_pfm+0x177>
xorps  %xmm0,%xmm0
xchg   %ax,%ax
movss  (%rbx,%rcx,4),%xmm1
maxss  %xmm0,%xmm1
movss  %xmm1,(%rbx,%rcx,4)
add    $0x1,%rcx
cmp    %rcx,%rax
jne    12c0 <read_pfm+0x160>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %edx,%edx
test   $0x1,%sil
jne    128a <read_pfm+0x12a>
jmp    12b6 <read_pfm+0x156>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [16 x i8] c"PF\0A%d %d\0A%*[^\0A]\00"
define float* @read_pfm(i8* %0, i32* %1, i32* %2) {
%4 = alloca float*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca float*
%10 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%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** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store float* null, float** %4
br label %69
16: ; preds = %3
%17 = load i32*, i32** %8
%18 = load i32*, i32** %6
%19 = load i32*, i32** %7
%20 = call i32 @fscanf(i32* %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32* %18, i32* %19)
%21 = load i32*, i32** %8
%22 = call i32 @fgetc(i32* %21)
%23 = load i32*, i32** %6
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = mul i64 12, %25
%27 = load i32*, i32** %7
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = mul i64 %26, %29
%31 = trunc i64 %30 to i32
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to float*
store float* %33, float** %9
%34 = load float*, float** %9
%35 = load i32*, i32** %6
%36 = load i32, i32* %35
%37 = load i32*, i32** %7
%38 = load i32, i32* %37
%39 = mul nsw i32 %36, %38
%40 = load i32*, i32** %8
%41 = call i32 @fread(float* %34, i32 12, i32 %39, i32* %40)
store i32 0, i32* %10
br label %42
42: ; preds = %62, %16
%43 = load i32, i32* %10
%44 = load i32*, i32** %6
%45 = load i32, i32* %44
%46 = mul nsw i32 3, %45
%47 = load i32*, i32** %7
%48 = load i32, i32* %47
%49 = mul nsw i32 %46, %48
%50 = icmp slt i32 %43, %49
br i1 %50, label %51, label %65
51: ; preds = %42
%52 = load float*, float** %9
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds float, float* %52, i64 %54
%56 = load float, float* %55
%57 = call float @llvm.maxnum.f32(float 0.000000e+00, float %56)
%58 = load float*, float** %9
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds float, float* %58, i64 %60
store float %57, float* %61
br label %62
62: ; preds = %51
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %42
65: ; preds = %42
%66 = load i32*, i32** %8
%67 = call i32 @fclose(i32* %66)
%68 = load float*, float** %9
store float* %68, float** %4
br label %69
69: ; preds = %65, %15
%70 = load float*, float** %4
ret float* %70
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [16 x i8] c"PF\0A%d %d\0A%*[^\0A]\00"
define float* @read_pfm(i8* nocapture %0, i32* %1, i32* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %90, label %6
6: ; preds = %3
%7 = tail call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32* %1, i32* %2)
%8 = tail call i32 @fgetc(i32* %4)
%9 = load i32, i32* %1
%10 = mul i32 %9, 12
%11 = load i32, i32* %2
%12 = mul i32 %10, %11
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to float*
%15 = load i32, i32* %1
%16 = load i32, i32* %2
%17 = mul nsw i32 %16, %15
%18 = tail call i32 @fread(float* %14, i32 12, i32 %17, i32* %4)
%19 = load i32, i32* %1
%20 = mul nsw i32 %19, 3
%21 = load i32, i32* %2
%22 = mul i32 %20, %21
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %81
24: ; preds = %6
%25 = zext i32 %22 to i64
%26 = icmp ult i32 %22, 8
br i1 %26, label %79, label %27
27: ; preds = %24
%28 = and i64 %25, 4294967288
%29 = add nsw i64 %28, -8
%30 = lshr exact i64 %29, 3
%31 = add nuw nsw i64 %30, 1
%32 = and i64 %31, 1
%33 = icmp eq i64 %29, 0
br i1 %33, label %63, label %34
34: ; preds = %27
%35 = and i64 %31, 4611686018427387902
br label %36
36: ; preds = %36, %34
%37 = phi i64 [ 0, %34 ], [ %60, %36 ]
%38 = phi i64 [ 0, %34 ], [ %61, %36 ]
%39 = getelementptr inbounds float, float* %14, i64 %37
%40 = bitcast float* %39 to <4 x float>*
%41 = load <4 x float>, <4 x float>* %40
%42 = getelementptr inbounds float, float* %39, i64 4
%43 = bitcast float* %42 to <4 x float>*
%44 = load <4 x float>, <4 x float>* %43
%45 = call <4 x float> @llvm.maxnum.v4f32(<4 x float> %41, <4 x float> zeroinitializer)
%46 = call <4 x float> @llvm.maxnum.v4f32(<4 x float> %44, <4 x float> zeroinitializer)
%47 = bitcast float* %39 to <4 x float>*
store <4 x float> %45, <4 x float>* %47
%48 = bitcast float* %42 to <4 x float>*
store <4 x float> %46, <4 x float>* %48
%49 = or i64 %37, 8
%50 = getelementptr inbounds float, float* %14, i64 %49
%51 = bitcast float* %50 to <4 x float>*
%52 = load <4 x float>, <4 x float>* %51
%53 = getelementptr inbounds float, float* %50, i64 4
%54 = bitcast float* %53 to <4 x float>*
%55 = load <4 x float>, <4 x float>* %54
%56 = call <4 x float> @llvm.maxnum.v4f32(<4 x float> %52, <4 x float> zeroinitializer)
%57 = call <4 x float> @llvm.maxnum.v4f32(<4 x float> %55, <4 x float> zeroinitializer)
%58 = bitcast float* %50 to <4 x float>*
store <4 x float> %56, <4 x float>* %58
%59 = bitcast float* %53 to <4 x float>*
store <4 x float> %57, <4 x float>* %59
%60 = add nuw i64 %37, 16
%61 = add i64 %38, 2
%62 = icmp eq i64 %61, %35
br i1 %62, label %63, label %36
63: ; preds = %36, %27
%64 = phi i64 [ 0, %27 ], [ %60, %36 ]
%65 = icmp eq i64 %32, 0
br i1 %65, label %77, label %66
66: ; preds = %63
%67 = getelementptr inbounds float, float* %14, i64 %64
%68 = bitcast float* %67 to <4 x float>*
%69 = load <4 x float>, <4 x float>* %68
%70 = getelementptr inbounds float, float* %67, i64 4
%71 = bitcast float* %70 to <4 x float>*
%72 = load <4 x float>, <4 x float>* %71
%73 = call <4 x float> @llvm.maxnum.v4f32(<4 x float> %69, <4 x float> zeroinitializer)
%74 = call <4 x float> @llvm.maxnum.v4f32(<4 x float> %72, <4 x float> zeroinitializer)
%75 = bitcast float* %67 to <4 x float>*
store <4 x float> %73, <4 x float>* %75
%76 = bitcast float* %70 to <4 x float>*
store <4 x float> %74, <4 x float>* %76
br label %77
77: ; preds = %63, %66
%78 = icmp eq i64 %28, %25
br i1 %78, label %81, label %79
79: ; preds = %24, %77
%80 = phi i64 [ 0, %24 ], [ %28, %77 ]
br label %83
81: ; preds = %83, %77, %6
%82 = tail call i32 @fclose(i32* %4)
br label %90
83: ; preds = %79, %83
%84 = phi i64 [ %88, %83 ], [ %80, %79 ]
%85 = getelementptr inbounds float, float* %14, i64 %84
%86 = load float, float* %85
%87 = tail call float @llvm.maxnum.f32(float %86, float 0.000000e+00)
store float %87, float* %85
%88 = add nuw nsw i64 %84, 1
%89 = icmp eq i64 %88, %25
br i1 %89, label %81, label %83
90: ; preds = %3, %81
%91 = phi float* [ %14, %81 ], [ null, %3 ]
ret float* %91
}