int solve(int k) {
  double lower;
  int l;
  double m1, m2;
  lower = (-1 + sqrt(1 + 8.0 * k)) / 2;
  l = ceil(lower);
  if (k % 2) {
    if ((l - 2) % 4 == 0 || (l - 1) % 4 == 0)
      return l;
    m1 = ceil((l - 2) / 4.0);
    m2 = ceil((l - 1) / 4.0);
    return ((4 * m1 + 2) < (4 * m2 + 1) ? (4 * m1 + 2) : (4 * m2 + 1));
  }
  if ((l + 1) % 4 == 0 || l % 4 == 0)
    return l;
  m1 = ceil(l / 4.0);
  m2 = ceil((l + 1) / 4.0);
  return ((4 * m1) < (4 * m2 - 1) ? (4 * m1) : (4 * m2 - 1));
}


x86 assembly (clang, -O0):
<solve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  0xed8(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xed4(%rip),%xmm1
addsd  %xmm1,%xmm0
call   1040 <sqrt@plt>
movaps %xmm0,%xmm1
movsd  0xeb0(%rip),%xmm0
addsd  %xmm1,%xmm0
movsd  0xe9c(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     12a9 <solve+0x189>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11c0 <solve+0xa0>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11cb <solve+0xab>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1393 <solve+0x273>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe43(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  0xe23(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  0xe08(%rip),%xmm0
mulsd  %xmm0,%xmm1
movsd  0xddc(%rip),%xmm0
addsd  %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  0xdeb(%rip),%xmm2
mulsd  %xmm2,%xmm0
movsd  0xdd7(%rip),%xmm2
addsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    1276 <solve+0x156>
movsd  -0x20(%rbp),%xmm0
movsd  0xdc4(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xd98(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    1298 <solve+0x178>
movsd  -0x28(%rbp),%xmm0
movsd  0xd9d(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xd89(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x4(%rbp)
jmp    1393 <solve+0x273>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     12d4 <solve+0x1b4>
mov    -0x14(%rbp),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    12df <solve+0x1bf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1393 <solve+0x273>
cvtsi2sdl -0x14(%rbp),%xmm0
movsd  0xd34(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  0xd14(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  0xcfe(%rip),%xmm1
mulsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  0xcec(%rip),%xmm2
mulsd  %xmm2,%xmm0
movsd  0xcc8(%rip),%xmm2
addsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    1365 <solve+0x245>
movsd  0xcca(%rip),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
jmp    1387 <solve+0x267>
movsd  -0x28(%rbp),%xmm0
movsd  0xcae(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xc8a(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<solve>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
cvtsi2sd %edi,%xmm0
mulsd  0xecd(%rip),%xmm0
addsd  0xecd(%rip),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     114b <solve+0x2b>
sqrtsd %xmm0,%xmm0
jmp    1150 <solve+0x30>
call   1040 <sqrt@plt>
addsd  0xeb8(%rip),%xmm0
mulsd  0xeb8(%rip),%xmm0
call   1030 <ceil@plt>
cvttsd2si %xmm0,%eax
test   $0x1,%bl
jne    11d9 <solve+0xb9>
lea    0x1(%rax),%ebx
test   $0x3,%bl
je     1249 <solve+0x129>
mov    %eax,%ecx
and    $0x3,%ecx
je     1249 <solve+0x129>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  0xe8c(%rip),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
mulsd  0xe73(%rip),%xmm0
call   1030 <ceil@plt>
movsd  0xe6e(%rip),%xmm1
mulsd  %xmm1,%xmm0
addsd  0xe4a(%rip),%xmm0
movsd  (%rsp),%xmm2
mulsd  %xmm1,%xmm2
minsd  %xmm0,%xmm2
cvttsd2si %xmm2,%eax
jmp    1249 <solve+0x129>
lea    -0x2(%rax),%ecx
test   $0x3,%cl
je     1249 <solve+0x129>
lea    -0x1(%rax),%edx
test   $0x3,%dl
je     1249 <solve+0x129>
movd   %edx,%xmm0
movd   %ecx,%xmm1
punpckldq %xmm0,%xmm1
cvtdq2pd %xmm1,%xmm0
mulpd  0xe2f(%rip),%xmm0
movapd %xmm0,0x10(%rsp)
call   1030 <ceil@plt>
movapd %xmm0,(%rsp)
movapd 0x10(%rsp),%xmm0
unpckhpd %xmm0,%xmm0
call   1030 <ceil@plt>
movapd (%rsp),%xmm1
unpcklpd %xmm0,%xmm1
mulpd  0xe0f(%rip),%xmm1
addpd  0xe17(%rip),%xmm1
movapd %xmm1,%xmm0
unpckhpd %xmm1,%xmm0
minsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @solve(i32 %0) {
%2 = sitofp i32 %0 to double
%3 = tail call double @llvm.fmuladd.f64(double %2, double 8.000000e+00, double 1.000000e+00)
%4 = tail call double @sqrt(double %3)
%5 = fadd double %4, -1.000000e+00
%6 = fmul double %5, 5.000000e-01
%7 = tail call double @llvm.ceil.f64(double %6)
%8 = fptosi double %7 to i32
%9 = and i32 %0, 1
%10 = icmp eq i32 %9, 0
br i1 %10, label %31, label %11
11: ; preds = %1
%12 = add nsw i32 %8, -2
%13 = and i32 %12, 3
%14 = icmp eq i32 %13, 0
br i1 %14, label %50, label %15
15: ; preds = %11
%16 = add nsw i32 %8, -1
%17 = and i32 %16, 3
%18 = icmp eq i32 %17, 0
br i1 %18, label %50, label %19
19: ; preds = %15
%20 = insertelement <2 x i32> poison, i32 %12, i64 0
%21 = insertelement <2 x i32> %20, i32 %16, i64 1
%22 = sitofp <2 x i32> %21 to <2 x double>
%23 = fmul <2 x double> %22, <double 2.500000e-01, double 2.500000e-01>
%24 = call <2 x double> @llvm.ceil.v2f64(<2 x double> %23)
%25 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %24, <2 x double> <double 4.000000e+00, double 4.000000e+00>, <2 x double> <double 2.000000e+00, double 1.000000e+00>)
%26 = extractelement <2 x double> %25, i64 0
%27 = extractelement <2 x double> %25, i64 1
%28 = fcmp olt double %26, %27
%29 = select i1 %28, double %26, double %27
%30 = fptosi double %29 to i32
br label %50
31: ; preds = %1
%32 = add nsw i32 %8, 1
%33 = and i32 %32, 3
%34 = icmp eq i32 %33, 0
%35 = and i32 %8, 3
%36 = icmp eq i32 %35, 0
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %50, label %38
38: ; preds = %31
%39 = sitofp i32 %8 to double
%40 = fmul double %39, 2.500000e-01
%41 = tail call double @llvm.ceil.f64(double %40)
%42 = sitofp i32 %32 to double
%43 = fmul double %42, 2.500000e-01
%44 = tail call double @llvm.ceil.f64(double %43)
%45 = fmul double %41, 4.000000e+00
%46 = tail call double @llvm.fmuladd.f64(double %44, double 4.000000e+00, double -1.000000e+00)
%47 = fcmp olt double %45, %46
%48 = select i1 %47, double %45, double %46
%49 = fptosi double %48 to i32
br label %50
50: ; preds = %31, %11, %15, %38, %19
%51 = phi i32 [ %30, %19 ], [ %49, %38 ], [ %8, %15 ], [ %8, %11 ], [ %8, %31 ]
ret i32 %51
}