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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, ...);
int /*<<< orphan*/ yap_quit(char *, ...);
int /*<<< orphan*/ yap_sysquit(char *, char *);
void read_u16sparse(int nr, int nc, uint16_t **x, char *fname) {
  FILE *fp = fopen(fname, "r");
  int i, u, v;
  int Nin;
  int sparse = 0;
  if (!fp)
    yap_sysquit("Cannot open file '%s' for read\n", fname);
  if (fscanf(fp, "%d", &Nin) != 1 || Nin != nr)
    yap_quit("Number rows wrong for sparse matrix in '%s', should be %d\n",
             fname, nr);
  if (fscanf(fp, "%d", &Nin) != 1 || Nin != nc)
    yap_quit("Number columns wrong for sparse matrix in '%s', should be %d\n",
             fname, nc);
  if (fscanf(fp, "%d", &sparse) != 1)
    yap_quit("Cannot read sparsity in matrix in '%s'\n", fname);
  for (i = 0; i < sparse; i++) {
    if (fscanf(fp, "%d %d %u", &u, &v, &Nin) != 3)
      yap_quit("Cannot read line %d in matrix in '%s'\n", i, fname);
    x[u][v] = Nin;
  }
  if (ferror(fp))
    yap_sysquit("Error on reading file '%s' ", fname);
  fclose(fp);
}


