#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 */
double fabs(double);
int lrint(double);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int tkh_encode_drift_into(double drift, char *target, int max_length) {
  if (max_length < 9)
    return -1;
  if (drift < 0)
    *target = '-';
  else
    *target = '+';
  drift = fabs(drift);
  int value = (drift >= 1.00000001e-8 && drift < 1.3) ? lrint(1.0 / drift) : 0;
  snprintf(target + 1, 9, "%08d", value);
  target[9] = 0;
  return 9;
}


x86 assembly (clang, -O0):
<tkh_encode_drift_into>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
cmpl   $0x9,-0x1c(%rbp)
jge    114a <tkh_encode_drift_into+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <tkh_encode_drift_into+0xdf>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1164 <tkh_encode_drift_into+0x44>
mov    -0x18(%rbp),%rax
movb   $0x2d,(%rax)
jmp    116b <tkh_encode_drift_into+0x4b>
mov    -0x18(%rbp),%rax
movb   $0x2b,(%rax)
movsd  -0x10(%rbp),%xmm0
movaps 0xea9(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe73(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     11c4 <tkh_encode_drift_into+0xa4>
movsd  0xe69(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11c4 <tkh_encode_drift_into+0xa4>
movsd  0xe5e(%rip),%xmm0
divsd  -0x10(%rbp),%xmm0
call   1040 <lrint@plt>
mov    %eax,-0x24(%rbp)
jmp    11ce <tkh_encode_drift_into+0xae>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    11ce <tkh_encode_drift_into+0xae>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%ecx
mov    $0x9,%esi
lea    0xe45(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,0x9(%rax)
movl   $0x9,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tkh_encode_drift_into>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
cmp    $0x9,%esi
jl     1198 <tkh_encode_drift_into+0x78>
mov    %rdi,%rbx
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
seta   %al
add    %al,%al
add    $0x2b,%al
mov    %al,(%rdi)
andpd  0xeb7(%rip),%xmm0
xor    %ecx,%ecx
ucomisd 0xebd(%rip),%xmm0
jb     117a <tkh_encode_drift_into+0x5a>
movsd  0xebb(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    117a <tkh_encode_drift_into+0x5a>
movsd  0xeb5(%rip),%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1040 <lrint@plt>
mov    %eax,%ecx
lea    0x1(%rbx),%rdi
lea    0xea3(%rip),%rdx
mov    $0x9,%ebp
mov    $0x9,%esi
call   1030 <snprintf@plt>
movb   $0x0,0x9(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%08d\00"
define i32 @tkh_encode_drift_into(double %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store double %0, double* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 9
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 -1, i32* %4
br label %40
12: ; preds = %3
%13 = load double, double* %5
%14 = fcmp olt double %13, 0.000000e+00
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i8*, i8** %6
store i8 45, i8* %16
br label %19
17: ; preds = %12
%18 = load i8*, i8** %6
store i8 43, i8* %18
br label %19
19: ; preds = %17, %15
%20 = load double, double* %5
%21 = call double @llvm.fabs.f64(double %20)
store double %21, double* %5
%22 = load double, double* %5
%23 = fcmp oge double %22, 0x3E45798EE5CAE28D
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = load double, double* %5
%26 = fcmp olt double %25, 1.300000e+00
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load double, double* %5
%29 = fdiv double 1.000000e+00, %28
%30 = call i32 @lrint(double %29)
br label %32
31: ; preds = %24, %19
br label %32
32: ; preds = %31, %27
%33 = phi i32 [ %30, %27 ], [ 0, %31 ]
store i32 %33, i32* %8
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = load i32, i32* %8
%37 = call i32 @snprintf(i8* %35, i32 9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 9
store i8 0, i8* %39
store i32 9, i32* %4
br label %40
40: ; preds = %32, %11
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@.str = [5 x i8] c"%08d\00"
define i32 @tkh_encode_drift_into(double %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %2, 9
br i1 %4, label %20, label %5
5: ; preds = %3
%6 = fcmp olt double %0, 0.000000e+00
%7 = select i1 %6, i8 45, i8 43
store i8 %7, i8* %1
%8 = tail call double @llvm.fabs.f64(double %0)
%9 = fcmp oge double %8, 0x3E45798EE5CAE28D
%10 = fcmp olt double %8, 1.300000e+00
%11 = and i1 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = fdiv double 1.000000e+00, %8
%14 = tail call i32 @lrint(double %13)
br label %15
15: ; preds = %5, %12
%16 = phi i32 [ %14, %12 ], [ 0, %5 ]
%17 = getelementptr inbounds i8, i8* %1, i64 1
%18 = tail call i32 @snprintf(i8* %17, i32 9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %16)
%19 = getelementptr inbounds i8, i8* %1, i64 9
store i8 0, i8* %19
br label %20
20: ; preds = %3, %15
%21 = phi i32 [ 9, %15 ], [ -1, %3 ]
ret i32 %21
}