#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*/ fixed_point_set_sign(unsigned long long *, unsigned int,
                                        unsigned int);
int /*<<< orphan*/ is_fp2fd_overflow(double, unsigned int, unsigned int);
void fixed_point_fp2fd_peform(void *result, double fp, unsigned width,
                              unsigned frac_bits) {
  unsigned long long *tmp;
  unsigned sign;
  tmp = (unsigned long long *)&fp;
  sign = (((*tmp) >> (63)) & 0x1ull);
  tmp = result;
  if (!is_fp2fd_overflow(fp, width, frac_bits))
    *tmp = (unsigned long long)fp;
  else
    *tmp = (~0ull >> 1) >> (64 - width);
  *tmp &= (~0ull >> (64 - width));
  fixed_point_set_sign(tmp, width, sign);
}


x86 assembly (clang, -O0):
<fixed_point_fp2fd_peform>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
shr    $0x3f,%rax
and    $0x1,%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <is_fp2fd_overflow@plt>
cmp    $0x0,%eax
jne    11a9 <fixed_point_fp2fd_peform+0x89>
movsd  -0x10(%rbp),%xmm0
movsd  0xe81(%rip),%xmm2
movaps %xmm0,%xmm1
subsd  %xmm2,%xmm1
cvttsd2si %xmm1,%rax
cvttsd2si %xmm0,%rcx
mov    %rcx,%rdx
sar    $0x3f,%rdx
and    %rdx,%rax
or     %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
jmp    11cc <fixed_point_fp2fd_peform+0xac>
mov    $0x40,%eax
sub    -0x14(%rbp),%eax
mov    %eax,%eax
mov    %eax,%ecx
movabs $0x7fffffffffffffff,%rax
ff ff 7f
shr    %cl,%rax
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    $0x40,%eax
sub    -0x14(%rbp),%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
shr    %cl,%rax
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
and    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1030 <fixed_point_set_sign@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fixed_point_fp2fd_peform>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
movaps %xmm0,(%rsp)
mov    %rdi,%r14
mov    %esi,%edi
mov    %edx,%esi
call   1040 <is_fp2fd_overflow@plt>
movapd (%rsp),%xmm0
cvttsd2si %xmm0,%rsi
mov    %rsi,%rcx
sar    $0x3f,%rcx
movmskpd %xmm0,%edx
subsd  0xea9(%rip),%xmm0
cvttsd2si %xmm0,%rdi
and    %rcx,%rdi
mov    %ebp,%ecx
neg    %cl
movabs $0x7fffffffffffffff,%rbx
ff ff 7f
shr    %cl,%rbx
or     %rsi,%rdi
test   %eax,%eax
cmove  %rdi,%rbx
and    $0x1,%edx
shl    %cl,%rbx
shr    %cl,%rbx
mov    %rbx,(%r14)
mov    %r14,%rdi
mov    %ebp,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fixed_point_set_sign@plt>

LLVM IR (-O0):
define void @fixed_point_fp2fd_peform(i8* %0, double %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64*
%10 = alloca i32
store i8* %0, i8** %5
store double %1, double* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = bitcast double* %6 to i64*
store i64* %11, i64** %9
%12 = load i64*, i64** %9
%13 = load i64, i64* %12
%14 = lshr i64 %13, 63
%15 = and i64 %14, 1
%16 = trunc i64 %15 to i32
store i32 %16, i32* %10
%17 = load i8*, i8** %5
%18 = bitcast i8* %17 to i64*
store i64* %18, i64** %9
%19 = load double, double* %6
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = call i32 @is_fp2fd_overflow(double %19, i32 %20, i32 %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %4
%25 = load double, double* %6
%26 = fptoui double %25 to i64
%27 = load i64*, i64** %9
store i64 %26, i64* %27
br label %34
28: ; preds = %4
%29 = load i32, i32* %7
%30 = sub i32 64, %29
%31 = zext i32 %30 to i64
%32 = lshr i64 9223372036854775807, %31
%33 = load i64*, i64** %9
store i64 %32, i64* %33
br label %34
34: ; preds = %28, %24
%35 = load i32, i32* %7
%36 = sub i32 64, %35
%37 = zext i32 %36 to i64
%38 = lshr i64 -1, %37
%39 = load i64*, i64** %9
%40 = load i64, i64* %39
%41 = and i64 %40, %38
store i64 %41, i64* %39
%42 = load i64*, i64** %9
%43 = load i32, i32* %7
%44 = load i32, i32* %10
%45 = call i32 @fixed_point_set_sign(i64* %42, i32 %43, i32 %44)
ret void
}

LLVM IR (-O2):
define void @fixed_point_fp2fd_peform(i8* %0, double %1, i32 %2, i32 %3) {
%5 = bitcast i8* %0 to i64*
%6 = tail call i32 @is_fp2fd_overflow(double %1, i32 %2, i32 %3)
%7 = icmp eq i32 %6, 0
%8 = fptoui double %1 to i64
%9 = sub i32 64, %2
%10 = zext i32 %9 to i64
%11 = lshr i64 9223372036854775807, %10
%12 = select i1 %7, i64 %8, i64 %11
%13 = bitcast double %1 to i64
%14 = lshr i64 %13, 63
%15 = trunc i64 %14 to i32
%16 = lshr i64 -1, %10
%17 = and i64 %12, %16
store i64 %17, i64* %5
%18 = tail call i32 @fixed_point_set_sign(i64* %5, i32 %2, i32 %15)
ret void
}