#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 sqrt(int);
unsigned int row_index(unsigned int i, unsigned int M) {
  double m = M;
  double row = (-2 * m - 1 + sqrt((4 * m * (m + 1) - 8 * (double)i - 7))) / -2;
  if (row == (double)(int)row)
    row -= 1;
  return (unsigned int)row;
}


x86 assembly (clang, -O0):
<row_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm2
movaps %xmm2,%xmm1
addsd  %xmm1,%xmm1
movsd  0xed9(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xed0(%rip),%xmm1
movaps %xmm2,%xmm0
mulsd  %xmm1,%xmm0
movsd  0xec9(%rip),%xmm1
addsd  %xmm1,%xmm2
mov    -0x4(%rbp),%eax
cvtsi2sd %rax,%xmm3
movsd  0xe9d(%rip),%xmm1
mulsd  %xmm3,%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xe6a(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1030 <sqrt@plt>
movsd  -0x20(%rbp),%xmm0
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm0
movsd  0xe40(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
cvttsd2si -0x18(%rbp),%eax
cvtsi2sd %eax,%xmm1
ucomisd %xmm1,%xmm0
jne    11fd <row_index+0xed>
jp     11fd <row_index+0xed>
movsd  -0x18(%rbp),%xmm0
movsd  0xe34(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
cvttsd2si -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<row_index>:
push   %rax
mov    %esi,%eax
cvtsi2sd %rax,%xmm0
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xed8(%rip),%xmm3
movsd  0xed8(%rip),%xmm2
mulsd  %xmm0,%xmm2
addsd  0xed4(%rip),%xmm0
subsd  %xmm1,%xmm3
movsd  %xmm3,(%rsp)
mov    %edi,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
mulsd  %xmm2,%xmm0
mulsd  0xebd(%rip),%xmm1
addsd  %xmm0,%xmm1
addsd  0xeb9(%rip),%xmm1
cvttsd2si %xmm1,%edi
call   1030 <sqrt@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  (%rsp),%xmm0
mulsd  0xea4(%rip),%xmm0
cvttpd2dq %xmm0,%xmm1
cvtdq2pd %xmm1,%xmm1
movsd  0xe6c(%rip),%xmm2
addsd  %xmm0,%xmm2
cmpeqsd %xmm0,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
cvttsd2si %xmm1,%rax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @row_index(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = uitofp i32 %7 to double
store double %8, double* %5
%9 = load double, double* %5
%10 = call double @llvm.fmuladd.f64(double -2.000000e+00, double %9, double -1.000000e+00)
%11 = load double, double* %5
%12 = fmul double 4.000000e+00, %11
%13 = load double, double* %5
%14 = fadd double %13, 1.000000e+00
%15 = load i32, i32* %3
%16 = uitofp i32 %15 to double
%17 = fmul double 8.000000e+00, %16
%18 = fneg double %17
%19 = call double @llvm.fmuladd.f64(double %12, double %14, double %18)
%20 = fsub double %19, 7.000000e+00
%21 = fptosi double %20 to i32
%22 = call i32 @sqrt(i32 %21)
%23 = sitofp i32 %22 to double
%24 = fadd double %10, %23
%25 = fdiv double %24, -2.000000e+00
store double %25, double* %6
%26 = load double, double* %6
%27 = load double, double* %6
%28 = fptosi double %27 to i32
%29 = sitofp i32 %28 to double
%30 = fcmp oeq double %26, %29
br i1 %30, label %31, label %34
31: ; preds = %2
%32 = load double, double* %6
%33 = fsub double %32, 1.000000e+00
store double %33, double* %6
br label %34
34: ; preds = %31, %2
%35 = load double, double* %6
%36 = fptoui double %35 to i32
ret i32 %36
}

LLVM IR (-O2):
define i32 @row_index(i32 %0, i32 %1) {
%3 = uitofp i32 %1 to double
%4 = tail call double @llvm.fmuladd.f64(double %3, double -2.000000e+00, double -1.000000e+00)
%5 = fmul double %3, 4.000000e+00
%6 = fadd double %3, 1.000000e+00
%7 = uitofp i32 %0 to double
%8 = fmul double %7, -8.000000e+00
%9 = tail call double @llvm.fmuladd.f64(double %5, double %6, double %8)
%10 = fadd double %9, -7.000000e+00
%11 = fptosi double %10 to i32
%12 = tail call i32 @sqrt(i32 %11)
%13 = sitofp i32 %12 to double
%14 = fadd double %4, %13
%15 = fmul double %14, -5.000000e-01
%16 = fptosi double %15 to i32
%17 = sitofp i32 %16 to double
%18 = fcmp oeq double %15, %17
%19 = fadd double %15, -1.000000e+00
%20 = select i1 %18, double %19, double %15
%21 = fptoui double %20 to i32
ret i32 %21
}