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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strncmp(char *, char *, int);
int line_count(char *file) {
  FILE *in = fopen(file, "r");
  if (in == NULL) {
    fprintf(stderr, "Error opening input file %s for line count:%d.\n", file,
            errno);
    return -1;
  }
  char line[5000];
  int line_count = 0;
  while (fgets(line, sizeof(line), in) != NULL) {
    if (strncmp(line, "@SQ", 3) == 0) {
      line_count++;
    }
  }
  fclose(in);
  return line_count;
}


x86 assembly (clang, -O0):
<line_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x13b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ac <line_count+0x5c>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2e46(%rip),%rax
mov    (%rax),%ecx
lea    0xe67(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <line_count+0xcd>
movl   $0x0,-0x13a4(%rbp)
lea    -0x13a0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1388,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     120b <line_count+0xbb>
lea    -0x13a0(%rbp),%rdi
lea    0xe4f(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1206 <line_count+0xb6>
mov    -0x13a4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13a4(%rbp)
jmp    11b6 <line_count+0x66>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x13a4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x13b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<line_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1388,%rsp
mov    %rdi,%rbx
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d6 <line_count+0x86>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x1388,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11cc <line_count+0x7c>
lea    0xea0(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x1,%rax
adc    $0x0,%ebp
mov    %rbx,%rdi
mov    $0x1388,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11a0 <line_count+0x50>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    11fc <line_count+0xac>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
lea    0xe13(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x1388,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [48 x i8] c"Error opening input file %s for line count:%d.\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"@SQ\00"
define i32 @line_count(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [5000 x i8]
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = load i32, i32* @errno
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %13, i32 %14)
store i32 -1, i32* %2
br label %34
16: ; preds = %1
store i32 0, i32* %6
br label %17
17: ; preds = %29, %16
%18 = getelementptr inbounds [5000 x i8], [5000 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %4
%20 = call i32* @fgets(i8* %18, i32 5000, i32* %19)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %30
22: ; preds = %17
%23 = getelementptr inbounds [5000 x i8], [5000 x i8]* %5, i64 0, i64 0
%24 = call i64 @strncmp(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %29
29: ; preds = %26, %22
br label %17
30: ; preds = %17
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* %6
store i32 %33, i32* %2
br label %34
34: ; preds = %30, %11
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [48 x i8] c"Error opening input file %s for line count:%d.\0A\00"
@errno = global i32 0
@.str.2 = [4 x i8] c"@SQ\00"
define i32 @line_count(i8* %0) {
%2 = alloca [5000 x i8]
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %7)
br label %24
9: ; preds = %1
%10 = getelementptr inbounds [5000 x i8], [5000 x i8]* %2, i64 0, i64 0
%11 = call i32* @fgets(i8* %10, i32 5000, i32* %3)
%12 = icmp eq i32* %11, null
br i1 %12, label %21, label %13
13: ; preds = %9, %13
%14 = phi i32 [ %18, %13 ], [ 0, %9 ]
%15 = call i64 @strncmp(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%16 = icmp eq i64 %15, 0
%17 = zext i1 %16 to i32
%18 = add nuw nsw i32 %14, %17
%19 = call i32* @fgets(i8* %10, i32 5000, i32* %3)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %13
21: ; preds = %13, %9
%22 = phi i32 [ 0, %9 ], [ %18, %13 ]
%23 = call i32 @fclose(i32* %3)
br label %24
24: ; preds = %21, %5
%25 = phi i32 [ -1, %5 ], [ %22, %21 ]
ret i32 %25
}