#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 scalar_t__ uint64_t;
struct histogram {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ fatal_error_with_errno(char *, char const *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, long);
scalar_t__ fputc(char, int /*<<< orphan*/ *);
scalar_t__ hist_count_at(struct histogram const *, long);
int /*<<< orphan*/ xfclose(int /*<<< orphan*/ *, char const *);
int /*<<< orphan*/ *xfopen(char const *, char *);
void write_histogram_file(const char *histogram_file,
                          const struct histogram *hist, long first_nonzero_idx,
                          long last_nonzero_idx, uint64_t max_freq) {
  const double max_num_asterisks = 72;
  double scale = max_num_asterisks / (double)max_freq;
  FILE *fp = xfopen(histogram_file, "w");
  for (long i = first_nonzero_idx; i <= last_nonzero_idx; i++) {
    if (fprintf(fp, "%ld\t", i) < 0)
      goto write_error;
    size_t num_asterisks = (size_t)(scale * (double)hist_count_at(hist, i));
    while (num_asterisks--)
      if (fputc('*', fp) == EOF)
        goto write_error;
    if (fputc('\n', fp) == EOF)
      goto write_error;
  }
  xfclose(fp, histogram_file);
  return;
write_error:
  fatal_error_with_errno("Error writing to \"%s\"", histogram_file);
}


x86 assembly (clang, -O0):
<write_histogram_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movsd  0xe7c(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
cvtsi2sdq -0x28(%rbp),%xmm1
movsd  0xe69(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1030 <xfopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x20(%rbp),%rax
jg     12b6 <write_histogram_file+0x156>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
cmp    $0x0,%rax
jge    11ed <write_histogram_file+0x8d>
jmp    12c8 <write_histogram_file+0x168>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1070 <hist_count_at@plt>
movsd  -0x58(%rbp),%xmm0
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xddf(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x50(%rbp)
cmp    $0x0,%rax
je     127d <write_histogram_file+0x11d>
mov    -0x40(%rbp),%rsi
mov    $0x2a,%edi
call   1050 <fputc@plt>
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
jne    1278 <write_histogram_file+0x118>
jmp    12c8 <write_histogram_file+0x168>
jmp    123c <write_histogram_file+0xdc>
mov    -0x40(%rbp),%rsi
mov    $0xa,%edi
call   1050 <fputc@plt>
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%rax
jne    12a0 <write_histogram_file+0x140>
jmp    12c8 <write_histogram_file+0x168>
jmp    12a5 <write_histogram_file+0x145>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    11bc <write_histogram_file+0x5c>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <xfclose@plt>
jmp    12d8 <write_histogram_file+0x178>
mov    -0x8(%rbp),%rsi
lea    0xd44(%rip),%rdi
call   1040 <fatal_error_with_errno@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_histogram_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbp
lea    0xe8a(%rip),%rsi
call   1030 <xfopen@plt>
mov    %rax,%rbx
cmp    %r15,%r13
jle    11ac <write_histogram_file+0x4c>
mov    %rbx,%rdi
mov    %rbp,%rsi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <xfclose@plt>
cvtsi2sd %r14,%xmm0
movsd  0xe47(%rip),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
lea    0xe48(%rip),%rsi
mov    0x2e17(%rip),%r12
mov    %rbp,%r14
mov    %r15,%rbp
mov    %rbx,%rdi
mov    %r13,%rdx
call   1060 <fprintf@plt>
test   %rax,%rax
js     1285 <write_histogram_file+0x125>
mov    0x10(%rsp),%rdi
mov    %r13,%rsi
call   1070 <hist_count_at@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  0x8(%rsp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xdf2(%rip),%xmm0
cvttsd2si %xmm0,%r15
sar    $0x3f,%rcx
and    %rcx,%r15
or     %rax,%r15
add    $0x1,%r15
nopl   0x0(%rax)
add    $0xffffffffffffffff,%r15
je     1250 <write_histogram_file+0xf0>
mov    $0x2a,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
cmp    (%r12),%rax
jne    1230 <write_histogram_file+0xd0>
jmp    1285 <write_histogram_file+0x125>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
cmp    (%r12),%rax
je     1285 <write_histogram_file+0x125>
lea    0x1(%r13),%rax
mov    %rbp,%r15
cmp    %rbp,%r13
mov    %rax,%r13
lea    0xd9b(%rip),%rsi
mov    %r14,%rbp
jne    11d1 <write_histogram_file+0x71>
jmp    1193 <write_histogram_file+0x33>
lea    0xd8b(%rip),%rdi
mov    %r14,%rsi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fatal_error_with_errno@plt>

LLVM IR (-O0):
%struct.histogram = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"%ld\09\00"
@EOF = global i64 0
@.str.2 = [22 x i8] c"Error writing to \22%s\22\00"
define void @write_histogram_file(i8* %0, %struct.histogram* %1, i64 %2, i64 %3, i64 %4) {
%6 = alloca i8*
%7 = alloca %struct.histogram*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca double
%12 = alloca double
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i64
store i8* %0, i8** %6
store %struct.histogram* %1, %struct.histogram** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i64 %4, i64* %10
store double 7.200000e+01, double* %11
%16 = load i64, i64* %10
%17 = sitofp i64 %16 to double
%18 = fdiv double 7.200000e+01, %17
store double %18, double* %12
%19 = load i8*, i8** %6
%20 = call i32* @xfopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %13
%21 = load i64, i64* %8
store i64 %21, i64* %14
br label %22
22: ; preds = %58, %5
%23 = load i64, i64* %14
%24 = load i64, i64* %9
%25 = icmp sle i64 %23, %24
br i1 %25, label %26, label %61
26: ; preds = %22
%27 = load i32*, i32** %13
%28 = load i64, i64* %14
%29 = call i64 @fprintf(i32* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
br label %65
32: ; preds = %26
%33 = load double, double* %12
%34 = load %struct.histogram*, %struct.histogram** %7
%35 = load i64, i64* %14
%36 = call i64 @hist_count_at(%struct.histogram* %34, i64 %35)
%37 = sitofp i64 %36 to double
%38 = fmul double %33, %37
%39 = fptoui double %38 to i64
store i64 %39, i64* %15
br label %40
40: ; preds = %50, %32
%41 = load i64, i64* %15
%42 = add i64 %41, -1
store i64 %42, i64* %15
%43 = icmp ne i64 %41, 0
br i1 %43, label %44, label %51
44: ; preds = %40
%45 = load i32*, i32** %13
%46 = call i64 @fputc(i8 signext 42, i32* %45)
%47 = load i64, i64* @EOF
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %44
br label %65
50: ; preds = %44
br label %40
51: ; preds = %40
%52 = load i32*, i32** %13
%53 = call i64 @fputc(i8 signext 10, i32* %52)
%54 = load i64, i64* @EOF
%55 = icmp eq i64 %53, %54
br i1 %55, label %56, label %57
56: ; preds = %51
br label %65
57: ; preds = %51
br label %58
58: ; preds = %57
%59 = load i64, i64* %14
%60 = add nsw i64 %59, 1
store i64 %60, i64* %14
br label %22
61: ; preds = %22
%62 = load i32*, i32** %13
%63 = load i8*, i8** %6
%64 = call i32 @xfclose(i32* %62, i8* %63)
br label %68
65: ; preds = %56, %49, %31
%66 = load i8*, i8** %6
%67 = call i32 @fatal_error_with_errno(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %66)
br label %68
68: ; preds = %65, %61
ret void
}

LLVM IR (-O2):
%struct.histogram = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [5 x i8] c"%ld\09\00"
@EOF = global i64 0
@.str.2 = [22 x i8] c"Error writing to \22%s\22\00"
define void @write_histogram_file(i8* %0, %struct.histogram* %1, i64 %2, i64 %3, i64 %4) {
%6 = sitofp i64 %4 to double
%7 = fdiv double 7.200000e+01, %6
%8 = tail call i32* @xfopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp sgt i64 %2, %3
br i1 %9, label %34, label %13
10: ; preds = %30
%11 = add i64 %14, 1
%12 = icmp eq i64 %14, %3
br i1 %12, label %34, label %13
13: ; preds = %5, %10
%14 = phi i64 [ %11, %10 ], [ %2, %5 ]
%15 = tail call i64 @fprintf(i32* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %36, label %17
17: ; preds = %13
%18 = tail call i64 @hist_count_at(%struct.histogram* %1, i64 %14)
%19 = sitofp i64 %18 to double
%20 = fmul double %7, %19
%21 = fptoui double %20 to i64
br label %22
22: ; preds = %25, %17
%23 = phi i64 [ %21, %17 ], [ %26, %25 ]
%24 = icmp eq i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = add i64 %23, -1
%27 = tail call i64 @fputc(i8 signext 42, i32* %8)
%28 = load i64, i64* @EOF
%29 = icmp eq i64 %27, %28
br i1 %29, label %36, label %22
30: ; preds = %22
%31 = tail call i64 @fputc(i8 signext 10, i32* %8)
%32 = load i64, i64* @EOF
%33 = icmp eq i64 %31, %32
br i1 %33, label %36, label %10
34: ; preds = %10, %5
%35 = tail call i32 @xfclose(i32* %8, i8* %0)
br label %38
36: ; preds = %30, %13, %25
%37 = tail call i32 @fatal_error_with_errno(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %38
38: ; preds = %36, %34
ret void
}