#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 GLint;
typedef double GLdouble;

/* Variables and functions */
int GLUT_DOWN;
#define GLUT_LEFT_BUTTON 129
#define GLUT_RIGHT_BUTTON 128
int /*<<< orphan*/ GL_MODELVIEW_MATRIX;
int /*<<< orphan*/ GL_PROJECTION_MATRIX;
int /*<<< orphan*/ GL_VIEWPORT;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ glGetDoublev(int /*<<< orphan*/, double *);
int /*<<< orphan*/ glGetIntegerv(int /*<<< orphan*/, int *);
int /*<<< orphan*/ gluUnProject(double, double, double, double *, double *,
                                int *, double *, double *, double *);
int /*<<< orphan*/ printf(char *, double, double, ...);
void unproject_mouse(int button, int state, int x, int y) {
  GLint viewport[4];
  GLdouble mvmatrix[16];
  GLdouble projmatrix[16];
  GLint realy;
  GLdouble wx, wy, wz;
  switch (button) {
  case GLUT_LEFT_BUTTON:
    if (state == GLUT_DOWN) {
      glGetIntegerv(GL_VIEWPORT, viewport);
      glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
      glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
      realy = viewport[3] - (GLint)y - 1;
      printf("Coordinates at cursor are (%4d, %4d)\n", x, realy);
      gluUnProject((GLdouble)x, (GLdouble)realy, 0.0, mvmatrix, projmatrix,
                   viewport, &wx, &wy, &wz);
      printf("World coords at z = 0.0 are (%f, %f, %f)\n", wx, wy, wz);
      gluUnProject((GLdouble)x, (GLdouble)realy, 1.0, mvmatrix, projmatrix,
                   viewport, &wx, &wy, &wz);
      printf("World coords at z = 1.0 are (%f, %f, %f)\n", wx, wy, wz);
    }
    break;
  case GLUT_RIGHT_BUTTON:
    if (state == GLUT_DOWN) {
      exit(0);
    }
    break;
  default:
    break;
  }
}


