#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 *, ...);
int /*<<< orphan*/ printf(char *, char const *);
int WriteLibSVM(const char *filename, double *X, double *y, int d, int n) {
  FILE *fp = NULL;
  fp = fopen(filename, "w");
  if (fp == NULL) {
    printf("WriteData. Error: Cannot open file %s. Function call takes no "
           "effect.\n",
           filename);
    return -1;
  }
  long i, j;
  long ln = (long)n;
  long ld = (long)d;
  for (i = 0; i < ln; i++) {
    fprintf(fp, "%f", y[i]);
    for (j = 0; j < ld; j++) {
      fprintf(fp, " %ld:%g", j + 1, X[ln * j + i]);
    }
    fprintf(fp, "\n");
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<WriteLibSVM>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119e <WriteLibSVM+0x5e>
mov    -0x10(%rbp),%rsi
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <WriteLibSVM+0x12d>
movslq -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x48(%rbp),%rax
jge    125d <WriteLibSVM+0x11d>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe6d(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x50(%rbp),%rax
jge    123a <WriteLibSVM+0xfa>
mov    -0x30(%rbp),%rdi
mov    -0x40(%rbp),%rdx
add    $0x1,%rdx
mov    -0x18(%rbp),%rax
mov    -0x48(%rbp),%rcx
imul   -0x40(%rbp),%rcx
add    -0x38(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe2a(%rip),%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    11eb <WriteLibSVM+0xab>
mov    -0x30(%rbp),%rdi
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11b6 <WriteLibSVM+0x76>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteLibSVM>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r12d
mov    %ecx,0x4(%rsp)
mov    %rdx,0x18(%rsp)
mov    %rsi,0x8(%rsp)
mov    %rdi,%rbx
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     125c <WriteLibSVM+0x10c>
mov    %rax,%rbp
test   %r12d,%r12d
jle    1250 <WriteLibSVM+0x100>
movslq %r12d,%rax
movslq 0x4(%rsp),%r15
mov    %rax,0x10(%rsp)
lea    0x0(,%rax,8),%r14
neg    %r15
xor    %ecx,%ecx
lea    0xe98(%rip),%rbx
jmp    11e3 <WriteLibSVM+0x93>
cs nopw 0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
mov    0x20(%rsp),%rcx
add    $0x1,%rcx
addq   $0x8,0x8(%rsp)
cmp    0x10(%rsp),%rcx
je     1250 <WriteLibSVM+0x100>
mov    0x18(%rsp),%rax
mov    %rcx,0x20(%rsp)
movsd  (%rax,%rcx,8),%xmm0
mov    %rbp,%rdi
lea    0xe4d(%rip),%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
cmpl   $0x0,0x4(%rsp)
jle    11c0 <WriteLibSVM+0x70>
mov    $0x1,%r12d
mov    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
movsd  0x0(%r13),%xmm0
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
mov    $0x1,%al
call   1060 <fprintf@plt>
add    %r14,%r13
lea    (%r15,%r12,1),%rax
add    $0x1,%rax
add    $0x1,%r12
cmp    $0x1,%rax
jne    1220 <WriteLibSVM+0xd0>
jmp    11c0 <WriteLibSVM+0x70>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1270 <WriteLibSVM+0x120>
lea    0xd9f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [71 x i8] c"WriteData. Error: Cannot open file %s. Function call takes no effect.\0A\00"
@.str.2 = [3 x i8] c"%f\00"
@.str.3 = [8 x i8] c" %ld:%g\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @WriteLibSVM(i8* %0, double* %1, double* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca double*
%9 = alloca double*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
store i8* %0, i8** %7
store double* %1, double** %8
store double* %2, double** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32* null, i32** %12
%17 = load i8*, i8** %7
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %12
%19 = load i32*, i32** %12
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %5
%22 = load i8*, i8** %7
%23 = call i32 @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %6
br label %69
24: ; preds = %5
%25 = load i32, i32* %11
%26 = sext i32 %25 to i64
store i64 %26, i64* %15
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
store i64 %28, i64* %16
store i64 0, i64* %13
br label %29
29: ; preds = %63, %24
%30 = load i64, i64* %13
%31 = load i64, i64* %15
%32 = icmp slt i64 %30, %31
br i1 %32, label %33, label %66
33: ; preds = %29
%34 = load i32*, i32** %12
%35 = load double*, double** %9
%36 = load i64, i64* %13
%37 = getelementptr inbounds double, double* %35, i64 %36
%38 = load double, double* %37
%39 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), double %38)
store i64 0, i64* %14
br label %40
40: ; preds = %57, %33
%41 = load i64, i64* %14
%42 = load i64, i64* %16
%43 = icmp slt i64 %41, %42
br i1 %43, label %44, label %60
44: ; preds = %40
%45 = load i32*, i32** %12
%46 = load i64, i64* %14
%47 = add nsw i64 %46, 1
%48 = load double*, double** %8
%49 = load i64, i64* %15
%50 = load i64, i64* %14
%51 = mul nsw i64 %49, %50
%52 = load i64, i64* %13
%53 = add nsw i64 %51, %52
%54 = getelementptr inbounds double, double* %48, i64 %53
%55 = load double, double* %54
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 %47, double %55)
br label %57
57: ; preds = %44
%58 = load i64, i64* %14
%59 = add nsw i64 %58, 1
store i64 %59, i64* %14
br label %40
60: ; preds = %40
%61 = load i32*, i32** %12
%62 = call i32 (i32*, i8*, ...) @fprintf(i32* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %60
%64 = load i64, i64* %13
%65 = add nsw i64 %64, 1
store i64 %65, i64* %13
br label %29
66: ; preds = %29
%67 = load i32*, i32** %12
%68 = call i32 @fclose(i32* %67)
store i32 0, i32* %6
br label %69
69: ; preds = %66, %21
%70 = load i32, i32* %6
ret i32 %70
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [71 x i8] c"WriteData. Error: Cannot open file %s. Function call takes no effect.\0A\00"
@.str.2 = [3 x i8] c"%f\00"
@.str.3 = [8 x i8] c" %ld:%g\00"
define i32 @WriteLibSVM(i8* %0, double* nocapture %1, double* nocapture %2, i32 %3, i32 %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @printf(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %36
10: ; preds = %5
%11 = sext i32 %4 to i64
%12 = sext i32 %3 to i64
%13 = icmp sgt i32 %4, 0
br i1 %13, label %14, label %34
14: ; preds = %10
%15 = icmp sgt i32 %3, 0
br label %16
16: ; preds = %14, %30
%17 = phi i64 [ 0, %14 ], [ %32, %30 ]
%18 = getelementptr inbounds double, double* %2, i64 %17
%19 = load double, double* %18
%20 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), double %19)
br i1 %15, label %21, label %30
21: ; preds = %16, %21
%22 = phi i64 [ %23, %21 ], [ 0, %16 ]
%23 = add nuw nsw i64 %22, 1
%24 = mul nsw i64 %22, %11
%25 = add nsw i64 %24, %17
%26 = getelementptr inbounds double, double* %1, i64 %25
%27 = load double, double* %26
%28 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 %23, double %27)
%29 = icmp eq i64 %23, %12
br i1 %29, label %30, label %21
30: ; preds = %21, %16
%31 = tail call i32 @fputc(i32 10, i32* %6)
%32 = add nuw nsw i64 %17, 1
%33 = icmp eq i64 %32, %11
br i1 %33, label %34, label %16
34: ; preds = %30, %10
%35 = tail call i32 @fclose(i32* %6)
br label %36
36: ; preds = %34, %8
%37 = phi i32 [ -1, %8 ], [ 0, %34 ]
ret i32 %37
}