#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 */

/* Variables and functions */
int /*<<< orphan*/ assert(int);
size_t mat4_idx(int, int);
int /*<<< orphan*/ memset(float *, int /*<<< orphan*/, int);
double tan(double);
void perspective(float *mat4, double fovy, double aspect, double zNear,
                 double zFar) {
  double f = 1.0 / tan(fovy / 2.0);
  memset(mat4, 0, sizeof(*mat4) * 4 * 4);
  assert(aspect != 0.0 && zNear != zFar);
  mat4[mat4_idx(0, 0)] = f / aspect;
  mat4[mat4_idx(1, 1)] = f;
  mat4[mat4_idx(2, 2)] = (zFar + zNear) / (zNear - zFar);
  mat4[mat4_idx(2, 3)] = (2.0 * zFar * zNear) / (zNear - zFar);
  mat4[mat4_idx(3, 2)] = -1.0;
}


x86 assembly (clang, -O0):
<perspective>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  %xmm3,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe98(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1060 <tan@plt>
movaps %xmm0,%xmm1
movsd  0xe7c(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
movsd  -0x18(%rbp),%xmm0
xor    %eax,%eax
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
mov    %al,-0x31(%rbp)
jne    11bf <perspective+0x7f>
jp     11bf <perspective+0x7f>
jmp    11d4 <perspective+0x94>
movsd  -0x20(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
setne  %al
setp   %cl
or     %cl,%al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movsd  -0x30(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x7c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x78(%rbp)
xor    %esi,%esi
mov    %esi,%edi
call   1030 <mat4_idx@plt>
movss  -0x7c(%rbp),%xmm0
mov    %rax,%rcx
mov    -0x78(%rbp),%rax
movss  %xmm0,(%rax,%rcx,4)
movsd  -0x30(%rbp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x6c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    $0x1,%esi
mov    %esi,%edi
call   1030 <mat4_idx@plt>
movss  -0x6c(%rbp),%xmm0
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
movss  %xmm0,(%rax,%rcx,4)
movsd  -0x28(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
subsd  -0x28(%rbp),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x5c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x2,%esi
mov    %esi,%edi
call   1030 <mat4_idx@plt>
movss  -0x5c(%rbp),%xmm0
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
movss  %xmm0,(%rax,%rcx,4)
movsd  0xd71(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
subsd  -0x28(%rbp),%xmm1
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x4c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x2,%edi
mov    $0x3,%esi
call   1030 <mat4_idx@plt>
movss  -0x4c(%rbp),%xmm0
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
movss  %xmm0,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x3,%edi
mov    $0x2,%esi
call   1030 <mat4_idx@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
movss  0xd0a(%rip),%xmm0
movss  %xmm0,(%rax,%rcx,4)
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<perspective>:
push   %rbx
sub    $0x20,%rsp
movsd  %xmm3,0x8(%rsp)
movsd  %xmm2,0x18(%rsp)
movsd  %xmm1,(%rsp)
mov    %rdi,%rbx
mulsd  0xe9f(%rip),%xmm0
call   1060 <tan@plt>
movsd  0xe9a(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
xorpd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
setp   %al
setne  %cl
or     %al,%cl
movsd  0x18(%rsp),%xmm0
ucomisd 0x8(%rsp),%xmm0
setp   %al
setne  %dl
or     %al,%dl
and    %cl,%dl
movzbl %dl,%edi
call   1050 <assert@plt>
movsd  0x10(%rsp),%xmm0
divsd  (%rsp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,(%rsp)
xor    %edi,%edi
xor    %esi,%esi
call   1030 <mat4_idx@plt>
movss  (%rsp),%xmm0
movss  %xmm0,(%rbx,%rax,4)
movsd  0x10(%rsp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,(%rsp)
mov    $0x1,%edi
mov    $0x1,%esi
call   1030 <mat4_idx@plt>
movss  (%rsp),%xmm0
movss  %xmm0,(%rbx,%rax,4)
movsd  0x18(%rsp),%xmm1
movapd %xmm1,%xmm0
movsd  0x8(%rsp),%xmm2
addsd  %xmm2,%xmm0
subsd  %xmm2,%xmm1
movsd  %xmm1,(%rsp)
divsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0x10(%rsp)
mov    $0x2,%edi
mov    $0x2,%esi
call   1030 <mat4_idx@plt>
movss  0x10(%rsp),%xmm0
movss  %xmm0,(%rbx,%rax,4)
movsd  0x8(%rsp),%xmm0
addsd  %xmm0,%xmm0
mulsd  0x18(%rsp),%xmm0
divsd  (%rsp),%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,0x8(%rsp)
mov    $0x2,%edi
mov    $0x3,%esi
call   1030 <mat4_idx@plt>
movss  0x8(%rsp),%xmm0
movss  %xmm0,(%rbx,%rax,4)
mov    $0x3,%edi
mov    $0x2,%esi
call   1030 <mat4_idx@plt>
movl   $0xbf800000,(%rbx,%rax,4)
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define void @perspective(float* %0, double %1, double %2, double %3, double %4) {
%6 = alloca float*
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
store float* %0, float** %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
store double %4, double* %10
%12 = load double, double* %7
%13 = fdiv double %12, 2.000000e+00
%14 = call double @tan(double %13)
%15 = fdiv double 1.000000e+00, %14
store double %15, double* %11
%16 = load float*, float** %6
%17 = call i32 @memset(float* %16, i32 0, i32 64)
%18 = load double, double* %8
%19 = fcmp une double %18, 0.000000e+00
br i1 %19, label %20, label %24
20: ; preds = %5
%21 = load double, double* %9
%22 = load double, double* %10
%23 = fcmp une double %21, %22
br label %24
24: ; preds = %20, %5
%25 = phi i1 [ false, %5 ], [ %23, %20 ]
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load double, double* %11
%29 = load double, double* %8
%30 = fdiv double %28, %29
%31 = fptrunc double %30 to float
%32 = load float*, float** %6
%33 = call i64 @mat4_idx(i32 0, i32 0)
%34 = getelementptr inbounds float, float* %32, i64 %33
store float %31, float* %34
%35 = load double, double* %11
%36 = fptrunc double %35 to float
%37 = load float*, float** %6
%38 = call i64 @mat4_idx(i32 1, i32 1)
%39 = getelementptr inbounds float, float* %37, i64 %38
store float %36, float* %39
%40 = load double, double* %10
%41 = load double, double* %9
%42 = fadd double %40, %41
%43 = load double, double* %9
%44 = load double, double* %10
%45 = fsub double %43, %44
%46 = fdiv double %42, %45
%47 = fptrunc double %46 to float
%48 = load float*, float** %6
%49 = call i64 @mat4_idx(i32 2, i32 2)
%50 = getelementptr inbounds float, float* %48, i64 %49
store float %47, float* %50
%51 = load double, double* %10
%52 = fmul double 2.000000e+00, %51
%53 = load double, double* %9
%54 = fmul double %52, %53
%55 = load double, double* %9
%56 = load double, double* %10
%57 = fsub double %55, %56
%58 = fdiv double %54, %57
%59 = fptrunc double %58 to float
%60 = load float*, float** %6
%61 = call i64 @mat4_idx(i32 2, i32 3)
%62 = getelementptr inbounds float, float* %60, i64 %61
store float %59, float* %62
%63 = load float*, float** %6
%64 = call i64 @mat4_idx(i32 3, i32 2)
%65 = getelementptr inbounds float, float* %63, i64 %64
store float -1.000000e+00, float* %65
ret void
}

LLVM IR (-O2):
define void @perspective(float* %0, double %1, double %2, double %3, double %4) {
%6 = fmul double %1, 5.000000e-01
%7 = tail call double @tan(double %6)
%8 = fdiv double 1.000000e+00, %7
%9 = tail call i32 @memset(float* %0, i32 0, i32 64)
%10 = fcmp une double %2, 0.000000e+00
%11 = fcmp une double %3, %4
%12 = and i1 %10, %11
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = fdiv double %8, %2
%16 = fptrunc double %15 to float
%17 = tail call i64 @mat4_idx(i32 0, i32 0)
%18 = getelementptr inbounds float, float* %0, i64 %17
store float %16, float* %18
%19 = fptrunc double %8 to float
%20 = tail call i64 @mat4_idx(i32 1, i32 1)
%21 = getelementptr inbounds float, float* %0, i64 %20
store float %19, float* %21
%22 = fadd double %3, %4
%23 = fsub double %3, %4
%24 = fdiv double %22, %23
%25 = fptrunc double %24 to float
%26 = tail call i64 @mat4_idx(i32 2, i32 2)
%27 = getelementptr inbounds float, float* %0, i64 %26
store float %25, float* %27
%28 = fmul double %4, 2.000000e+00
%29 = fmul double %28, %3
%30 = fdiv double %29, %23
%31 = fptrunc double %30 to float
%32 = tail call i64 @mat4_idx(i32 2, i32 3)
%33 = getelementptr inbounds float, float* %0, i64 %32
store float %31, float* %33
%34 = tail call i64 @mat4_idx(i32 3, i32 2)
%35 = getelementptr inbounds float, float* %0, i64 %34
store float -1.000000e+00, float* %35
ret void
}