#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 */
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*/ *);
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, unsigned long 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);
  return;
write_error:
  fatal_error_with_errno("Error writing to the file \"%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)
movabs $0x4052000000000000,%rax
mov    %rax,-0x30(%rbp)
movaps 0xe7f(%rip),%xmm0
movq   -0x28(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xe7e(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe47(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe63(%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     12d8 <write_histogram_file+0x178>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rdx
lea    0xe37(%rip),%rsi
call   1060 <fprintf@plt>
cmp    $0x0,%rax
jge    120f <write_histogram_file+0xaf>
jmp    12e6 <write_histogram_file+0x186>
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  0xdbd(%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     129f <write_histogram_file+0x13f>
mov    -0x40(%rbp),%rsi
mov    $0x2a,%edi
call   1050 <fputc@plt>
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
jne    129a <write_histogram_file+0x13a>
jmp    12e6 <write_histogram_file+0x186>
jmp    125e <write_histogram_file+0xfe>
mov    -0x40(%rbp),%rsi
mov    $0xa,%edi
call   1050 <fputc@plt>
mov    0x2d34(%rip),%rcx
cmp    (%rcx),%rax
jne    12c2 <write_histogram_file+0x162>
jmp    12e6 <write_histogram_file+0x186>
jmp    12c7 <write_histogram_file+0x167>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    11de <write_histogram_file+0x7e>
mov    -0x40(%rbp),%rdi
call   1080 <xfclose@plt>
jmp    12f6 <write_histogram_file+0x196>
mov    -0x8(%rbp),%rsi
lea    0xd46(%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,%rbp
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,0x10(%rsp)
lea    0xead(%rip),%rsi
mov    %rdi,0x8(%rsp)
call   1030 <xfopen@plt>
mov    %rax,%rbx
cmp    %r12,%r13
jle    11ab <write_histogram_file+0x4b>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <xfclose@plt>
movq   %rbp,%xmm0
punpckldq 0xe48(%rip),%xmm0
subpd  0xe50(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe4c(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,(%rsp)
lea    0xe4e(%rip),%rsi
mov    0x2dfd(%rip),%rbp
mov    %rbx,%rdi
mov    %rsi,%r14
mov    %r13,%rdx
call   1060 <fprintf@plt>
test   %rax,%rax
js     128b <write_histogram_file+0x12b>
mov    0x10(%rsp),%rdi
mov    %r13,%rsi
call   1070 <hist_count_at@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
mulsd  (%rsp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,%rcx
subsd  0xdfc(%rip),%xmm0
cvttsd2si %xmm0,%r15
sar    $0x3f,%rcx
and    %rcx,%r15
or     %rax,%r15
add    $0x1,%r15
nop
add    $0xffffffffffffffff,%r15
je     1260 <write_histogram_file+0x100>
mov    $0x2a,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
cmp    0x0(%rbp),%rax
jne    1240 <write_histogram_file+0xe0>
jmp    128b <write_histogram_file+0x12b>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
cmp    0x0(%rbp),%rax
je     128b <write_histogram_file+0x12b>
lea    0x1(%r13),%rax
cmp    %r12,%r13
mov    %rax,%r13
mov    %r14,%rsi
jne    11eb <write_histogram_file+0x8b>
jmp    1195 <write_histogram_file+0x35>
lea    0xda5(%rip),%rdi
mov    0x8(%rsp),%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 = [31 x i8] c"Error writing to the file \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 = uitofp 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 %64
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 %64
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 %64
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 = call i32 @xfclose(i32* %62)
br label %67
64: ; preds = %56, %49, %31
%65 = load i8*, i8** %6
%66 = call i32 @fatal_error_with_errno(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %64, %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 = [31 x i8] c"Error writing to the file \22%s\22\00"
define void @write_histogram_file(i8* %0, %struct.histogram* %1, i64 %2, i64 %3, i64 %4) {
%6 = uitofp 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)
br label %38
36: ; preds = %30, %13, %25
%37 = tail call i32 @fatal_error_with_errno(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %38
38: ; preds = %36, %34
ret void
}