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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fwrite(char const *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, float);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int ai_common_save_func(const char *header, float **values,
                        const char *filename) {
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    fprintf(stderr, "Couldn't open file %s\n", filename);
    return 1;
  }
  int ret = fwrite(header, strlen(header), 1, fp);
  if (ret != 1) {
    fprintf(stderr, "Couldn't write header to file %s\n", filename);
    fclose(fp);
    return 1;
  }
  char buf[1024];
  memset(buf, 0x00, sizeof(buf));
  while (*values) {
    sprintf(buf, " %f", **values);
    if ((fwrite(buf, strlen(buf), 1, fp)) != 1) {
      fprintf(stderr, "Couldn't write float to file %s\n", filename);
      fclose(fp);
      return 1;
    }
    values++;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<ai_common_save_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe6e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cb <ai_common_save_func+0x5b>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe48(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f9 <ai_common_save_func+0x189>
mov    -0x10(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
je     1231 <ai_common_save_func+0xc1>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe02(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f9 <ai_common_save_func+0x189>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12e9 <ai_common_save_func+0x179>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movss  (%rax),%xmm0
lea    0xdd0(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    -0x430(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x440(%rbp),%rdi
mov    %eax,%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     12d8 <ai_common_save_func+0x168>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd81(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f9 <ai_common_save_func+0x189>
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x18(%rbp)
jmp    1244 <ai_common_save_func+0xd4>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ai_common_save_func>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe6f(%rip),%rsi
mov    %rdx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     123d <ai_common_save_func+0xcd>
mov    %rax,%r13
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    $0x1,%r15d
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
jne    125d <ai_common_save_func+0xed>
mov    %rsp,%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%rax
test   %rax,%rax
je     1295 <ai_common_save_func+0x125>
add    $0x8,%rbp
lea    0xe43(%rip),%r12
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
movss  (%rax),%xmm0
mov    %rbx,%rdi
mov    %r12,%rsi
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
jne    1277 <ai_common_save_func+0x107>
mov    0x0(%rbp),%rax
add    $0x8,%rbp
test   %rax,%rax
jne    1200 <ai_common_save_func+0x90>
jmp    1295 <ai_common_save_func+0x125>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%r15d
jmp    129d <ai_common_save_func+0x12d>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
jmp    1295 <ai_common_save_func+0x125>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%r15d
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %r15d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Couldn't open file %s\0A\00"
@.str.2 = [34 x i8] c"Couldn't write header to file %s\0A\00"
@.str.3 = [4 x i8] c" %f\00"
@.str.4 = [33 x i8] c"Couldn't write float to file %s\0A\00"
define i32 @ai_common_save_func(i8* %0, float** %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca float**
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca [1024 x i8]
store i8* %0, i8** %5
store float** %1, float*** %6
store i8* %2, i8** %7
%11 = load i8*, i8** %7
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %7
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 1, i32* %4
br label %64
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = load i32*, i32** %8
%24 = call i32 @fwrite(i8* %20, i32 %22, i32 1, i32* %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %33
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %7
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** %8
%32 = call i32 @fclose(i32* %31)
store i32 1, i32* %4
br label %64
33: ; preds = %19
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%35 = call i32 @memset(i8* %34, i32 0, i32 1024)
br label %36
36: ; preds = %58, %33
%37 = load float**, float*** %6
%38 = load float*, float** %37
%39 = icmp ne float* %38, null
br i1 %39, label %40, label %61
40: ; preds = %36
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%42 = load float**, float*** %6
%43 = load float*, float** %42
%44 = load float, float* %43
%45 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), float %44)
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = load i32*, i32** %8
%50 = call i32 @fwrite(i8* %46, i32 %48, i32 1, i32* %49)
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %58
52: ; preds = %40
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %7
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = load i32*, i32** %8
%57 = call i32 @fclose(i32* %56)
store i32 1, i32* %4
br label %64
58: ; preds = %40
%59 = load float**, float*** %6
%60 = getelementptr inbounds float*, float** %59, i32 1
store float** %60, float*** %6
br label %36
61: ; preds = %36
%62 = load i32*, i32** %8
%63 = call i32 @fclose(i32* %62)
store i32 0, i32* %4
br label %64
64: ; preds = %61, %52, %27, %15
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Couldn't open file %s\0A\00"
@.str.2 = [34 x i8] c"Couldn't write header to file %s\0A\00"
@.str.3 = [4 x i8] c" %f\00"
@.str.4 = [33 x i8] c"Couldn't write float to file %s\0A\00"
define i32 @ai_common_save_func(i8* %0, float** nocapture %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %41
10: ; preds = %3
%11 = tail call i32 @strlen(i8* %0)
%12 = tail call i32 @fwrite(i8* %0, i32 %11, i32 1, i32* %5)
%13 = icmp eq i32 %12, 1
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%17 = tail call i32 @fclose(i32* %5)
br label %41
18: ; preds = %10
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 1024)
%21 = load float*, float** %1
%22 = icmp eq float* %21, null
br i1 %22, label %38, label %26
23: ; preds = %26
%24 = load float*, float** %34
%25 = icmp eq float* %24, null
br i1 %25, label %38, label %26
26: ; preds = %18, %23
%27 = phi float* [ %24, %23 ], [ %21, %18 ]
%28 = phi float** [ %34, %23 ], [ %1, %18 ]
%29 = load float, float* %27
%30 = call i32 @sprintf(i8* %19, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), float %29)
%31 = call i32 @strlen(i8* %19)
%32 = call i32 @fwrite(i8* %19, i32 %31, i32 1, i32* %5)
%33 = icmp eq i32 %32, 1
%34 = getelementptr inbounds float*, float** %28, i64 1
br i1 %33, label %23, label %35
35: ; preds = %26
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %38
38: ; preds = %23, %18, %35
%39 = phi i32 [ 1, %35 ], [ 0, %18 ], [ 0, %23 ]
%40 = call i32 @fclose(i32* %5)
br label %41
41: ; preds = %14, %38, %7
%42 = phi i32 [ 1, %7 ], [ 1, %14 ], [ %39, %38 ]
ret i32 %42
}