x86 assembly (clang, -O0):
<read_u16sparse>:
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    -0x18(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <read_u16sparse+0x4c>
mov    -0x18(%rbp),%rsi
lea    0xe5b(%rip),%rdi
call   1080 <yap_sysquit@plt>
mov    -0x20(%rbp),%rdi
lea    0xe6b(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    11d7 <read_u16sparse+0x77>
mov    -0x30(%rbp),%eax
cmp    -0x4(%rbp),%eax
je     11ec <read_u16sparse+0x8c>
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1060 <yap_quit@plt>
mov    -0x20(%rbp),%rdi
lea    0xe2b(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    1217 <read_u16sparse+0xb7>
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     122c <read_u16sparse+0xcc>
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1060 <yap_quit@plt>
mov    -0x20(%rbp),%rdi
lea    0xdeb(%rip),%rsi
lea    -0x34(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     125d <read_u16sparse+0xfd>
mov    -0x18(%rbp),%rsi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1060 <yap_quit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    12d0 <read_u16sparse+0x170>
mov    -0x20(%rbp),%rdi
lea    0xe4b(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
lea    -0x30(%rbp),%r8
mov    $0x0,%al
call   1050 <fscanf@plt>
cmp    $0x3,%eax
je     12ac <read_u16sparse+0x14c>
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <yap_quit@plt>
mov    -0x30(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1264 <read_u16sparse+0x104>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12f3 <read_u16sparse+0x193>
mov    -0x18(%rbp),%rsi
lea    0xe08(%rip),%rdi
call   1080 <yap_sysquit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_u16sparse>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %esi,%r12d
mov    %edi,%ebp
lea    0xe81(%rip),%rsi
mov    %rcx,0x10(%rsp)
mov    %rcx,%rdi
call   1070 <fopen@plt>
mov    %rax,%r13
movl   $0x0,0xc(%rsp)
test   %rax,%rax
jne    11ad <read_u16sparse+0x4d>
lea    0xe5f(%rip),%rdi
mov    0x10(%rsp),%rsi
call   1080 <yap_sysquit@plt>
lea    0xe6e(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    11ce <read_u16sparse+0x6e>
cmp    %ebp,0x8(%rsp)
je     11e3 <read_u16sparse+0x83>
lea    0xe50(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <yap_quit@plt>
lea    0xe38(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
cmp    $0x1,%eax
jne    1205 <read_u16sparse+0xa5>
cmp    %r12d,0x8(%rsp)
je     121b <read_u16sparse+0xbb>
lea    0xe54(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <yap_quit@plt>
lea    0xe00(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
cmp    $0x1,%eax
je     1249 <read_u16sparse+0xe9>
lea    0xe61(%rip),%rdi
mov    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <yap_quit@plt>
cmpl   $0x0,0xc(%rsp)
jle    12c8 <read_u16sparse+0x168>
lea    0xe6f(%rip),%r12
lea    0x1c(%rsp),%rbx
lea    0x18(%rsp),%rbp
lea    0x8(%rsp),%r14
xor    %r15d,%r15d
jmp    1295 <read_u16sparse+0x135>
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%eax
movslq 0x1c(%rsp),%rcx
mov    0x20(%rsp),%rdx
mov    (%rdx,%rcx,8),%rcx
movslq 0x18(%rsp),%rdx
mov    %eax,(%rcx,%rdx,4)
add    $0x1,%r15d
cmp    0xc(%rsp),%r15d
jge    12c8 <read_u16sparse+0x168>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1050 <fscanf@plt>
cmp    $0x3,%eax
je     1270 <read_u16sparse+0x110>
lea    0xe18(%rip),%rdi
mov    %r15d,%esi
mov    0x10(%rsp),%rdx
xor    %eax,%eax
call   1060 <yap_quit@plt>
jmp    1270 <read_u16sparse+0x110>
mov    %r13,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     12e6 <read_u16sparse+0x186>
lea    0xe1a(%rip),%rdi
mov    0x10(%rsp),%rsi
call   1080 <yap_sysquit@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [32 x i8] c"Cannot open file '%s' for read\0A\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [59 x i8] c"Number rows wrong for sparse matrix in '%s', should be %d\0A\00"
@.str.4 = [62 x i8] c"Number columns wrong for sparse matrix in '%s', should be %d\0A\00"
@.str.5 = [40 x i8] c"Cannot read sparsity in matrix in '%s'\0A\00"
@.str.6 = [9 x i8] c"%d %d %u\00"
@.str.7 = [39 x i8] c"Cannot read line %d in matrix in '%s'\0A\00"
@.str.8 = [28 x i8] c"Error on reading file '%s' \00"
define void @read_u16sparse(i32 %0, i32 %1, i32** nocapture %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = bitcast i32* %5 to i8*
%11 = bitcast i32* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
store i32 0, i32* %8
%14 = icmp eq i32* %9, null
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = tail call i32 @yap_sysquit(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %17
17: ; preds = %15, %4
%18 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%19 = icmp eq i32 %18, 1
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, %0
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = call i32 (i8*, ...) @yap_quit(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0), i8* %3, i32 %0)
br label %25
25: ; preds = %17, %23
%26 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%27 = icmp eq i32 %26, 1
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, %1
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = call i32 (i8*, ...) @yap_quit(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i8* %3, i32 %1)
br label %33
33: ; preds = %25, %31
%34 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%35 = icmp eq i32 %34, 1
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 (i8*, ...) @yap_quit(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %3)
br label %38
38: ; preds = %36, %33
%39 = load i32, i32* %8
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %59
41: ; preds = %38, %47
%42 = phi i32 [ %56, %47 ], [ 0, %38 ]
%43 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32* %5, i32* %6, i32* %7)
%44 = icmp eq i32 %43, 3
br i1 %44, label %47, label %45
45: ; preds = %41
%46 = call i32 (i8*, ...) @yap_quit(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0), i32 %42, i8* %3)
br label %47
47: ; preds = %45, %41
%48 = load i32, i32* %7
%49 = load i32, i32* %5
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32*, i32** %2, i64 %50
%52 = load i32*, i32** %51
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
store i32 %48, i32* %55
%56 = add nuw nsw i32 %42, 1
%57 = load i32, i32* %8
%58 = icmp slt i32 %56, %57
br i1 %58, label %41, label %59
59: ; preds = %47, %38
%60 = call i64 @ferror(i32* %9)
%61 = icmp eq i64 %60, 0
br i1 %61, label %64, label %62
62: ; preds = %59
%63 = call i32 @yap_sysquit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %3)
br label %64
64: ; preds = %62, %59
%65 = call i32 @fclose(i32* %9)
ret void
}