#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 */
int /*<<< orphan*/ fatal_error_with_errno(char *, char const *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, long, unsigned long);
unsigned long hist_count_at(struct histogram const *, long);
int /*<<< orphan*/ xfclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *xfopen(char const *, char *);
void write_hist_file(const char *hist_file, const struct histogram *hist,
                     long first_nonzero_idx, long last_nonzero_idx) {
  FILE *fp = xfopen(hist_file, "w");
  for (long i = first_nonzero_idx; i <= last_nonzero_idx; i++) {
    unsigned long count = hist_count_at(hist, i);
    if (count != 0) {
      if (fprintf(fp, "%ld\t%zu\n", i, count) < 0) {
        fatal_error_with_errno("Error writing to "
                               "the file \"%s\"",
                               hist_file);
      }
    }
  }
  xfclose(fp);
}


x86 assembly (clang, -O0):
<write_hist_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1030 <xfopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jg     11fb <write_hist_file+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1060 <hist_count_at@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11e5 <write_hist_file+0x95>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rcx
lea    0xe41(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    11e0 <write_hist_file+0x90>
mov    -0x8(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1040 <fatal_error_with_errno@plt>
jmp    11e5 <write_hist_file+0x95>
jmp    11ea <write_hist_file+0x9a>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1184 <write_hist_file+0x34>
mov    -0x28(%rbp),%rdi
call   1070 <xfclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_hist_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xe92(%rip),%rsi
call   1030 <xfopen@plt>
mov    %rax,%r13
cmp    %r14,%rbx
jle    1191 <write_hist_file+0x41>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <xfclose@plt>
add    $0x1,%r14
lea    0xe66(%rip),%rbp
jmp    11a9 <write_hist_file+0x59>
xchg   %ax,%ax
add    $0x1,%rbx
cmp    %rbx,%r14
je     117b <write_hist_file+0x2b>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <hist_count_at@plt>
test   %rax,%rax
je     11a0 <write_hist_file+0x50>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
test   %rax,%rax
jns    11a0 <write_hist_file+0x50>
lea    0xe35(%rip),%rdi
mov    %r12,%rsi
call   1040 <fatal_error_with_errno@plt>
jmp    11a0 <write_hist_file+0x50>

LLVM IR (-O0):
%struct.histogram = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [9 x i8] c"%ld\09%zu\0A\00"
@.str.2 = [31 x i8] c"Error writing to the file \22%s\22\00"
define void @write_hist_file(i8* %0, %struct.histogram* %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca %struct.histogram*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store %struct.histogram* %1, %struct.histogram** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%12 = load i8*, i8** %5
%13 = call i32* @xfopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
%14 = load i64, i64* %7
store i64 %14, i64* %10
br label %15
15: ; preds = %36, %4
%16 = load i64, i64* %10
%17 = load i64, i64* %8
%18 = icmp sle i64 %16, %17
br i1 %18, label %19, label %39
19: ; preds = %15
%20 = load %struct.histogram*, %struct.histogram** %6
%21 = load i64, i64* %10
%22 = call i64 @hist_count_at(%struct.histogram* %20, i64 %21)
store i64 %22, i64* %11
%23 = load i64, i64* %11
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %19
%26 = load i32*, i32** %9
%27 = load i64, i64* %10
%28 = load i64, i64* %11
%29 = call i64 @fprintf(i32* %26, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %27, i64 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = load i8*, i8** %5
%33 = call i32 @fatal_error_with_errno(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %25
br label %35
35: ; preds = %34, %19
br label %36
36: ; preds = %35
%37 = load i64, i64* %10
%38 = add nsw i64 %37, 1
store i64 %38, i64* %10
br label %15
39: ; preds = %15
%40 = load i32*, i32** %9
%41 = call i32 @xfclose(i32* %40)
ret void
}

LLVM IR (-O2):
%struct.histogram = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [9 x i8] c"%ld\09%zu\0A\00"
@.str.2 = [31 x i8] c"Error writing to the file \22%s\22\00"
define void @write_hist_file(i8* %0, %struct.histogram* %1, i64 %2, i64 %3) {
%5 = tail call i32* @xfopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp sgt i64 %2, %3
br i1 %6, label %7, label %9
7: ; preds = %18, %4
%8 = tail call i32 @xfclose(i32* %5)
ret void
9: ; preds = %4, %18
%10 = phi i64 [ %19, %18 ], [ %2, %4 ]
%11 = tail call i64 @hist_count_at(%struct.histogram* %1, i64 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = tail call i64 @fprintf(i32* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %10, i64 %11)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = 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 %18
18: ; preds = %13, %16, %9
%19 = add i64 %10, 1
%20 = icmp eq i64 %10, %3
br i1 %20, label %7, label %9
}