#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 */
scalar_t__ floor(int);
int sqrt(int);
void convert_Zidx(int p, int *N, int *M) {
  int n = (int)floor((-1. + sqrt(1. + 8. * p)) / 2.);
  if (M)
    *M = (int)(2.0 * (p - n * (n + 1.) / 2. - 0.5 * (double)n));
  if (N)
    *N = n;
}


x86 assembly (clang, -O0):
<convert_Zidx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cvtsi2sdl -0x4(%rbp),%xmm0
movsd  0xed0(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xecc(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1030 <sqrt@plt>
cvtsi2sd %eax,%xmm1
movsd  0xea3(%rip),%xmm0
addsd  %xmm1,%xmm0
movsd  0xe8f(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <floor@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e6 <convert_Zidx+0xc6>
cvtsi2sdl -0x4(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
cvtsi2sdl -0x1c(%rbp),%xmm2
movsd  0xe75(%rip),%xmm3
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm1
movsd  0xe4d(%rip),%xmm2
divsd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  0xe58(%rip),%xmm2
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe28(%rip),%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11fa <convert_Zidx+0xda>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_Zidx>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
cvtsi2sd %edi,%xmm1
movsd  0xeca(%rip),%xmm0
movsd  %xmm1,(%rsp)
mulsd  %xmm1,%xmm0
addsd  0xec1(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <sqrt@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  0xeb1(%rip),%xmm0
mulsd  0xeb1(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <floor@plt>
test   %rbx,%rbx
je     11b3 <convert_Zidx+0x93>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  0xe84(%rip),%xmm1
addsd  %xmm0,%xmm1
mulsd  %xmm0,%xmm1
movsd  0xe8c(%rip),%xmm2
mulsd  %xmm2,%xmm1
movsd  (%rsp),%xmm3
addsd  %xmm1,%xmm3
mulsd  %xmm2,%xmm0
addsd  %xmm3,%xmm0
addsd  %xmm0,%xmm0
cvttsd2si %xmm0,%ecx
mov    %ecx,(%rbx)
test   %r14,%r14
je     11bb <convert_Zidx+0x9b>
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @convert_Zidx(i32 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%8 = load i32, i32* %4
%9 = sitofp i32 %8 to double
%10 = call double @llvm.fmuladd.f64(double 8.000000e+00, double %9, double 1.000000e+00)
%11 = fptosi double %10 to i32
%12 = call i32 @sqrt(i32 %11)
%13 = sitofp i32 %12 to double
%14 = fadd double -1.000000e+00, %13
%15 = fdiv double %14, 2.000000e+00
%16 = fptosi double %15 to i32
%17 = call i64 @floor(i32 %16)
%18 = trunc i64 %17 to i32
store i32 %18, i32* %7
%19 = load i32*, i32** %6
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %38
21: ; preds = %3
%22 = load i32, i32* %4
%23 = sitofp i32 %22 to double
%24 = load i32, i32* %7
%25 = sitofp i32 %24 to double
%26 = load i32, i32* %7
%27 = sitofp i32 %26 to double
%28 = fadd double %27, 1.000000e+00
%29 = fmul double %25, %28
%30 = fdiv double %29, 2.000000e+00
%31 = fsub double %23, %30
%32 = load i32, i32* %7
%33 = sitofp i32 %32 to double
%34 = call double @llvm.fmuladd.f64(double -5.000000e-01, double %33, double %31)
%35 = fmul double 2.000000e+00, %34
%36 = fptosi double %35 to i32
%37 = load i32*, i32** %6
store i32 %36, i32* %37
br label %38
38: ; preds = %21, %3
%39 = load i32*, i32** %5
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* %7
%43 = load i32*, i32** %5
store i32 %42, i32* %43
br label %44
44: ; preds = %41, %38
ret void
}

LLVM IR (-O2):
define void @convert_Zidx(i32 %0, i32* writeonly %1, i32* writeonly %2) {
%4 = sitofp i32 %0 to double
%5 = tail call double @llvm.fmuladd.f64(double %4, double 8.000000e+00, double 1.000000e+00)
%6 = fptosi double %5 to i32
%7 = tail call i32 @sqrt(i32 %6)
%8 = sitofp i32 %7 to double
%9 = fadd double %8, -1.000000e+00
%10 = fmul double %9, 5.000000e-01
%11 = fptosi double %10 to i32
%12 = tail call i64 @floor(i32 %11)
%13 = trunc i64 %12 to i32
%14 = icmp eq i32* %2, null
br i1 %14, label %24, label %15
15: ; preds = %3
%16 = sitofp i32 %13 to double
%17 = fadd double %16, 1.000000e+00
%18 = fmul double %17, %16
%19 = fmul double %18, 5.000000e-01
%20 = fsub double %4, %19
%21 = tail call double @llvm.fmuladd.f64(double %16, double -5.000000e-01, double %20)
%22 = fmul double %21, 2.000000e+00
%23 = fptosi double %22 to i32
store i32 %23, i32* %2
br label %24
24: ; preds = %15, %3
%25 = icmp eq i32* %1, null
br i1 %25, label %27, label %26
26: ; preds = %24
store i32 %13, i32* %1
br label %27
27: ; preds = %26, %24
ret void
}