#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__ log10(double);
double pow(double, double);
int /*<<< orphan*/ sprintf(char *, char *, char, int, int, double, char, int,
                           int, int);
char *engineer_notation(double d, int p) {
  static char mansign;
  static char expsign;
  static char output[1 + 20 + 1 + 1 + 3 + 1];
  double mantissa;
  int exponent;
  mansign = '+';
  expsign = '+';
  if (p > 20)
    p = 20;
  if (p < 3)
    p = 3;
  --p;
  if (d < 0.0) {
    mansign = '-';
    d = -d;
  }
  if (d == 0.0) {
    exponent = 0;
    mantissa = 0;
  } else {
    exponent = (int)log10(d);
    if (exponent == 0 && d < 1.0) {
      --exponent;
      --p;
    }
    if (exponent < 0) {
      expsign = '-';
      --exponent;
    }
    mantissa = d / pow(10.0, (double)exponent);
    if (exponent < 0)
      exponent = -exponent;
    while (exponent % 3 != 0) {
      mantissa *= 10.0;
      p--;
      if (expsign == '-')
        ++exponent;
      else
        --exponent;
    }
  }
  sprintf(output, "%c%*.*fe%c%*.*d", mansign, p, p, mantissa, expsign, 3, 3,
          exponent);
  return (output);
}