x86 assembly (clang, -O0):
<unproject_mouse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x144(%rbp)
sub    $0x80,%eax
je     12dc <unproject_mouse+0x18c>
jmp    1180 <unproject_mouse+0x30>
mov    -0x144(%rbp),%eax
sub    $0x81,%eax
jne    12fa <unproject_mouse+0x1aa>
jmp    1196 <unproject_mouse+0x46>
mov    -0x8(%rbp),%eax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%eax
jne    12d7 <unproject_mouse+0x187>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1070 <glGetIntegerv@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rsi
call   1040 <glGetDoublev@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rsi
call   1040 <glGetDoublev@plt>
mov    -0x14(%rbp),%eax
sub    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x124(%rbp)
cvtsi2sdl -0xc(%rbp),%xmm0
cvtsi2sdl -0x124(%rbp),%xmm1
ff
lea    0xe01(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
cvtsi2sdl -0xc(%rbp),%xmm0
cvtsi2sdl -0x124(%rbp),%xmm1
ff
lea    -0xa0(%rbp),%rdi
lea    -0x120(%rbp),%rsi
lea    -0x20(%rbp),%rdx
xorps  %xmm2,%xmm2
lea    -0x130(%rbp),%rcx
lea    -0x138(%rbp),%r8
lea    -0x140(%rbp),%r9
call   1060 <gluUnProject@plt>
movsd  -0x130(%rbp),%xmm0
ff
movsd  -0x138(%rbp),%xmm1
ff
movsd  -0x140(%rbp),%xmm2
ff
lea    0xdc5(%rip),%rdi
mov    $0x3,%al
call   1030 <printf@plt>
cvtsi2sdl -0xc(%rbp),%xmm0
cvtsi2sdl -0x124(%rbp),%xmm1
ff
lea    -0xa0(%rbp),%rdi
lea    -0x120(%rbp),%rsi
lea    -0x20(%rbp),%rdx
movsd  0xd69(%rip),%xmm2
lea    -0x130(%rbp),%rcx
lea    -0x138(%rbp),%r8
lea    -0x140(%rbp),%r9
call   1060 <gluUnProject@plt>
movsd  -0x130(%rbp),%xmm0
ff
movsd  -0x138(%rbp),%xmm1
ff
movsd  -0x140(%rbp),%xmm2
ff
lea    0xd88(%rip),%rdi
mov    $0x3,%al
call   1030 <printf@plt>
jmp    12ff <unproject_mouse+0x1af>
mov    -0x8(%rbp),%eax
mov    0x2cf2(%rip),%rcx
cmp    (%rcx),%eax
jne    12f5 <unproject_mouse+0x1a5>
xor    %edi,%edi
call   1050 <exit@plt>
jmp    12ff <unproject_mouse+0x1af>
jmp    12ff <unproject_mouse+0x1af>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unproject_mouse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x148,%rsp
cmp    $0x80,%edi
je     129b <unproject_mouse+0x14b>
cmp    $0x81,%edi
jne    12a6 <unproject_mouse+0x156>
mov    0x2e58(%rip),%rax
cmp    %esi,(%rax)
jne    12a6 <unproject_mouse+0x156>
mov    %ecx,%ebp
mov    %edx,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0x30(%rsp),%r14
mov    %r14,%rsi
call   1070 <glGetIntegerv@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xc0(%rsp),%r15
mov    %r15,%rsi
call   1040 <glGetDoublev@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0x40(%rsp),%r12
mov    %r12,%rsi
call   1040 <glGetDoublev@plt>
not    %ebp
add    0x3c(%rsp),%ebp
cvtsi2sd %ebx,%xmm0
movsd  %xmm0,0x10(%rsp)
cvtsi2sd %ebp,%xmm1
movsd  %xmm1,0x8(%rsp)
lea    0xe16(%rip),%rdi
mov    $0x2,%al
call   1030 <printf@plt>
lea    0x28(%rsp),%rbx
lea    0x20(%rsp),%rbp
lea    0x18(%rsp),%r13
xorps  %xmm2,%xmm2
movsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rbp,%r8
mov    %r13,%r9
call   1060 <gluUnProject@plt>
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
movsd  0x18(%rsp),%xmm2
lea    0xde7(%rip),%rdi
mov    $0x3,%al
call   1030 <printf@plt>
movsd  0xdaa(%rip),%xmm2
movsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rbp,%r8
mov    %r13,%r9
call   1060 <gluUnProject@plt>
movsd  0x28(%rsp),%xmm0
movsd  0x20(%rsp),%xmm1
movsd  0x18(%rsp),%xmm2
lea    0xdc6(%rip),%rdi
mov    $0x3,%al
call   1030 <printf@plt>
jmp    12a6 <unproject_mouse+0x156>
mov    0x2d36(%rip),%rax
cmp    %esi,(%rax)
je     12b8 <unproject_mouse+0x168>
add    $0x148,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@GLUT_DOWN = global i32 0
@GL_VIEWPORT = global i32 0
@GL_MODELVIEW_MATRIX = global i32 0
@GL_PROJECTION_MATRIX = global i32 0
@.str = [38 x i8] c"Coordinates at cursor are (%4d, %4d)\0A\00"
@.str.1 = [42 x i8] c"World coords at z = 0.0 are (%f, %f, %f)\0A\00"
@.str.2 = [42 x i8] c"World coords at z = 1.0 are (%f, %f, %f)\0A\00"
define void @unproject_mouse(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4 x i32]
%10 = alloca [16 x double]
%11 = alloca [16 x double]
%12 = alloca i32
%13 = alloca double
%14 = alloca double
%15 = alloca double
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%16 = load i32, i32* %5
switch i32 %16, label %73 [
i32 129, label %17
i32 128, label %66
]
17: ; preds = %4
%18 = load i32, i32* %6
%19 = load i32, i32* @GLUT_DOWN
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %65
21: ; preds = %17
%22 = load i32, i32* @GL_VIEWPORT
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%24 = call i32 @glGetIntegerv(i32 %22, i32* %23)
%25 = load i32, i32* @GL_MODELVIEW_MATRIX
%26 = getelementptr inbounds [16 x double], [16 x double]* %10, i64 0, i64 0
%27 = call i32 @glGetDoublev(i32 %25, double* %26)
%28 = load i32, i32* @GL_PROJECTION_MATRIX
%29 = getelementptr inbounds [16 x double], [16 x double]* %11, i64 0, i64 0
%30 = call i32 @glGetDoublev(i32 %28, double* %29)
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 3
%32 = load i32, i32* %31
%33 = load i32, i32* %8
%34 = sub nsw i32 %32, %33
%35 = sub nsw i32 %34, 1
store i32 %35, i32* %12
%36 = load i32, i32* %7
%37 = sitofp i32 %36 to double
%38 = load i32, i32* %12
%39 = sitofp i32 %38 to double
%40 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), double %37, double %39)
%41 = load i32, i32* %7
%42 = sitofp i32 %41 to double
%43 = load i32, i32* %12
%44 = sitofp i32 %43 to double
%45 = getelementptr inbounds [16 x double], [16 x double]* %10, i64 0, i64 0
%46 = getelementptr inbounds [16 x double], [16 x double]* %11, i64 0, i64 0
%47 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%48 = call i32 @gluUnProject(double %42, double %44, double 0.000000e+00, double* %45, double* %46, i32* %47, double* %13, double* %14, double* %15)
%49 = load double, double* %13
%50 = load double, double* %14
%51 = load double, double* %15
%52 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), double %49, double %50, double %51)
%53 = load i32, i32* %7
%54 = sitofp i32 %53 to double
%55 = load i32, i32* %12
%56 = sitofp i32 %55 to double
%57 = getelementptr inbounds [16 x double], [16 x double]* %10, i64 0, i64 0
%58 = getelementptr inbounds [16 x double], [16 x double]* %11, i64 0, i64 0
%59 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%60 = call i32 @gluUnProject(double %54, double %56, double 1.000000e+00, double* %57, double* %58, i32* %59, double* %13, double* %14, double* %15)
%61 = load double, double* %13
%62 = load double, double* %14
%63 = load double, double* %15
%64 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), double %61, double %62, double %63)
br label %65
65: ; preds = %21, %17
br label %74
66: ; preds = %4
%67 = load i32, i32* %6
%68 = load i32, i32* @GLUT_DOWN
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @exit(i32 0)
unreachable
72: ; preds = %66
br label %74
73: ; preds = %4
br label %74
74: ; preds = %73, %72, %65
ret void
}

