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

/* Variables and functions */
int /*<<< orphan*/ BUFSIZ;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ spamsum_match(char const *, char *);
int strlen(char *);
u_int32_t spamsum_match_db(const char *fname, const char *sum,
                           u_int32_t threshold) {
  FILE *f;
  char line[BUFSIZ + 1];
  u_int32_t best = 0;
  f = fopen(fname, "r");
  if (!f)
    return 0;
  while (fgets(line, BUFSIZ, f)) {
    u_int32_t score;
    int len;
    len = strlen(line);
    if (line[len - 1] == '\n')
      line[len - 1] = 0;
    score = spamsum_match(sum, line);
    if (score > best) {
      best = score;
      if (best >= threshold)
        break;
    }
  }
  fclose(f);
  return best;
}


x86 assembly (clang, -O0):
<spamsum_match_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ce <spamsum_match_db+0x7e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1288 <spamsum_match_db+0x138>
jmp    11d3 <spamsum_match_db+0x83>
mov    -0x60(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1270 <spamsum_match_db+0x120>
mov    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x60(%rbp),%rax
mov    %ecx,-0x54(%rbp)
mov    -0x54(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    122c <spamsum_match_db+0xdc>
mov    -0x60(%rbp),%rax
mov    -0x54(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x60(%rbp),%rsi
mov    -0x18(%rbp),%rdi
call   1060 <spamsum_match@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x40(%rbp),%rax
jle    126b <spamsum_match_db+0x11b>
mov    -0x50(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jl     1266 <spamsum_match_db+0x116>
jmp    1270 <spamsum_match_db+0x120>
jmp    126b <spamsum_match_db+0x11b>
jmp    11d3 <spamsum_match_db+0x83>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spamsum_match_db>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,-0x30(%rbp)
mov    %rsi,%r15
mov    0x2e74(%rip),%r14
mov    (%r14),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe76(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fc <spamsum_match_db+0xac>
mov    %rax,%r12
mov    (%r14),%esi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %r13d,%r13d
jmp    11be <spamsum_match_db+0x6e>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11f2 <spamsum_match_db+0xa2>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11d9 <spamsum_match_db+0x89>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <spamsum_match@plt>
cmp    %r13,%rax
jle    11b0 <spamsum_match_db+0x60>
mov    %rax,%r13
cmp    -0x30(%rbp),%rax
jl     11b0 <spamsum_match_db+0x60>
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    11ff <spamsum_match_db+0xaf>
xor    %r13d,%r13d
mov    %r13,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
define i64 @spamsum_match_db(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%15 = load i32, i32* @BUFSIZ
%16 = add nsw i32 %15, 1
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %9
%19 = alloca i8, i64 %17
store i64 %17, i64* %10
store i64 0, i64* %11
%20 = load i8*, i8** %5
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %21, i32** %8
%22 = load i32*, i32** %8
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %3
store i64 0, i64* %4
store i32 1, i32* %12
br label %63
25: ; preds = %3
br label %26
26: ; preds = %58, %25
%27 = load i32, i32* @BUFSIZ
%28 = load i32*, i32** %8
%29 = call i64 @fgets(i8* %19, i32 %27, i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %59
31: ; preds = %26
%32 = call i32 @strlen(i8* %19)
store i32 %32, i32* %14
%33 = load i32, i32* %14
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %19, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 10
br i1 %39, label %40, label %45
40: ; preds = %31
%41 = load i32, i32* %14
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %19, i64 %43
store i8 0, i8* %44
br label %45
45: ; preds = %40, %31
%46 = load i8*, i8** %6
%47 = call i64 @spamsum_match(i8* %46, i8* %19)
store i64 %47, i64* %13
%48 = load i64, i64* %13
%49 = load i64, i64* %11
%50 = icmp sgt i64 %48, %49
br i1 %50, label %51, label %58
51: ; preds = %45
%52 = load i64, i64* %13
store i64 %52, i64* %11
%53 = load i64, i64* %11
%54 = load i64, i64* %7
%55 = icmp sge i64 %53, %54
br i1 %55, label %56, label %57
56: ; preds = %51
br label %59
57: ; preds = %51
br label %58
58: ; preds = %57, %45
br label %26
59: ; preds = %56, %26
%60 = load i32*, i32** %8
%61 = call i32 @fclose(i32* %60)
%62 = load i64, i64* %11
store i64 %62, i64* %4
store i32 1, i32* %12
br label %63
63: ; preds = %59, %24
%64 = load i8*, i8** %9
%65 = load i64, i64* %4
ret i64 %65
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [2 x i8] c"r\00"
define i64 @spamsum_match_db(i8* nocapture %0, i8* %1, i64 %2) {
%4 = load i32, i32* @BUFSIZ
%5 = add nsw i32 %4, 1
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %36, label %10
10: ; preds = %3
%11 = load i32, i32* @BUFSIZ
%12 = call i64 @fgets(i8* %7, i32 %11, i32* %8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %33, label %14
14: ; preds = %10, %28
%15 = phi i64 [ %29, %28 ], [ 0, %10 ]
%16 = call i32 @strlen(i8* %7)
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %7, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 10
br i1 %21, label %22, label %23
22: ; preds = %14
store i8 0, i8* %19
br label %23
23: ; preds = %22, %14
%24 = call i64 @spamsum_match(i8* %1, i8* %7)
%25 = icmp sgt i64 %24, %15
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = icmp slt i64 %24, %2
br i1 %27, label %28, label %33
28: ; preds = %23, %26
%29 = phi i64 [ %24, %26 ], [ %15, %23 ]
%30 = load i32, i32* @BUFSIZ
%31 = call i64 @fgets(i8* %7, i32 %30, i32* %8)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %14
33: ; preds = %28, %26, %10
%34 = phi i64 [ 0, %10 ], [ %24, %26 ], [ %29, %28 ]
%35 = call i32 @fclose(i32* %8)
br label %36
36: ; preds = %3, %33
%37 = phi i64 [ %34, %33 ], [ 0, %3 ]
ret i64 %37
}