x86 assembly (clang, -O0):
<engineer_notation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movb   $0x2b,0x2f09(%rip)
movb   $0x2b,0x2f03(%rip)
cmpl   $0x14,-0xc(%rbp)
jle    115f <engineer_notation+0x2f>
movl   $0x14,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    1170 <engineer_notation+0x40>
movl   $0x3,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11af <engineer_notation+0x7f>
movb   $0x2d,0x2ec2(%rip)
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11db <engineer_notation+0xab>
jp     11db <engineer_notation+0xab>
movl   $0x0,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    12d5 <engineer_notation+0x1a5>
movsd  -0x8(%rbp),%xmm0
call   1040 <log10@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1217 <engineer_notation+0xe7>
movsd  0xe06(%rip),%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    1217 <engineer_notation+0xe7>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1231 <engineer_notation+0x101>
movb   $0x2d,0x2e29(%rip)
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm1
movsd  0xdc0(%rip),%xmm0
call   1030 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1270 <engineer_notation+0x140>
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1275 <engineer_notation+0x145>
mov    -0x1c(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     12d0 <engineer_notation+0x1a0>
movsd  0xd77(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
movsbl 0x2da6(%rip),%eax
cmp    $0x2d,%eax
jne    12c2 <engineer_notation+0x192>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12cb <engineer_notation+0x19b>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1275 <engineer_notation+0x145>
jmp    12d5 <engineer_notation+0x1a5>
mov    0x2d75(%rip),%dl
mov    -0xc(%rbp),%ecx
mov    -0xc(%rbp),%r8d
movsd  -0x18(%rbp),%xmm0
mov    0x2d63(%rip),%r9b
mov    -0x1c(%rbp),%eax
lea    0x2d68(%rip),%rdi
lea    0xd11(%rip),%rsi
movsbl %dl,%edx
movsbl %r9b,%r9d
movl   $0x3,(%rsp)
movl   $0x3,0x8(%rsp)
mov    %eax,0x10(%rsp)
call   1050 <sprintf@plt>
lea    0x2d3b(%rip),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<engineer_notation>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movapd %xmm0,%xmm1
movb   $0x2b,0x2f0d(%rip)
cmp    $0x14,%edi
mov    $0x14,%eax
cmovl  %edi,%eax
cmp    $0x4,%eax
mov    $0x3,%r14d
cmovge %eax,%r14d
movb   $0x2b,0x2eef(%rip)
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    117b <engineer_notation+0x4b>
movb   $0x2d,0x2edd(%rip)
xorpd  0xe85(%rip),%xmm1
lea    -0x1(%r14),%ebx
ucomisd %xmm0,%xmm1
jne    1190 <engineer_notation+0x60>
jp     1190 <engineer_notation+0x60>
xor    %eax,%eax
mov    $0x2b,%cl
jmp    1241 <engineer_notation+0x111>
movapd %xmm1,%xmm0
movapd %xmm1,(%rsp)
call   1040 <log10@plt>
test   %eax,%eax
sete   %cl
movsd  0xe65(%rip),%xmm0
ucomisd (%rsp),%xmm0
seta   %dl
add    $0xfffffffe,%r14d
test   %cl,%dl
cmovne %r14d,%ebx
mov    $0xffffffff,%ebp
cmove  %eax,%ebp
test   %ebp,%ebp
jns    11d3 <engineer_notation+0xa3>
movb   $0x2d,0x2e81(%rip)
add    $0xffffffff,%ebp
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
movsd  0xe36(%rip),%xmm0
call   1030 <pow@plt>
mov    %ebp,%eax
neg    %eax
cmovs  %ebp,%eax
movapd (%rsp),%xmm1
divsd  %xmm0,%xmm1
imul   $0xaaaaaaab,%eax,%edx
mov    0x2e4e(%rip),%cl
cmp    $0x55555556,%edx
jb     123d <engineer_notation+0x10d>
xor    %edx,%edx
cmp    $0x2d,%cl
sete   %dl
add    %edx,%edx
add    $0xffffffff,%edx
movsd  0xdf8(%rip),%xmm0
add    %edx,%eax
mulsd  %xmm0,%xmm1
add    $0xffffffff,%ebx
imul   $0xaaaaaaab,%eax,%esi
add    $0x2aaaaaaa,%esi
cmp    $0x55555554,%esi
ja     1220 <engineer_notation+0xf0>
movapd %xmm1,%xmm0
movsbl 0x2e08(%rip),%edx
sub    $0x8,%rsp
movsbl %cl,%r9d
lea    0x2e09(%rip),%r14
lea    0xdc2(%rip),%rsi
mov    %r14,%rdi
mov    %ebx,%ecx
mov    %ebx,%r8d
push   %rax
push   $0x3
push   $0x3
call   1050 <sprintf@plt>
add    $0x20,%rsp
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@engineer_notation.mansign = internal global i8 0
@engineer_notation.expsign = internal global i8 0
@engineer_notation.output = internal global [27 x i8] zeroinitializer
@.str = [16 x i8] c"%c%*.*fe%c%*.*d\00"
define i8* @engineer_notation(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
store double %0, double* %3
store i32 %1, i32* %4
store i8 43, i8* @engineer_notation.mansign
store i8 43, i8* @engineer_notation.expsign
%7 = load i32, i32* %4
%8 = icmp sgt i32 %7, 20
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 20, i32* %4
br label %10
10: ; preds = %9, %2
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %14
13: ; preds = %10
store i32 3, i32* %4
br label %14
14: ; preds = %13, %10
%15 = load i32, i32* %4
%16 = add nsw i32 %15, -1
store i32 %16, i32* %4
%17 = load double, double* %3
%18 = fcmp olt double %17, 0.000000e+00
br i1 %18, label %19, label %22
19: ; preds = %14
store i8 45, i8* @engineer_notation.mansign
%20 = load double, double* %3
%21 = fneg double %20
store double %21, double* %3
br label %22
22: ; preds = %19, %14
%23 = load double, double* %3
%24 = fcmp oeq double %23, 0.000000e+00
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 0, i32* %6
store double 0.000000e+00, double* %5
br label %78
26: ; preds = %22
%27 = load double, double* %3
%28 = call i64 @log10(double %27)
%29 = trunc i64 %28 to i32
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %26
%33 = load double, double* %3
%34 = fcmp olt double %33, 1.000000e+00
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* %6
%37 = add nsw i32 %36, -1
store i32 %37, i32* %6
%38 = load i32, i32* %4
%39 = add nsw i32 %38, -1
store i32 %39, i32* %4
br label %40
40: ; preds = %35, %32, %26
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
store i8 45, i8* @engineer_notation.expsign
%44 = load i32, i32* %6
%45 = add nsw i32 %44, -1
store i32 %45, i32* %6
br label %46
46: ; preds = %43, %40
%47 = load double, double* %3
%48 = load i32, i32* %6
%49 = sitofp i32 %48 to double
%50 = call double @pow(double 1.000000e+01, double %49)
%51 = fdiv double %47, %50
store double %51, double* %5
%52 = load i32, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = load i32, i32* %6
%56 = sub nsw i32 0, %55
store i32 %56, i32* %6
br label %57
57: ; preds = %54, %46
br label %58
58: ; preds = %76, %57
%59 = load i32, i32* %6
%60 = srem i32 %59, 3
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %77
62: ; preds = %58
%63 = load double, double* %5
%64 = fmul double %63, 1.000000e+01
store double %64, double* %5
%65 = load i32, i32* %4
%66 = add nsw i32 %65, -1
store i32 %66, i32* %4
%67 = load i8, i8* @engineer_notation.expsign
%68 = sext i8 %67 to i32
%69 = icmp eq i32 %68, 45
br i1 %69, label %70, label %73
70: ; preds = %62
%71 = load i32, i32* %6
%72 = add nsw i32 %71, 1
store i32 %72, i32* %6
br label %76
73: ; preds = %62
%74 = load i32, i32* %6
%75 = add nsw i32 %74, -1
store i32 %75, i32* %6
br label %76
76: ; preds = %73, %70
br label %58
77: ; preds = %58
br label %78
78: ; preds = %77, %25
%79 = load i8, i8* @engineer_notation.mansign
%80 = load i32, i32* %4
%81 = load i32, i32* %4
%82 = load double, double* %5
%83 = load i8, i8* @engineer_notation.expsign
%84 = load i32, i32* %6
%85 = call i32 @sprintf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @engineer_notation.output, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8 signext %79, i32 %80, i32 %81, double %82, i8 signext %83, i32 3, i32 3, i32 %84)
ret i8* getelementptr inbounds ([27 x i8], [27 x i8]* @engineer_notation.output, i64 0, i64 0)
}

LLVM IR (-O2):
@engineer_notation.mansign = internal unnamed_addr global i8 0
@engineer_notation.expsign = internal unnamed_addr global i8 0
@engineer_notation.output = internal global [27 x i8] zeroinitializer
@.str = [16 x i8] c"%c%*.*fe%c%*.*d\00"
define i8* @engineer_notation(double %0, i32 %1) {
store i8 43, i8* @engineer_notation.mansign
store i8 43, i8* @engineer_notation.expsign
%3 = icmp slt i32 %1, 20
%4 = select i1 %3, i32 %1, i32 20
%5 = icmp sgt i32 %4, 3
%6 = select i1 %5, i32 %4, i32 3
%7 = add nsw i32 %6, -1
%8 = fcmp olt double %0, 0.000000e+00
br i1 %8, label %9, label %11
9: ; preds = %2
store i8 45, i8* @engineer_notation.mansign
%10 = fneg double %0
br label %11
11: ; preds = %9, %2
%12 = phi double [ %10, %9 ], [ %0, %2 ]
%13 = fcmp oeq double %12, 0.000000e+00
br i1 %13, label %47, label %14
14: ; preds = %11
%15 = tail call i64 @log10(double %12)
%16 = trunc i64 %15 to i32
%17 = icmp eq i32 %16, 0
%18 = fcmp olt double %12, 1.000000e+00
%19 = and i1 %18, %17
%20 = add nsw i32 %6, -2
%21 = select i1 %19, i32 %20, i32 %7
%22 = select i1 %19, i32 -1, i32 %16
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %14
store i8 45, i8* @engineer_notation.expsign
%25 = add nsw i32 %22, -1
br label %26
26: ; preds = %24, %14
%27 = phi i32 [ %25, %24 ], [ %22, %14 ]
%28 = sitofp i32 %27 to double
%29 = tail call double @pow(double 1.000000e+01, double %28)
%30 = fdiv double %12, %29
%31 = tail call i32 @llvm.abs.i32(i32 %27, i1 true)
%32 = urem i32 %31, 3
%33 = icmp eq i32 %32, 0
%34 = load i8, i8* @engineer_notation.expsign
br i1 %33, label %47, label %35
35: ; preds = %26
%36 = icmp eq i8 %34, 45
%37 = select i1 %36, i32 1, i32 -1
br label %38
38: ; preds = %35, %38
%39 = phi i32 [ %31, %35 ], [ %44, %38 ]
%40 = phi double [ %30, %35 ], [ %42, %38 ]
%41 = phi i32 [ %21, %35 ], [ %43, %38 ]
%42 = fmul double %40, 1.000000e+01
%43 = add nsw i32 %41, -1
%44 = add nsw i32 %37, %39
%45 = srem i32 %44, 3
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %38
47: ; preds = %38, %26, %11
%48 = phi i8 [ 43, %11 ], [ %34, %26 ], [ %34, %38 ]
%49 = phi i32 [ %7, %11 ], [ %21, %26 ], [ %43, %38 ]
%50 = phi double [ 0.000000e+00, %11 ], [ %30, %26 ], [ %42, %38 ]
%51 = phi i32 [ 0, %11 ], [ %31, %26 ], [ %44, %38 ]
%52 = load i8, i8* @engineer_notation.mansign
%53 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @engineer_notation.output, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8 signext %52, i32 %49, i32 %49, double %50, i8 signext %48, i32 3, i32 3, i32 %51)
ret i8* getelementptr inbounds ([27 x i8], [27 x i8]* @engineer_notation.output, i64 0, i64 0)
}