#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 */
struct span_table {
  int dummy;
};
struct htrace_span {
  int dummy;
};
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ err;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ span_json_parse(char *, struct htrace_span **, char *,
                                   size_t);
int /*<<< orphan*/ span_table_put(struct span_table *, struct htrace_span *);
int /*<<< orphan*/ stderr;
char *terror(int);
int load_trace_span_file(const char *path, struct span_table *st) {
  char line[8196], err[1024];
  size_t err_len = sizeof(err);
  FILE *fp = NULL;
  int lineno = 0, ret = EXIT_FAILURE;
  struct htrace_span *span;
  fp = fopen(path, "r");
  if (!fp) {
    int res = errno;
    fprintf(stderr, "failed to open %s: %s\n", path, terror(res));
    return -1;
  }
  while (1) {
    ++lineno;
    if (!fgets(line, sizeof(line), fp)) {
      if (ferror(fp)) {
        int res = errno;
        fprintf(stderr, "error reading from %s: %s\n", path, terror(res));
        break;
      }
      ret = EXIT_SUCCESS;
      break;
    }
    span_json_parse(line, &span, err, err_len);
    if (err[0]) {
      fprintf(stderr,
              "error parsing line %d.  Failed to parse %s "
              "from %s: %s\n",
              lineno, line, path, err);
      break;
    }
    span_table_put(st, span);
  }
  fclose(fp);
  if (ret == EXIT_SUCCESS) {
    return lineno - 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<load_trace_span_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2470,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x400,-0x2428(%rbp)
movq   $0x0,-0x2430(%rbp)
movl   $0x0,-0x2434(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2438(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x2430(%rbp)
cmpq   $0x0,-0x2430(%rbp)
jne    1245 <load_trace_span_file+0xc5>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2444(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2454(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x2450(%rbp)
mov    -0x2444(%rbp),%edi
call   1080 <terror@plt>
mov    -0x2454(%rbp),%edi
mov    -0x2450(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <load_trace_span_file+0x22b>
jmp    124a <load_trace_span_file+0xca>
mov    -0x2434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2434(%rbp)
lea    -0x2020(%rbp),%rdi
mov    -0x2430(%rbp),%rdx
mov    $0x2004,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    12fb <load_trace_span_file+0x17b>
mov    -0x2430(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12e7 <load_trace_span_file+0x167>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2448(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2464(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x2460(%rbp)
mov    -0x2448(%rbp),%edi
call   1080 <terror@plt>
mov    -0x2464(%rbp),%edi
mov    -0x2460(%rbp),%rdx
mov    %rax,%rcx
lea    0xd3e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1372 <load_trace_span_file+0x1f2>
mov    0x2cda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2438(%rbp)
jmp    1372 <load_trace_span_file+0x1f2>
lea    -0x2020(%rbp),%rdi
lea    -0x2420(%rbp),%rdx
mov    -0x2428(%rbp),%rcx
lea    -0x2440(%rbp),%rsi
call   10a0 <span_json_parse@plt>
cmpb   $0x0,-0x2420(%rbp)
je     135d <load_trace_span_file+0x1dd>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
mov    -0x2434(%rbp),%edx
lea    -0x2020(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    -0x2420(%rbp),%r9
lea    0xce3(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1372 <load_trace_span_file+0x1f2>
mov    -0x18(%rbp),%rdi
mov    -0x2440(%rbp),%rsi
call   1060 <span_table_put@plt>
jmp    124a <load_trace_span_file+0xca>
mov    -0x2430(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2438(%rbp),%eax
mov    0x2c3d(%rip),%rcx
cmp    (%rcx),%eax
jne    13a4 <load_trace_span_file+0x224>
mov    -0x2434(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    13ab <load_trace_span_file+0x22b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_trace_span_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2428,%rsp
mov    %rsi,%r15
mov    %rdi,%rbp
mov    0x2e32(%rip),%rax
mov    (%rax),%ebx
lea    0xe59(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11fd <load_trace_span_file+0x7d>
mov    %rax,%r12
lea    0x420(%rsp),%rdi
mov    $0x2004,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     1234 <load_trace_span_file+0xb4>
lea    0x420(%rsp),%rdi
lea    0x18(%rsp),%rsi
lea    0x20(%rsp),%rdx
mov    $0x400,%ecx
call   10a0 <span_json_parse@plt>
cmpb   $0x0,0x20(%rsp)
je     123c <load_trace_span_file+0xbc>
xor    %r14d,%r14d
mov    $0x1,%edx
jmp    12ac <load_trace_span_file+0x12c>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%ebx
call   1080 <terror@plt>
lea    0xde7(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1343 <load_trace_span_file+0x1c3>
xor    %r14d,%r14d
jmp    12e2 <load_trace_span_file+0x162>
mov    %rbp,0x10(%rsp)
mov    %ebx,0xc(%rsp)
xor    %r14d,%r14d
lea    0x420(%rsp),%rbx
lea    0x18(%rsp),%r13
lea    0x20(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rsi
mov    %r15,%rdi
call   1060 <span_table_put@plt>
mov    %rbx,%rdi
mov    $0x2004,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     12d5 <load_trace_span_file+0x155>
mov    $0x400,%ecx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   10a0 <span_json_parse@plt>
add    $0x1,%r14d
cmpb   $0x0,0x20(%rsp)
je     1260 <load_trace_span_file+0xe0>
lea    0x1(%r14),%edx
mov    0xc(%rsp),%ebx
mov    0x10(%rsp),%rbp
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
lea    0x420(%rsp),%rcx
lea    0x20(%rsp),%r9
mov    %rbp,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1329 <load_trace_span_file+0x1a9>
add    $0x1,%r14d
mov    0xc(%rsp),%ebx
mov    0x10(%rsp),%rbp
mov    %r12,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1320 <load_trace_span_file+0x1a0>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%r15d
call   1080 <terror@plt>
lea    0xd0b(%rip),%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1329 <load_trace_span_file+0x1a9>
mov    0x2ca1(%rip),%rax
mov    (%rax),%ebx
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    0x2c90(%rip),%rax
cmp    (%rax),%ebx
mov    $0xffffffff,%eax
cmove  %r14d,%eax
add    $0x2428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.span_table = type { i32 }
%struct.htrace_span = type { i32 }
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"failed to open %s: %s\0A\00"
@.str.2 = [27 x i8] c"error reading from %s: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [56 x i8] c"error parsing line %d. Failed to parse %s from %s: %s\0A\00"
define i32 @load_trace_span_file(i8* %0, %struct.span_table* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.span_table*
%6 = alloca [8196 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.htrace_span*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store %struct.span_table* %1, %struct.span_table** %5
store i64 1024, i64* %8
store i32* null, i32** %9
store i32 0, i32* %10
%15 = load i32, i32* @EXIT_FAILURE
store i32 %15, i32* %11
%16 = load i8*, i8** %4
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %9
%18 = load i32*, i32** %9
%19 = icmp ne i32* %18, null
br i1 %19, label %27, label %20
20: ; preds = %2
%21 = load i32, i32* @errno
store i32 %21, i32* %13
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %4
%24 = load i32, i32* %13
%25 = call i8* @terror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %25)
store i32 -1, i32* %3
br label %77
27: ; preds = %2
br label %28
28: ; preds = %27, %63
%29 = load i32, i32* %10
%30 = add nsw i32 %29, 1
store i32 %30, i32* %10
%31 = getelementptr inbounds [8196 x i8], [8196 x i8]* %6, i64 0, i64 0
%32 = load i32*, i32** %9
%33 = call i32 @fgets(i8* %31, i32 8196, i32* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %48, label %35
35: ; preds = %28
%36 = load i32*, i32** %9
%37 = call i64 @ferror(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i32, i32* @errno
store i32 %40, i32* %14
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %4
%43 = load i32, i32* %14
%44 = call i8* @terror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %44)
br label %67
46: ; preds = %35
%47 = load i32, i32* @EXIT_SUCCESS
store i32 %47, i32* %11
br label %67
48: ; preds = %28
%49 = getelementptr inbounds [8196 x i8], [8196 x i8]* %6, i64 0, i64 0
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%51 = load i64, i64* %8
%52 = call i32 @span_json_parse(i8* %49, %struct.htrace_span** %12, i8* %50, i64 %51)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%54 = load i8, i8* %53
%55 = icmp ne i8 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %48
%57 = load i32, i32* @stderr
%58 = load i32, i32* %10
%59 = getelementptr inbounds [8196 x i8], [8196 x i8]* %6, i64 0, i64 0
%60 = load i8*, i8** %4
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %58, i8* %59, i8* %60, i8* %61)
br label %67
63: ; preds = %48
%64 = load %struct.span_table*, %struct.span_table** %5
%65 = load %struct.htrace_span*, %struct.htrace_span** %12
%66 = call i32 @span_table_put(%struct.span_table* %64, %struct.htrace_span* %65)
br label %28
67: ; preds = %56, %46, %39
%68 = load i32*, i32** %9
%69 = call i32 @fclose(i32* %68)
%70 = load i32, i32* %11
%71 = load i32, i32* @EXIT_SUCCESS
%72 = icmp eq i32 %70, %71
br i1 %72, label %73, label %76
73: ; preds = %67
%74 = load i32, i32* %10
%75 = sub nsw i32 %74, 1
store i32 %75, i32* %3
br label %77
76: ; preds = %67
store i32 -1, i32* %3
br label %77
77: ; preds = %76, %73, %20
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.span_table = type { i32 }
%struct.htrace_span = type { i32 }
@EXIT_FAILURE = global i32 0
@.str = [2 x i8] c"r\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"failed to open %s: %s\0A\00"
@.str.2 = [27 x i8] c"error reading from %s: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [56 x i8] c"error parsing line %d. Failed to parse %s from %s: %s\0A\00"
define i32 @load_trace_span_file(i8* %0, %struct.span_table* %1) {
%3 = alloca [8196 x i8]
%4 = alloca [1024 x i8]
%5 = alloca %struct.htrace_span*
%6 = getelementptr inbounds [8196 x i8], [8196 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* @EXIT_FAILURE
%9 = bitcast %struct.htrace_span** %5 to i8*
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %19, label %12
12: ; preds = %2
%13 = call i32 @fgets(i8* %6, i32 8196, i32* %10)
%14 = icmp eq i32 %13, 0
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = call i32 @span_json_parse(i8* %6, %struct.htrace_span** %5, i8* %7, i64 1024)
%17 = load i8, i8* %7
%18 = icmp eq i8 %17, 0
br i1 %18, label %44, label %39
19: ; preds = %2
%20 = load i32, i32* @errno
%21 = load i32, i32* @stderr
%22 = tail call i8* @terror(i32 %20)
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %22)
br label %58
24: ; preds = %44, %12
%25 = phi i32 [ 0, %12 ], [ %45, %44 ]
%26 = call i64 @ferror(i32* %10)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = load i32, i32* @stderr
%31 = call i8* @terror(i32 %29)
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %31)
br label %51
33: ; preds = %24
%34 = load i32, i32* @EXIT_SUCCESS
br label %51
35: ; preds = %44
%36 = call i32 @span_json_parse(i8* %6, %struct.htrace_span** %5, i8* %7, i64 1024)
%37 = load i8, i8* %7
%38 = icmp eq i8 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %35, %15
%40 = phi i32 [ 1, %15 ], [ %48, %35 ]
%41 = phi i32 [ 0, %15 ], [ %45, %35 ]
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %40, i8* %6, i8* %0, i8* %7)
br label %51
44: ; preds = %15, %35
%45 = phi i32 [ %48, %35 ], [ 1, %15 ]
%46 = load %struct.htrace_span*, %struct.htrace_span** %5
%47 = call i32 @span_table_put(%struct.span_table* %1, %struct.htrace_span* %46)
%48 = add nuw nsw i32 %45, 1
%49 = call i32 @fgets(i8* %6, i32 8196, i32* %10)
%50 = icmp eq i32 %49, 0
br i1 %50, label %24, label %35
51: ; preds = %39, %33, %28
%52 = phi i32 [ %41, %39 ], [ %25, %28 ], [ %25, %33 ]
%53 = phi i32 [ %8, %39 ], [ %8, %28 ], [ %34, %33 ]
%54 = call i32 @fclose(i32* %10)
%55 = load i32, i32* @EXIT_SUCCESS
%56 = icmp eq i32 %53, %55
%57 = select i1 %56, i32 %52, i32 -1
br label %58
58: ; preds = %51, %19
%59 = phi i32 [ -1, %19 ], [ %57, %51 ]
ret i32 %59
}