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

/* Variables and functions */
int countnnz16(int, int, scalar_t__ **, scalar_t__);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, ...);
int /*<<< orphan*/ yap_sysquit(char *, char *);
void write_u16sparseco(int nr, int nc, uint16_t **x, char *fname,
                       uint16_t cutoff) {
  FILE *fp = fopen(fname, "w");
  int i, j;
  if (!fp)
    yap_sysquit("Cannot open file '%s' for write\n", fname);
  fprintf(fp, "%d\n", nr);
  fprintf(fp, "%d\n", nc);
  fprintf(fp, "%d\n", countnnz16(nr, nc, x, cutoff));
  for (i = 0; i < nr; i++)
    for (j = 0; j < nc; j++)
      if (x[i][j] > cutoff)
        fprintf(fp, "%d %d %u\n", i, j, (unsigned)x[i][j]);
  if (ferror(fp))
    yap_sysquit("Error on writing file '%s' ", fname);
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_u16sparseco>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a9 <write_u16sparseco+0x49>
mov    -0x18(%rbp),%rsi
lea    0xe5e(%rip),%rdi
call   1080 <yap_sysquit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <countnnz16@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    129b <write_u16sparseco+0x13b>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1288 <write_u16sparseco+0x128>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
cmp    -0x20(%rbp),%rax
jle    1275 <write_u16sparseco+0x115>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rsi
mov    (%rax,%rsi,8),%rax
movslq -0x30(%rbp),%rsi
mov    (%rax,%rsi,8),%rax
mov    %eax,%r8d
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    127a <write_u16sparseco+0x11a>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    121c <write_u16sparseco+0xbc>
jmp    128d <write_u16sparseco+0x12d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1209 <write_u16sparseco+0xa9>
mov    -0x28(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12be <write_u16sparseco+0x15e>
mov    -0x18(%rbp),%rsi
lea    0xd78(%rip),%rdi
call   1080 <yap_sysquit@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_u16sparseco>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r12
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %edi,%r13d
lea    0xe7c(%rip),%rsi
mov    %rcx,%rdi
call   1070 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a3 <write_u16sparseco+0x43>
lea    0xe67(%rip),%rdi
mov    %rbp,%rsi
call   1080 <yap_sysquit@plt>
mov    %rbp,0x8(%rsp)
lea    0xe74(%rip),%rbp
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r13d,%edi
mov    %r14d,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1060 <countnnz16@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r13d,%r13d
jle    125e <write_u16sparseco+0xfe>
mov    %r13d,%eax
mov    %rax,0x10(%rsp)
mov    %r14d,%r13d
xor    %ebp,%ebp
mov    %r14d,0x4(%rsp)
jmp    1220 <write_u16sparseco+0xc0>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    0x10(%rsp),%rbp
mov    0x4(%rsp),%r14d
je     125e <write_u16sparseco+0xfe>
test   %r14d,%r14d
jle    1210 <write_u16sparseco+0xb0>
xor    %r14d,%r14d
jmp    1239 <write_u16sparseco+0xd9>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14
cmp    %r14,%r13
je     1210 <write_u16sparseco+0xb0>
mov    (%rbx,%rbp,8),%rax
mov    (%rax,%r14,8),%r8
cmp    %r12,%r8
jle    1230 <write_u16sparseco+0xd0>
mov    %r15,%rdi
lea    0xdd7(%rip),%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1230 <write_u16sparseco+0xd0>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     127c <write_u16sparseco+0x11c>
lea    0xdbf(%rip),%rdi
mov    0x8(%rsp),%rsi
call   1080 <yap_sysquit@plt>
mov    %r15,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [33 x i8] c"Cannot open file '%s' for write\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [10 x i8] c"%d %d %u\0A\00"
@.str.4 = [28 x i8] c"Error on writing file '%s' \00"
define void @write_u16sparseco(i32 %0, i32 %1, i64** %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64**
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i64** %2, i64*** %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%14 = load i8*, i8** %9
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %11
%16 = load i32*, i32** %11
%17 = icmp ne i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %5
%19 = load i8*, i8** %9
%20 = call i32 @yap_sysquit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %18, %5
%22 = load i32*, i32** %11
%23 = load i32, i32* %6
%24 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32*, i32** %11
%26 = load i32, i32* %7
%27 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32*, i32** %11
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = load i64**, i64*** %8
%32 = load i64, i64* %10
%33 = call i32 @countnnz16(i32 %29, i32 %30, i64** %31, i64 %32)
%34 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 0, i32* %12
br label %35
35: ; preds = %76, %21
%36 = load i32, i32* %12
%37 = load i32, i32* %6
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %79
39: ; preds = %35
store i32 0, i32* %13
br label %40
40: ; preds = %72, %39
%41 = load i32, i32* %13
%42 = load i32, i32* %7
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %75
44: ; preds = %40
%45 = load i64**, i64*** %8
%46 = load i32, i32* %12
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i64*, i64** %45, i64 %47
%49 = load i64*, i64** %48
%50 = load i32, i32* %13
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i64, i64* %49, i64 %51
%53 = load i64, i64* %52
%54 = load i64, i64* %10
%55 = icmp sgt i64 %53, %54
br i1 %55, label %56, label %71
56: ; preds = %44
%57 = load i32*, i32** %11
%58 = load i32, i32* %12
%59 = load i32, i32* %13
%60 = load i64**, i64*** %8
%61 = load i32, i32* %12
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i64*, i64** %60, i64 %62
%64 = load i64*, i64** %63
%65 = load i32, i32* %13
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i64, i64* %64, i64 %66
%68 = load i64, i64* %67
%69 = trunc i64 %68 to i32
%70 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %57, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %58, i32 %59, i32 %69)
br label %71
71: ; preds = %56, %44
br label %72
72: ; preds = %71
%73 = load i32, i32* %13
%74 = add nsw i32 %73, 1
store i32 %74, i32* %13
br label %40
75: ; preds = %40
br label %76
76: ; preds = %75
%77 = load i32, i32* %12
%78 = add nsw i32 %77, 1
store i32 %78, i32* %12
br label %35
79: ; preds = %35
%80 = load i32*, i32** %11
%81 = call i64 @ferror(i32* %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %79
%84 = load i8*, i8** %9
%85 = call i32 @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %84)
br label %86
86: ; preds = %83, %79
%87 = load i32*, i32** %11
%88 = call i32 @fclose(i32* %87)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [33 x i8] c"Cannot open file '%s' for write\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [10 x i8] c"%d %d %u\0A\00"
@.str.4 = [28 x i8] c"Error on writing file '%s' \00"
define void @write_u16sparseco(i32 %0, i32 %1, i64** %2, i8* %3, i64 %4) {
%6 = tail call i32* @fopen(i8* %3, 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 @yap_sysquit(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %10
10: ; preds = %8, %5
%11 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%12 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%13 = tail call i32 @countnnz16(i32 %0, i32 %1, i64** %2, i64 %4)
%14 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp sgt i32 %0, 0
br i1 %15, label %16, label %41
16: ; preds = %10
%17 = icmp sgt i32 %1, 0
%18 = zext i32 %0 to i64
%19 = zext i32 %1 to i64
br label %20
20: ; preds = %16, %38
%21 = phi i64 [ 0, %16 ], [ %39, %38 ]
br i1 %17, label %22, label %38
22: ; preds = %20
%23 = getelementptr inbounds i64*, i64** %2, i64 %21
%24 = trunc i64 %21 to i32
br label %25
25: ; preds = %22, %35
%26 = phi i64 [ 0, %22 ], [ %36, %35 ]
%27 = load i64*, i64** %23
%28 = getelementptr inbounds i64, i64* %27, i64 %26
%29 = load i64, i64* %28
%30 = icmp sgt i64 %29, %4
br i1 %30, label %31, label %35
31: ; preds = %25
%32 = trunc i64 %29 to i32
%33 = trunc i64 %26 to i32
%34 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %33, i32 %32)
br label %35
35: ; preds = %25, %31
%36 = add nuw nsw i64 %26, 1
%37 = icmp eq i64 %36, %19
br i1 %37, label %38, label %25
38: ; preds = %35, %20
%39 = add nuw nsw i64 %21, 1
%40 = icmp eq i64 %39, %18
br i1 %40, label %41, label %20
41: ; preds = %38, %10
%42 = tail call i64 @ferror(i32* %6)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %41
%45 = tail call i32 @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %46
46: ; preds = %44, %41
%47 = tail call i32 @fclose(i32* %6)
ret void
}