char *engineer_notation(d, p)
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 = 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%*.*lfe%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    12d9 <engineer_notation+0x1a9>
movsd  -0x8(%rbp),%xmm0
call   1040 <log10@plt>
cvttsd2si %xmm0,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    121b <engineer_notation+0xeb>
movsd  0xe02(%rip),%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    121b <engineer_notation+0xeb>
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    1235 <engineer_notation+0x105>
movb   $0x2d,0x2e25(%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  0xdbc(%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    1274 <engineer_notation+0x144>
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1279 <engineer_notation+0x149>
mov    -0x1c(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     12d4 <engineer_notation+0x1a4>
movsd  0xd73(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
movsbl 0x2da2(%rip),%eax
cmp    $0x2d,%eax
jne    12c6 <engineer_notation+0x196>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12cf <engineer_notation+0x19f>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1279 <engineer_notation+0x149>
jmp    12d9 <engineer_notation+0x1a9>
movsbl 0x2d70(%rip),%edx
mov    -0xc(%rbp),%ecx
mov    -0xc(%rbp),%r8d
movsd  -0x18(%rbp),%xmm0
movsbl 0x2d5d(%rip),%r9d
mov    -0x1c(%rbp),%eax
lea    0x2d62(%rip),%rdi
lea    0xd0b(%rip),%rsi
movl   $0x3,(%rsp)
movl   $0x3,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x1,%al
call   1050 <sprintf@plt>
lea    0x2d3a(%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,%ebx
cmovge %eax,%ebx
movb   $0x2b,0x2ef1(%rip)
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    1179 <engineer_notation+0x49>
movb   $0x2d,0x2edf(%rip)
xorpd  0xe87(%rip),%xmm1
lea    -0x1(%rbx),%ebp
ucomisd %xmm0,%xmm1
jne    118d <engineer_notation+0x5d>
jp     118d <engineer_notation+0x5d>
xor    %ebx,%ebx
mov    $0x2b,%al
jmp    1251 <engineer_notation+0x121>
movapd %xmm1,%xmm0
movapd %xmm1,(%rsp)
call   1040 <log10@plt>
cvttsd2si %xmm0,%eax
test   %eax,%eax
sete   %cl
movsd  0xe64(%rip),%xmm0
ucomisd (%rsp),%xmm0
seta   %dl
add    $0xfffffffe,%ebx
test   %cl,%dl
cmovne %ebx,%ebp
mov    $0xffffffff,%r14d
cmove  %eax,%r14d
test   %r14d,%r14d
jns    11d6 <engineer_notation+0xa6>
movb   $0x2d,0x2e7f(%rip)
add    $0xffffffff,%r14d
xorps  %xmm1,%xmm1
cvtsi2sd %r14d,%xmm1
movsd  0xe32(%rip),%xmm0
call   1030 <pow@plt>
mov    %r14d,%ebx
neg    %ebx
cmovs  %r14d,%ebx
movapd (%rsp),%xmm1
divsd  %xmm0,%xmm1
imul   $0xaaaaaaab,%ebx,%ecx
mov    0x2e48(%rip),%al
cmp    $0x55555556,%ecx
jb     124d <engineer_notation+0x11d>
xor    %ecx,%ecx
cmp    $0x2d,%al
sete   %cl
add    %ecx,%ecx
add    $0xffffffff,%ecx
movsd  0xdf3(%rip),%xmm0
cs nopw 0x0(%rax,%rax,1)
nop
add    %ecx,%ebx
mulsd  %xmm0,%xmm1
add    $0xffffffff,%ebp
imul   $0xaaaaaaab,%ebx,%edx
add    $0x2aaaaaaa,%edx
cmp    $0x55555554,%edx
ja     1230 <engineer_notation+0x100>
movapd %xmm1,%xmm0
movzbl 0x2df8(%rip),%edx
movzbl %al,%r9d
sub    $0x8,%rsp
lea    0x2df9(%rip),%r14
lea    0xdb2(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%ecx
mov    %ebp,%r8d
mov    $0x1,%al
push   %rbx
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 = [17 x i8] c"%c%*.*lfe%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 double @log10(double %27)
%29 = fptosi double %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 = sext i8 %79 to i32
%81 = load i32, i32* %4
%82 = load i32, i32* %4
%83 = load double, double* %5
%84 = load i8, i8* @engineer_notation.expsign
%85 = sext i8 %84 to i32
%86 = load i32, i32* %6
%87 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @engineer_notation.output, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %80, i32 %81, i32 %82, double %83, i32 %85, i32 3, i32 3, i32 %86)
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 = [17 x i8] c"%c%*.*lfe%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 double @log10(double %12)
%16 = fptosi double %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 = zext i8 %52 to i32
%54 = zext i8 %48 to i32
%55 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @engineer_notation.output, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %53, i32 %49, i32 %49, double %50, i32 %54, i32 3, i32 3, i32 %51)
ret i8* getelementptr inbounds ([27 x i8], [27 x i8]* @engineer_notation.output, i64 0, i64 0)
}