#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, double);
void write_file(char const *fn, double *buf, int nrow, int ncol) {
  FILE *fp = fopen(fn, "wt");
  for (int i = 0; i < nrow; ++i) {
    for (int j = 0; j + 1 < ncol; ++j) {
      fprintf(fp, "%3.4f\t", *buf);
      ++buf;
    }
    fprintf(fp, "%.6g\n", *buf);
    ++buf;
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e7 <write_file+0xb7>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
cmp    -0x18(%rbp),%eax
jge    11b5 <write_file+0x85>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
lea    0xe6d(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1174 <write_file+0x44>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
lea    0xe42(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1161 <write_file+0x31>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    %edx,%r14d
mov    %rsi,%rbx
lea    0xeb0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbp
mov    %r14d,0xc(%rsp)
test   %r14d,%r14d
jle    11cb <write_file+0x9b>
mov    0x10(%rsp),%rax
lea    -0x1(%rax),%r15d
xor    %r14d,%r14d
lea    0xe8e(%rip),%r12
jmp    11a2 <write_file+0x72>
nopw   0x0(%rax,%rax,1)
movsd  (%rbx),%xmm0
mov    %rbp,%rdi
lea    0xe7c(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x8,%rbx
add    $0x1,%r14d
cmp    0xc(%rsp),%r14d
je     11cb <write_file+0x9b>
mov    %r15d,%r13d
cmpl   $0x2,0x10(%rsp)
jl     1180 <write_file+0x50>
nopl   0x0(%rax)
movsd  (%rbx),%xmm0
mov    %rbp,%rdi
mov    %r12,%rsi
call   1040 <fprintf@plt>
add    $0x8,%rbx
add    $0xffffffff,%r13d
jne    11b0 <write_file+0x80>
jmp    1180 <write_file+0x50>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wt\00"
@.str.1 = [7 x i8] c"%3.4f\09\00"
@.str.2 = [6 x i8] c"%.6g\0A\00"
define void @write_file(i8* %0, double* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store double* %1, double** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
store i32 0, i32* %10
br label %14
14: ; preds = %41, %4
%15 = load i32, i32* %10
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %44
18: ; preds = %14
store i32 0, i32* %11
br label %19
19: ; preds = %31, %18
%20 = load i32, i32* %11
%21 = add nsw i32 %20, 1
%22 = load i32, i32* %8
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i32*, i32** %9
%26 = load double*, double** %6
%27 = load double, double* %26
%28 = call i32 @fprintf(i32* %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), double %27)
%29 = load double*, double** %6
%30 = getelementptr inbounds double, double* %29, i32 1
store double* %30, double** %6
br label %31
31: ; preds = %24
%32 = load i32, i32* %11
%33 = add nsw i32 %32, 1
store i32 %33, i32* %11
br label %19
34: ; preds = %19
%35 = load i32*, i32** %9
%36 = load double*, double** %6
%37 = load double, double* %36
%38 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %37)
%39 = load double*, double** %6
%40 = getelementptr inbounds double, double* %39, i32 1
store double* %40, double** %6
br label %41
41: ; preds = %34
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
br label %14
44: ; preds = %14
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wt\00"
@.str.1 = [7 x i8] c"%3.4f\09\00"
@.str.2 = [6 x i8] c"%.6g\0A\00"
define void @write_file(i8* nocapture %0, double* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = icmp sgt i32 %3, 1
br label %9
9: ; preds = %7, %14
%10 = phi i32 [ 0, %7 ], [ %19, %14 ]
%11 = phi double* [ %1, %7 ], [ %18, %14 ]
br i1 %8, label %21, label %14
12: ; preds = %14, %4
%13 = tail call i32 @fclose(i32* %5)
ret void
14: ; preds = %21, %9
%15 = phi double* [ %11, %9 ], [ %26, %21 ]
%16 = load double, double* %15
%17 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %16)
%18 = getelementptr inbounds double, double* %15, i64 1
%19 = add nuw nsw i32 %10, 1
%20 = icmp eq i32 %19, %2
br i1 %20, label %12, label %9
21: ; preds = %9, %21
%22 = phi i32 [ %27, %21 ], [ 1, %9 ]
%23 = phi double* [ %26, %21 ], [ %11, %9 ]
%24 = load double, double* %23
%25 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), double %24)
%26 = getelementptr inbounds double, double* %23, i64 1
%27 = add nuw nsw i32 %22, 1
%28 = icmp eq i32 %27, %3
br i1 %28, label %14, label %21
}