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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
char fgetc(int /*<<< orphan*/ *);
int find_signature(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long, ...);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int strtoul(char *, int /*<<< orphan*/ *, int);
int find_signature_named(const char *name, FILE *fp) {
  int is_eof, found = 0;
  int got_k = -1;
  for (is_eof = find_signature(fp); !found && !is_eof;
       found || (is_eof = find_signature(fp))) {
    const char *name_chr = name;
    size_t sig_offset = ftell(fp) - 18;
    while (*name_chr && *name_chr == fgetc(fp))
      name_chr++;
    if (*name_chr) {
      fprintf(stderr, "didn't match at %lu\n", (unsigned long)sig_offset);
      continue;
    }
    if (fgetc(fp) != '-') {
      fprintf(stderr, "too long at %lu\n", (unsigned long)sig_offset);
      continue;
    }
    found = 1;
    {
      char buf[16];
      fread(buf, 1, sizeof(buf), fp);
      fseek(fp, sig_offset, SEEK_SET);
      got_k = strtoul(buf, NULL, 10);
      fprintf(stderr, "match at %lu, size %d\n", (unsigned long)sig_offset,
              got_k);
    }
  }
  return got_k;
}


x86 assembly (clang, -O0):
<find_signature_named>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <find_signature@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x41(%rbp)
jne    11b5 <find_signature_named+0x45>
cmpl   $0x0,-0x14(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    11c5 <find_signature_named+0x55>
jmp    132d <find_signature_named+0x1bd>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
sub    $0x12,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x42(%rbp)
je     1217 <find_signature_named+0xa7>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,%cl
mov    -0x48(%rbp),%eax
movsbl %cl,%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x42(%rbp)
mov    -0x42(%rbp),%al
test   $0x1,%al
jne    1227 <find_signature_named+0xb7>
jmp    1238 <find_signature_named+0xc8>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11df <find_signature_named+0x6f>
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1265 <find_signature_named+0xf5>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1301 <find_signature_named+0x191>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
movsbl %al,%eax
cmp    $0x2d,%eax
je     129a <find_signature_named+0x12a>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1301 <find_signature_named+0x191>
movl   $0x1,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2d11(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xd2c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%al
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x49(%rbp)
jne    1325 <find_signature_named+0x1b5>
mov    -0x10(%rbp),%rdi
call   1040 <find_signature@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
jmp    119a <find_signature_named+0x2a>
mov    -0x1c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_signature_named>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <find_signature@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jne    1283 <find_signature_named+0x113>
lea    0x1(%r15),%r12
jmp    11d2 <find_signature_named+0x62>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
lea    0xe66(%rip),%rsi
cmp    $0x2d,%al
je     1228 <find_signature_named+0xb8>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <find_signature@plt>
test   %eax,%eax
jne    1220 <find_signature_named+0xb0>
mov    %rbx,%rdi
call   1070 <ftell@plt>
add    $0xffffffee,%eax
movslq %eax,%rbp
mov    (%r15),%r14b
test   %r14b,%r14b
je     11a0 <find_signature_named+0x30>
mov    %r12,%r13
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <fgetc@plt>
cmp    %al,%r14b
jne    1210 <find_signature_named+0xa0>
movzbl 0x0(%r13),%r14d
add    $0x1,%r13
test   %r14b,%r14b
jne    11f0 <find_signature_named+0x80>
jmp    11a0 <find_signature_named+0x30>
nopl   (%rax)
cmpb   $0x0,-0x1(%r13)
lea    0xde4(%rip),%rsi
jne    11b3 <find_signature_named+0x43>
jmp    11a0 <find_signature_named+0x30>
mov    $0xffffffff,%r14d
jmp    1283 <find_signature_named+0x113>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <fseek@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1090 <strtoul@plt>
mov    %eax,%r14d
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    %rbp,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"didn't match at %lu\0A\00"
@.str.1 = [17 x i8] c"too long at %lu\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [23 x i8] c"match at %lu, size %d\0A\00"
define i32 @find_signature_named(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [16 x i8]
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %6
store i32 -1, i32* %7
%11 = load i32*, i32** %4
%12 = call i32 @find_signature(i32* %11)
store i32 %12, i32* %5
br label %13
13: ; preds = %84, %2
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br label %20
20: ; preds = %16, %13
%21 = phi i1 [ false, %13 ], [ %19, %16 ]
br i1 %21, label %22, label %87
22: ; preds = %20
%23 = load i8*, i8** %3
store i8* %23, i8** %8
%24 = load i32*, i32** %4
%25 = call i32 @ftell(i32* %24)
%26 = sub nsw i32 %25, 18
%27 = sext i32 %26 to i64
store i64 %27, i64* %9
br label %28
28: ; preds = %43, %22
%29 = load i8*, i8** %8
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %28
%34 = load i8*, i8** %8
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = load i32*, i32** %4
%38 = call signext i8 @fgetc(i32* %37)
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %36, %39
br label %41
41: ; preds = %33, %28
%42 = phi i1 [ false, %28 ], [ %40, %33 ]
br i1 %42, label %43, label %46
43: ; preds = %41
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
br label %28
46: ; preds = %41
%47 = load i8*, i8** %8
%48 = load i8, i8* %47
%49 = icmp ne i8 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = load i64, i64* %9
%53 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i64 %52)
br label %77
54: ; preds = %46
%55 = load i32*, i32** %4
%56 = call signext i8 @fgetc(i32* %55)
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %57, 45
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = load i64, i64* %9
%62 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %61)
br label %77
63: ; preds = %54
store i32 1, i32* %6
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%65 = load i32*, i32** %4
%66 = call i32 @fread(i8* %64, i32 1, i32 16, i32* %65)
%67 = load i32*, i32** %4
%68 = load i64, i64* %9
%69 = load i32, i32* @SEEK_SET
%70 = call i32 @fseek(i32* %67, i64 %68, i32 %69)
%71 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%72 = call i32 @strtoul(i8* %71, i32* null, i32 10)
store i32 %72, i32* %7
%73 = load i32, i32* @stderr
%74 = load i64, i64* %9
%75 = load i32, i32* %7
%76 = call i32 (i32, i8*, i64, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64 %74, i32 %75)
br label %77
77: ; preds = %63, %59, %50
%78 = load i32, i32* %6
%79 = icmp ne i32 %78, 0
br i1 %79, label %84, label %80
80: ; preds = %77
%81 = load i32*, i32** %4
%82 = call i32 @find_signature(i32* %81)
store i32 %82, i32* %5
%83 = icmp ne i32 %82, 0
br label %84
84: ; preds = %80, %77
%85 = phi i1 [ true, %77 ], [ %83, %80 ]
%86 = zext i1 %85 to i32
br label %13
87: ; preds = %20
%88 = load i32, i32* %7
ret i32 %88
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"didn't match at %lu\0A\00"
@.str.1 = [17 x i8] c"too long at %lu\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [23 x i8] c"match at %lu, size %d\0A\00"
define i32 @find_signature_named(i8* nocapture %0, i32* %1) {
%3 = alloca [16 x i8]
%4 = tail call i32 @find_signature(i32* %1)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %42
6: ; preds = %2
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
br label %8
8: ; preds = %6, %36
%9 = tail call i32 @ftell(i32* %1)
%10 = add nsw i32 %9, -18
%11 = sext i32 %10 to i64
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %8, %19
%15 = phi i8 [ %21, %19 ], [ %12, %8 ]
%16 = phi i8* [ %20, %19 ], [ %0, %8 ]
%17 = tail call signext i8 @fgetc(i32* %1)
%18 = icmp eq i8 %15, %17
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = getelementptr inbounds i8, i8* %16, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %26, label %14
23: ; preds = %14
%24 = load i8, i8* %16
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %19, %8, %23
%27 = tail call signext i8 @fgetc(i32* %1)
%28 = icmp eq i8 %27, 45
br i1 %28, label %29, label %36
29: ; preds = %26
%30 = call i32 @fread(i8* %7, i32 1, i32 16, i32* %1)
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @fseek(i32* %1, i64 %11, i32 %31)
%33 = call i32 @strtoul(i8* nocapture %7, i32* null, i32 10)
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i64 %11, i32 %33)
br label %42
36: ; preds = %26, %23
%37 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), %23 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %26 ]
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, i64, ...) @fprintf(i32 %38, i8* %37, i64 %11)
%40 = tail call i32 @find_signature(i32* %1)
%41 = icmp eq i32 %40, 0
br i1 %41, label %8, label %42
42: ; preds = %36, %29, %2
%43 = phi i32 [ -1, %2 ], [ %33, %29 ], [ -1, %36 ]
ret i32 %43
}