#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*/ sprintf(char *, char *, double);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *fmt(double g) {
  static char buf[10][30];
  static int n;
  char *p;
  p = buf[n++];
  if (n == 10) {
    n = 0;
  }
  sprintf(p, "%g", g);
  if (strcmp(p, "0") == 0) {
    strcpy(p, "zero");
    return p;
  }
  if (strcmp(p, "-0") == 0) {
    strcpy(p, "-zero");
    return p;
  }
  return p;
}


x86 assembly (clang, -O0):
<fmt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    0x3039(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x302e(%rip)
movslq %eax,%rcx
lea    0x2ef8(%rip),%rax
imul   $0x1e,%rcx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0xa,0x3012(%rip)
jne    117a <fmt+0x4a>
movl   $0x0,0x3002(%rip)
mov    -0x18(%rbp),%rdi
movsd  -0x10(%rbp),%xmm0
lea    0xe76(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11c6 <fmt+0x96>
mov    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1205 <fmt+0xd5>
mov    -0x18(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11fd <fmt+0xcd>
mov    -0x18(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1205 <fmt+0xd5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt>:
push   %rbx
movslq 0x3044(%rip),%rax
lea    0x1(%rax),%ecx
mov    %rax,%rdx
shl    $0x5,%rdx
sub    %rax,%rdx
sub    %rax,%rdx
lea    0x2f01(%rip),%rbx
add    %rdx,%rbx
xor    %eax,%eax
cmp    $0xa,%ecx
cmovne %ecx,%eax
mov    %eax,0x301c(%rip)
lea    0xe99(%rip),%rsi
mov    %rbx,%rdi
call   1050 <sprintf@plt>
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11a0 <fmt+0x70>
lea    0xe79(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
jne    11af <fmt+0x7f>
lea    0xe68(%rip),%rsi
jmp    11a7 <fmt+0x77>
lea    0xe60(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@fmt.buf = internal global [10 x [30 x i8]] zeroinitializer
@fmt.n = internal global i32 0
@.str = [3 x i8] c"%g\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [5 x i8] c"zero\00"
@.str.3 = [3 x i8] c"-0\00"
@.str.4 = [6 x i8] c"-zero\00"
define i8* @fmt(double %0) {
%2 = alloca i8*
%3 = alloca double
%4 = alloca i8*
store double %0, double* %3
%5 = load i32, i32* @fmt.n
%6 = add nsw i32 %5, 1
store i32 %6, i32* @fmt.n
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds [10 x [30 x i8]], [10 x [30 x i8]]* @fmt.buf, i64 0, i64 %7
%9 = getelementptr inbounds [30 x i8], [30 x i8]* %8, i64 0, i64 0
store i8* %9, i8** %4
%10 = load i32, i32* @fmt.n
%11 = icmp eq i32 %10, 10
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 0, i32* @fmt.n
br label %13
13: ; preds = %12, %1
%14 = load i8*, i8** %4
%15 = load double, double* %3
%16 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %15)
%17 = load i8*, i8** %4
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = load i8*, i8** %4
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%23 = load i8*, i8** %4
store i8* %23, i8** %2
br label %34
24: ; preds = %13
%25 = load i8*, i8** %4
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i8*, i8** %4
%30 = call i32 @strcpy(i8* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%31 = load i8*, i8** %4
store i8* %31, i8** %2
br label %34
32: ; preds = %24
%33 = load i8*, i8** %4
store i8* %33, i8** %2
br label %34
34: ; preds = %32, %28, %20
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
@fmt.buf = internal global [10 x [30 x i8]] zeroinitializer
@fmt.n = internal unnamed_addr global i32 0
@.str = [3 x i8] c"%g\00"
@.str.1 = [2 x i8] c"0\00"
@.str.2 = [5 x i8] c"zero\00"
@.str.3 = [3 x i8] c"-0\00"
@.str.4 = [6 x i8] c"-zero\00"
define i8* @fmt(double %0) {
%2 = load i32, i32* @fmt.n
%3 = add nsw i32 %2, 1
%4 = sext i32 %2 to i64
%5 = getelementptr inbounds [10 x [30 x i8]], [10 x [30 x i8]]* @fmt.buf, i64 0, i64 %4, i64 0
%6 = icmp eq i32 %3, 10
%7 = select i1 %6, i32 0, i32 %3
store i32 %7, i32* @fmt.n
%8 = tail call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), double %0)
%9 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11, %1
%15 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %1 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %11 ]
%16 = tail call i32 @strcpy(i8* %5, i8* %15)
br label %17
17: ; preds = %14, %11
ret i8* %5
}