#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 */
double fabs(int);
int hex_qr2l(int, int);
int lround(double);
double sqrt(double);
int hex_xy2qr(double p, double x, double y, int *q, int *r) {
  double qf, rf, sf, qdf, rdf, sdf;
  int qr, rr, sr;
  qf = (1.0 * x) / (sqrt(3.0) / 2.0 * p);
  rf = (-0.5 * x + sqrt(3.0) / 2.0 * y) / (sqrt(3.0) / 2.0 * p);
  sf = -qf - rf;
  qr = lround(qf);
  rr = lround(rf);
  sr = lround(sf);
  qdf = fabs(qr - qf);
  rdf = fabs(rr - rf);
  sdf = fabs(sr - sf);
  *q = qr;
  *r = rr;
  if (qdf > rdf && qdf > sdf) {
    *q = -rr - sr;
  } else if (rdf > sdf) {
    *r = -qr - sr;
  }
  return hex_qr2l(*q, *r);
}


x86 assembly (clang, -O0):
<hex_xy2qr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
mov    %rsi,-0x28(%rbp)
movsd  0xeae(%rip),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  0xe8c(%rip),%xmm0
call   1060 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x80(%rbp),%xmm0
movsd  0xe6f(%rip),%xmm2
divsd  %xmm2,%xmm1
mulsd  -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  0xe53(%rip),%xmm0
call   1060 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x78(%rbp),%xmm0
movsd  0xe36(%rip),%xmm2
divsd  %xmm2,%xmm1
mulsd  -0x18(%rbp),%xmm1
movsd  0xe35(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  0xe18(%rip),%xmm0
call   1060 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  -0x70(%rbp),%xmm0
movsd  0xdfb(%rip),%xmm2
divsd  %xmm2,%xmm1
mulsd  -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
subsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <lround@plt>
mov    %eax,-0x5c(%rbp)
movsd  -0x38(%rbp),%xmm0
call   1030 <lround@plt>
mov    %eax,-0x60(%rbp)
movsd  -0x40(%rbp),%xmm0
call   1030 <lround@plt>
mov    %eax,-0x64(%rbp)
cvtsi2sdl -0x5c(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <fabs@plt>
movsd  %xmm0,-0x48(%rbp)
cvtsi2sdl -0x60(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <fabs@plt>
movsd  %xmm0,-0x50(%rbp)
cvtsi2sdl -0x64(%rbp),%xmm0
subsd  -0x40(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <fabs@plt>
movsd  %xmm0,-0x58(%rbp)
mov    -0x5c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x60(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movsd  -0x48(%rbp),%xmm0
ucomisd -0x50(%rbp),%xmm0
jbe    12f1 <hex_xy2qr+0x1b1>
movsd  -0x48(%rbp),%xmm0
ucomisd -0x58(%rbp),%xmm0
jbe    12f1 <hex_xy2qr+0x1b1>
xor    %ecx,%ecx
sub    -0x60(%rbp),%ecx
sub    -0x64(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    1314 <hex_xy2qr+0x1d4>
movsd  -0x50(%rbp),%xmm0
ucomisd -0x58(%rbp),%xmm0
jbe    130f <hex_xy2qr+0x1cf>
xor    %ecx,%ecx
sub    -0x5c(%rbp),%ecx
sub    -0x64(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    1314 <hex_xy2qr+0x1d4>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1040 <hex_qr2l@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hex_xy2qr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movsd  0xeb6(%rip),%xmm3
mulsd  %xmm3,%xmm0
movapd %xmm1,%xmm4
divsd  %xmm0,%xmm4
mulsd  %xmm3,%xmm2
mulsd  0xea6(%rip),%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movapd 0xe98(%rip),%xmm0
movapd %xmm4,0x10(%rsp)
xorpd  %xmm4,%xmm0
subsd  %xmm1,%xmm0
movapd %xmm0,0x20(%rsp)
movapd %xmm4,%xmm0
call   1030 <lround@plt>
mov    %eax,%ebx
movsd  0x8(%rsp),%xmm0
call   1030 <lround@plt>
mov    %eax,%ebp
movaps 0x20(%rsp),%xmm0
call   1030 <lround@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mov    %eax,%r12d
subsd  0x10(%rsp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <fabs@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
movsd  %xmm0,0x10(%rsp)
subsd  0x8(%rsp),%xmm1
cvttsd2si %xmm1,%edi
call   1050 <fabs@plt>
xorps  %xmm1,%xmm1
cvtsi2sd %r12d,%xmm1
movsd  %xmm0,0x8(%rsp)
subsd  0x20(%rsp),%xmm1
cvttsd2si %xmm1,%edi
call   1050 <fabs@plt>
movsd  0x8(%rsp),%xmm2
movsd  0x10(%rsp),%xmm1
mov    %ebx,(%r15)
mov    %ebp,(%r14)
ucomisd %xmm2,%xmm1
jbe    122b <hex_xy2qr+0xfb>
ucomisd %xmm0,%xmm1
jbe    122b <hex_xy2qr+0xfb>
add    %r12d,%ebp
neg    %ebp
mov    %ebp,(%r15)
mov    (%r14),%ebp
jmp    123b <hex_xy2qr+0x10b>
ucomisd %xmm0,%xmm2
jbe    123b <hex_xy2qr+0x10b>
add    %r12d,%ebx
neg    %ebx
mov    %ebx,(%r14)
mov    %ebx,%ebp
mov    (%r15),%edi
mov    %ebp,%esi
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <hex_qr2l@plt>

LLVM IR (-O0):
define i32 @hex_xy2qr(double %0, double %1, double %2, i32* %3, i32* %4) {
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store double %0, double* %6
store double %1, double* %7
store double %2, double* %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%20 = load double, double* %7
%21 = fmul double 1.000000e+00, %20
%22 = call double @sqrt(double 3.000000e+00)
%23 = fdiv double %22, 2.000000e+00
%24 = load double, double* %6
%25 = fmul double %23, %24
%26 = fdiv double %21, %25
store double %26, double* %11
%27 = load double, double* %7
%28 = call double @sqrt(double 3.000000e+00)
%29 = fdiv double %28, 2.000000e+00
%30 = load double, double* %8
%31 = fmul double %29, %30
%32 = call double @llvm.fmuladd.f64(double -5.000000e-01, double %27, double %31)
%33 = call double @sqrt(double 3.000000e+00)
%34 = fdiv double %33, 2.000000e+00
%35 = load double, double* %6
%36 = fmul double %34, %35
%37 = fdiv double %32, %36
store double %37, double* %12
%38 = load double, double* %11
%39 = fneg double %38
%40 = load double, double* %12
%41 = fsub double %39, %40
store double %41, double* %13
%42 = load double, double* %11
%43 = call i32 @lround(double %42)
store i32 %43, i32* %17
%44 = load double, double* %12
%45 = call i32 @lround(double %44)
store i32 %45, i32* %18
%46 = load double, double* %13
%47 = call i32 @lround(double %46)
store i32 %47, i32* %19
%48 = load i32, i32* %17
%49 = sitofp i32 %48 to double
%50 = load double, double* %11
%51 = fsub double %49, %50
%52 = fptosi double %51 to i32
%53 = call double @fabs(i32 %52)
store double %53, double* %14
%54 = load i32, i32* %18
%55 = sitofp i32 %54 to double
%56 = load double, double* %12
%57 = fsub double %55, %56
%58 = fptosi double %57 to i32
%59 = call double @fabs(i32 %58)
store double %59, double* %15
%60 = load i32, i32* %19
%61 = sitofp i32 %60 to double
%62 = load double, double* %13
%63 = fsub double %61, %62
%64 = fptosi double %63 to i32
%65 = call double @fabs(i32 %64)
store double %65, double* %16
%66 = load i32, i32* %17
%67 = load i32*, i32** %9
store i32 %66, i32* %67
%68 = load i32, i32* %18
%69 = load i32*, i32** %10
store i32 %68, i32* %69
%70 = load double, double* %14
%71 = load double, double* %15
%72 = fcmp ogt double %70, %71
br i1 %72, label %73, label %83
73: ; preds = %5
%74 = load double, double* %14
%75 = load double, double* %16
%76 = fcmp ogt double %74, %75
br i1 %76, label %77, label %83
77: ; preds = %73
%78 = load i32, i32* %18
%79 = sub nsw i32 0, %78
%80 = load i32, i32* %19
%81 = sub nsw i32 %79, %80
%82 = load i32*, i32** %9
store i32 %81, i32* %82
br label %94
83: ; preds = %73, %5
%84 = load double, double* %15
%85 = load double, double* %16
%86 = fcmp ogt double %84, %85
br i1 %86, label %87, label %93
87: ; preds = %83
%88 = load i32, i32* %17
%89 = sub nsw i32 0, %88
%90 = load i32, i32* %19
%91 = sub nsw i32 %89, %90
%92 = load i32*, i32** %10
store i32 %91, i32* %92
br label %93
93: ; preds = %87, %83
br label %94
94: ; preds = %93, %77
%95 = load i32*, i32** %9
%96 = load i32, i32* %95
%97 = load i32*, i32** %10
%98 = load i32, i32* %97
%99 = call i32 @hex_qr2l(i32 %96, i32 %98)
ret i32 %99
}

LLVM IR (-O2):
define i32 @hex_xy2qr(double %0, double %1, double %2, i32* nocapture %3, i32* nocapture %4) {
%6 = fmul double %0, 0x3FEBB67AE8584CAA
%7 = fdiv double %1, %6
%8 = fmul double %2, 0x3FEBB67AE8584CAA
%9 = tail call double @llvm.fmuladd.f64(double %1, double -5.000000e-01, double %8)
%10 = fdiv double %9, %6
%11 = fneg double %7
%12 = fsub double %11, %10
%13 = tail call i32 @lround(double %7)
%14 = tail call i32 @lround(double %10)
%15 = tail call i32 @lround(double %12)
%16 = sitofp i32 %13 to double
%17 = fsub double %16, %7
%18 = fptosi double %17 to i32
%19 = tail call double @fabs(i32 %18)
%20 = sitofp i32 %14 to double
%21 = fsub double %20, %10
%22 = fptosi double %21 to i32
%23 = tail call double @fabs(i32 %22)
%24 = sitofp i32 %15 to double
%25 = fsub double %24, %12
%26 = fptosi double %25 to i32
%27 = tail call double @fabs(i32 %26)
store i32 %13, i32* %3
store i32 %14, i32* %4
%28 = fcmp ogt double %19, %23
%29 = fcmp ogt double %19, %27
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %31, label %35
31: ; preds = %5
%32 = add i32 %14, %15
%33 = sub i32 0, %32
store i32 %33, i32* %3
%34 = load i32, i32* %4
br label %40
35: ; preds = %5
%36 = fcmp ogt double %23, %27
br i1 %36, label %37, label %40
37: ; preds = %35
%38 = add i32 %13, %15
%39 = sub i32 0, %38
store i32 %39, i32* %4
br label %40
40: ; preds = %35, %37, %31
%41 = phi i32 [ %14, %35 ], [ %39, %37 ], [ %34, %31 ]
%42 = load i32, i32* %3
%43 = tail call i32 @hex_qr2l(i32 %42, i32 %41)
ret i32 %43
}