#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int strfromd(char *, int const, char *, double);
int /*<<< orphan*/ strlen(char *);
bool format_double(int total_cols, double value, char *chars, int bytes_c,
                   int *offset, int *cols, FILE *fp) {
  fprintf(stderr, "format_double(%f) offset=%i\n", value, *offset);
  const int max = 20;
  char *format = "%f";
  int actual_c = strfromd(&chars[*offset], max, format, value);
  assert(actual_c < max);
  fprintf(stderr, "chars: %s %zi %i\n", chars, strlen(chars), actual_c);
  *offset += actual_c;
  (*cols)++;
  fprintf(stderr, "cols=%i offset=%i\n", *cols, *offset);
  if (*cols > total_cols) {
    chars[*offset] = '\n';
    *cols = 1;
  } else
    chars[*offset] = ',';
  *offset = *offset + 1;
  if (*offset > bytes_c - max) {
    fprintf(stderr, "write: %i\n", *offset);
    int actual_w = fwrite(chars, sizeof(char), *offset, fp);
    assert(actual_w == *offset);
    *offset = 0;
  }
  return true;
}


x86 assembly (clang, -O0):
<format_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
movsd  -0x10(%rbp),%xmm0
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xe72(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
movl   $0x14,-0x3c(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rdi
mov    -0x48(%rbp),%rdx
movsd  -0x10(%rbp),%xmm0
mov    $0x14,%esi
call   1060 <strfromd@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x14,-0x4c(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rdx
mov    %eax,%ecx
mov    -0x4c(%rbp),%r8d
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x4c(%rbp),%ecx
mov    -0x28(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
jle    127a <format_double+0x12a>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
movl   $0x1,(%rax)
jmp    1289 <format_double+0x139>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq (%rcx),%rcx
movb   $0x2c,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x14,%ecx
cmp    %ecx,%eax
jle    1304 <format_double+0x1b4>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    -0x28(%rbp),%rcx
cmp    (%rcx),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x1,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_double>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x8(%rsp)
mov    %r8,%rbp
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %rsi,%r15
movsd  %xmm0,0x10(%rsp)
mov    %edi,0x4(%rsp)
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    (%rcx),%edx
lea    0xe75(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
movslq (%rbx),%rdi
add    %r15,%rdi
lea    0xe7e(%rip),%rdx
mov    $0x14,%esi
movsd  0x10(%rsp),%xmm0
call   1060 <strfromd@plt>
mov    %eax,%r14d
xor    %edi,%edi
cmp    $0x14,%eax
setl   %dil
call   1040 <assert@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%r13d
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    0xe47(%rip),%rsi
mov    %r13d,%edi
mov    %r15,%rdx
mov    %eax,%ecx
mov    %r14d,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
add    %r14d,(%rbx)
mov    0x2deb(%rip),%r14
mov    0x0(%rbp),%edx
add    $0x1,%edx
mov    %edx,0x0(%rbp)
mov    (%r14),%edi
mov    (%rbx),%ecx
lea    0xe28(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
movslq (%rbx),%rax
mov    0x4(%rsp),%ecx
cmp    %ecx,0x0(%rbp)
jle    122b <format_double+0xdb>
movb   $0xa,(%r15,%rax,1)
movl   $0x1,0x0(%rbp)
jmp    1230 <format_double+0xe0>
movb   $0x2c,(%r15,%rax,1)
mov    (%rbx),%eax
lea    0x1(%rax),%edx
mov    %edx,(%rbx)
add    $0xffffffec,%r12d
cmp    %r12d,%eax
jl     1278 <format_double+0x128>
mov    (%r14),%edi
lea    0xdfb(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edx
mov    %r15,%rdi
mov    $0x1,%esi
mov    0x8(%rsp),%rcx
call   1070 <fwrite@plt>
xor    %edi,%edi
cmp    (%rbx),%eax
sete   %dil
call   1040 <assert@plt>
movl   $0x0,(%rbx)
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"format_double(%f) offset=%i\0A\00"
@.str.1 = [3 x i8] c"%f\00"
@.str.2 = [18 x i8] c"chars: %s %zi %i\0A\00"
@.str.3 = [19 x i8] c"cols=%i offset=%i\0A\00"
@.str.4 = [11 x i8] c"write: %i\0A\00"
define i32 @format_double(i32 %0, double %1, i8* %2, i32 %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca i32
%9 = alloca double
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %8
store double %1, double* %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i32* %4, i32** %12
store i32* %5, i32** %13
store i32* %6, i32** %14
%19 = load i32, i32* @stderr
%20 = load double, double* %9
%21 = load i32*, i32** %12
%22 = load i32, i32* %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), double %20, i32 %22)
store i32 20, i32* %15
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %16
%24 = load i8*, i8** %10
%25 = load i32*, i32** %12
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
%29 = load i8*, i8** %16
%30 = load double, double* %9
%31 = call i32 @strfromd(i8* %28, i32 20, i8* %29, double %30)
store i32 %31, i32* %17
%32 = load i32, i32* %17
%33 = icmp slt i32 %32, 20
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %10
%38 = load i8*, i8** %10
%39 = call i32 @strlen(i8* %38)
%40 = load i32, i32* %17
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 %39, i32 %40)
%42 = load i32, i32* %17
%43 = load i32*, i32** %12
%44 = load i32, i32* %43
%45 = add nsw i32 %44, %42
store i32 %45, i32* %43
%46 = load i32*, i32** %13
%47 = load i32, i32* %46
%48 = add nsw i32 %47, 1
store i32 %48, i32* %46
%49 = load i32, i32* @stderr
%50 = load i32*, i32** %13
%51 = load i32, i32* %50
%52 = load i32*, i32** %12
%53 = load i32, i32* %52
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 %53)
%55 = load i32*, i32** %13
%56 = load i32, i32* %55
%57 = load i32, i32* %8
%58 = icmp sgt i32 %56, %57
br i1 %58, label %59, label %66
59: ; preds = %7
%60 = load i8*, i8** %10
%61 = load i32*, i32** %12
%62 = load i32, i32* %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 10, i8* %64
%65 = load i32*, i32** %13
store i32 1, i32* %65
br label %72
66: ; preds = %7
%67 = load i8*, i8** %10
%68 = load i32*, i32** %12
%69 = load i32, i32* %68
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %67, i64 %70
store i8 44, i8* %71
br label %72
72: ; preds = %66, %59
%73 = load i32*, i32** %12
%74 = load i32, i32* %73
%75 = add nsw i32 %74, 1
%76 = load i32*, i32** %12
store i32 %75, i32* %76
%77 = load i32*, i32** %12
%78 = load i32, i32* %77
%79 = load i32, i32* %11
%80 = sub nsw i32 %79, 20
%81 = icmp sgt i32 %78, %80
br i1 %81, label %82, label %99
82: ; preds = %72
%83 = load i32, i32* @stderr
%84 = load i32*, i32** %12
%85 = load i32, i32* %84
%86 = call i32 (i32, i8*, ...) @fprintf(i32 %83, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %85)
%87 = load i8*, i8** %10
%88 = load i32*, i32** %12
%89 = load i32, i32* %88
%90 = load i32*, i32** %14
%91 = call i32 @fwrite(i8* %87, i32 1, i32 %89, i32* %90)
store i32 %91, i32* %18
%92 = load i32, i32* %18
%93 = load i32*, i32** %12
%94 = load i32, i32* %93
%95 = icmp eq i32 %92, %94
%96 = zext i1 %95 to i32
%97 = call i32 @assert(i32 %96)
%98 = load i32*, i32** %12
store i32 0, i32* %98
br label %99
99: ; preds = %82, %72
ret i32 1
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"format_double(%f) offset=%i\0A\00"
@.str.1 = [3 x i8] c"%f\00"
@.str.2 = [18 x i8] c"chars: %s %zi %i\0A\00"
@.str.3 = [19 x i8] c"cols=%i offset=%i\0A\00"
@.str.4 = [11 x i8] c"write: %i\0A\00"
define i32 @format_double(i32 %0, double %1, i8* %2, i32 %3, i32* nocapture %4, i32* nocapture %5, i32* nocapture %6) {
%8 = load i32, i32* @stderr
%9 = load i32, i32* %4
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), double %1, i32 %9)
%11 = load i32, i32* %4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %2, i64 %12
%14 = tail call i32 @strfromd(i8* %13, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), double %1)
%15 = icmp slt i32 %14, 20
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = load i32, i32* @stderr
%19 = tail call i32 @strlen(i8* %2)
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %2, i32 %19, i32 %14)
%21 = load i32, i32* %4
%22 = add nsw i32 %21, %14
store i32 %22, i32* %4
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
%25 = load i32, i32* @stderr
%26 = load i32, i32* %4
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %26)
%28 = load i32, i32* %5
%29 = icmp sgt i32 %28, %0
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %2, i64 %31
br i1 %29, label %33, label %34
33: ; preds = %7
store i8 10, i8* %32
store i32 1, i32* %5
br label %35
34: ; preds = %7
store i8 44, i8* %32
br label %35
35: ; preds = %34, %33
%36 = load i32, i32* %4
%37 = add nsw i32 %36, 1
store i32 %37, i32* %4
%38 = add nsw i32 %3, -20
%39 = icmp slt i32 %36, %38
br i1 %39, label %49, label %40
40: ; preds = %35
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%43 = load i32, i32* %4
%44 = tail call i32 @fwrite(i8* %2, i32 1, i32 %43, i32* %6)
%45 = load i32, i32* %4
%46 = icmp eq i32 %44, %45
%47 = zext i1 %46 to i32
%48 = tail call i32 @assert(i32 %47)
store i32 0, i32* %4
br label %49
49: ; preds = %40, %35
ret i32 1
}