#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 /*<<< orphan*/ assert(int);
double copysign(double, double);
double fmod(double, double);
int isfinite(double);
int /*<<< orphan*/ isnan(double);
double az_signmod(double a, double b, double sign) {
  assert(isfinite(a));
  assert(isfinite(b));
  assert(b != 0.0);
  assert(!isnan(sign));
  const double remain = fmod(a, b);
  if (remain == 0.0)
    return copysign(0.0, sign);
  else if (copysign(1.0, a) == copysign(1.0, sign))
    return remain;
  else
    return remain + copysign(b, sign);
}


x86 assembly (clang, -O0):
<az_signmod>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <isfinite@plt>
mov    %eax,%edi
call   1040 <assert@plt>
movsd  -0x18(%rbp),%xmm0
call   1050 <isfinite@plt>
mov    %eax,%edi
call   1040 <assert@plt>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
setne  %al
setp   %cl
or     %cl,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movsd  -0x20(%rbp),%xmm0
call   1060 <isnan@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
movsd  -0x10(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
call   1030 <fmod@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11f9 <az_signmod+0xb9>
jp     11f9 <az_signmod+0xb9>
movsd  -0x20(%rbp),%xmm0
movaps 0xe15(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1280 <az_signmod+0x140>
movsd  -0x10(%rbp),%xmm0
movaps 0xdfb(%rip),%xmm1
pand   %xmm1,%xmm0
movaps 0xe00(%rip),%xmm1
por    %xmm1,%xmm0
movsd  -0x20(%rbp),%xmm1
movaps 0xde0(%rip),%xmm2
pand   %xmm2,%xmm1
movaps 0xde5(%rip),%xmm2
por    %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jne    124e <az_signmod+0x10e>
jp     124e <az_signmod+0x10e>
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1280 <az_signmod+0x140>
movsd  -0x28(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm2
movaps 0xd9c(%rip),%xmm3
pand   %xmm3,%xmm2
movaps 0xdb1(%rip),%xmm3
pand   %xmm3,%xmm1
por    %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<az_signmod>:
sub    $0x38,%rsp
movaps %xmm2,0x10(%rsp)
movapd %xmm1,(%rsp)
movaps %xmm0,0x20(%rsp)
call   1050 <isfinite@plt>
mov    %eax,%edi
call   1040 <assert@plt>
movaps (%rsp),%xmm0
call   1050 <isfinite@plt>
mov    %eax,%edi
call   1040 <assert@plt>
xorpd  %xmm1,%xmm1
movapd (%rsp),%xmm0
cmpneqsd %xmm1,%xmm0
movq   %xmm0,%rdi
and    $0x1,%edi
call   1040 <assert@plt>
movaps 0x10(%rsp),%xmm0
call   1060 <isnan@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
movapd 0x20(%rsp),%xmm0
movaps (%rsp),%xmm1
call   1030 <fmod@plt>
ucomisd 0xe70(%rip),%xmm0
jne    11cd <az_signmod+0x8d>
jp     11cd <az_signmod+0x8d>
movaps 0x10(%rsp),%xmm0
andps  0xe38(%rip),%xmm0
add    $0x38,%rsp
ret
movapd (%rsp),%xmm4
movapd 0xe26(%rip),%xmm1
movapd 0x20(%rsp),%xmm5
andpd  %xmm1,%xmm5
movsd  0xe34(%rip),%xmm2
orpd   %xmm2,%xmm5
movapd 0x10(%rsp),%xmm3
andpd  %xmm3,%xmm1
orpd   %xmm2,%xmm1
ucomisd %xmm1,%xmm5
jne    120b <az_signmod+0xcb>
jp     120b <az_signmod+0xcb>
add    $0x38,%rsp
ret
andpd  0xded(%rip),%xmm3
andpd  0xdf5(%rip),%xmm4
orpd   %xmm3,%xmm4
addsd  %xmm4,%xmm0
add    $0x38,%rsp
ret

LLVM IR (-O0):
define double @az_signmod(double %0, double %1, double %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
store double %0, double* %5
store double %1, double* %6
store double %2, double* %7
%9 = load double, double* %5
%10 = call i32 @isfinite(double %9)
%11 = call i32 @assert(i32 %10)
%12 = load double, double* %6
%13 = call i32 @isfinite(double %12)
%14 = call i32 @assert(i32 %13)
%15 = load double, double* %6
%16 = fcmp une double %15, 0.000000e+00
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load double, double* %7
%20 = call i32 @isnan(double %19)
%21 = icmp ne i32 %20, 0
%22 = xor i1 %21, true
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load double, double* %5
%26 = load double, double* %6
%27 = call double @fmod(double %25, double %26)
store double %27, double* %8
%28 = load double, double* %8
%29 = fcmp oeq double %28, 0.000000e+00
br i1 %29, label %30, label %33
30: ; preds = %3
%31 = load double, double* %7
%32 = call double @llvm.copysign.f64(double 0.000000e+00, double %31)
store double %32, double* %4
br label %47
33: ; preds = %3
%34 = load double, double* %5
%35 = call double @llvm.copysign.f64(double 1.000000e+00, double %34)
%36 = load double, double* %7
%37 = call double @llvm.copysign.f64(double 1.000000e+00, double %36)
%38 = fcmp oeq double %35, %37
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = load double, double* %8
store double %40, double* %4
br label %47
41: ; preds = %33
%42 = load double, double* %8
%43 = load double, double* %6
%44 = load double, double* %7
%45 = call double @llvm.copysign.f64(double %43, double %44)
%46 = fadd double %42, %45
store double %46, double* %4
br label %47
47: ; preds = %41, %39, %30
%48 = load double, double* %4
ret double %48
}

LLVM IR (-O2):
define double @az_signmod(double %0, double %1, double %2) {
%4 = tail call i32 @isfinite(double %0)
%5 = tail call i32 @assert(i32 %4)
%6 = tail call i32 @isfinite(double %1)
%7 = tail call i32 @assert(i32 %6)
%8 = fcmp une double %1, 0.000000e+00
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = tail call i32 @isnan(double %2)
%12 = icmp eq i32 %11, 0
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call double @fmod(double %0, double %1)
%16 = fcmp oeq double %15, 0.000000e+00
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = tail call double @llvm.copysign.f64(double 0.000000e+00, double %2)
br label %26
19: ; preds = %3
%20 = tail call double @llvm.copysign.f64(double 1.000000e+00, double %0)
%21 = tail call double @llvm.copysign.f64(double 1.000000e+00, double %2)
%22 = fcmp oeq double %20, %21
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = tail call double @llvm.copysign.f64(double %1, double %2)
%25 = fadd double %24, %15
br label %26
26: ; preds = %19, %23, %17
%27 = phi double [ %18, %17 ], [ %25, %23 ], [ %15, %19 ]
ret double %27
}