LLVM IR (-O2):
@GLUT_DOWN = global i32 0
@GL_VIEWPORT = global i32 0
@GL_MODELVIEW_MATRIX = global i32 0
@GL_PROJECTION_MATRIX = global i32 0
@.str = [38 x i8] c"Coordinates at cursor are (%4d, %4d)\0A\00"
@.str.1 = [42 x i8] c"World coords at z = 0.0 are (%f, %f, %f)\0A\00"
@.str.2 = [42 x i8] c"World coords at z = 1.0 are (%f, %f, %f)\0A\00"
define void @unproject_mouse(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [4 x i32]
%6 = alloca [16 x double]
%7 = alloca [16 x double]
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = bitcast [4 x i32]* %5 to i8*
%12 = bitcast [16 x double]* %6 to i8*
%13 = bitcast [16 x double]* %7 to i8*
%14 = bitcast double* %8 to i8*
%15 = bitcast double* %9 to i8*
%16 = bitcast double* %10 to i8*
switch i32 %0, label %52 [
i32 129, label %17
i32 128, label %47
]
17: ; preds = %4
%18 = load i32, i32* @GLUT_DOWN
%19 = icmp eq i32 %18, %1
br i1 %19, label %20, label %52
20: ; preds = %17
%21 = load i32, i32* @GL_VIEWPORT
%22 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 0
%23 = call i32 @glGetIntegerv(i32 %21, i32* %22)
%24 = load i32, i32* @GL_MODELVIEW_MATRIX
%25 = getelementptr inbounds [16 x double], [16 x double]* %6, i64 0, i64 0
%26 = call i32 @glGetDoublev(i32 %24, double* %25)
%27 = load i32, i32* @GL_PROJECTION_MATRIX
%28 = getelementptr inbounds [16 x double], [16 x double]* %7, i64 0, i64 0
%29 = call i32 @glGetDoublev(i32 %27, double* %28)
%30 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 3
%31 = load i32, i32* %30
%32 = xor i32 %3, -1
%33 = add i32 %31, %32
%34 = sitofp i32 %2 to double
%35 = sitofp i32 %33 to double
%36 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), double %34, double %35)
%37 = call i32 @gluUnProject(double %34, double %35, double 0.000000e+00, double* %25, double* %28, i32* %22, double* %8, double* %9, double* %10)
%38 = load double, double* %8
%39 = load double, double* %9
%40 = load double, double* %10
%41 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), double %38, double %39, double %40)
%42 = call i32 @gluUnProject(double %34, double %35, double 1.000000e+00, double* %25, double* %28, i32* %22, double* %8, double* %9, double* %10)
%43 = load double, double* %8
%44 = load double, double* %9
%45 = load double, double* %10
%46 = call i32 (i8*, double, double, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), double %43, double %44, double %45)
br label %52
47: ; preds = %4
%48 = load i32, i32* @GLUT_DOWN
%49 = icmp eq i32 %48, %1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = tail call i32 @exit(i32 0)
unreachable
52: ; preds = %4, %47, %17, %20
ret void
}