#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*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int,
                           char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ memcmp(char const *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int match(const char *filename, const char *prefix, int preflen, char *buf) {
  FILE *input;
  input = fopen(filename, "rb");
  if (errno) {
    fprintf(stderr, "Can't open file '%s' (err=%d): %s\n", filename, errno,
            strerror(errno));
    return 0;
  }
  if (fread(buf, 1, preflen, input) != preflen) {
    fclose(input);
    return 0;
  }
  fclose(input);
  return (memcmp(prefix, buf, preflen) == 0);
}


x86 assembly (clang, -O0):
<match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e46(%rip),%rax
cmpl   $0x0,(%rax)
je     11ee <match+0x8e>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x34(%rbp),%ecx
mov    %rax,%r8
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1249 <match+0xe9>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x1c(%rbp),%eax
je     1221 <match+0xc1>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1249 <match+0xe9>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe82(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r13
mov    0x2e4b(%rip),%rax
mov    (%rax),%ebp
test   %ebp,%ebp
je     11bf <match+0x5f>
mov    0x2e46(%rip),%rax
mov    (%rax),%r14d
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe58(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %ebp,%ecx
mov    %rax,%r8
call   1060 <fprintf@plt>
xor    %eax,%eax
jmp    11fc <match+0x9c>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12d,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
cmp    %r12d,%ebx
jne    11fc <match+0x9c>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <memcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"Can't open file '%s' (err=%d): %s\0A\00"
define i32 @match(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%11 = load i8*, i8** %6
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %10
%13 = load i32, i32* @errno
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %6
%18 = load i32, i32* @errno
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %17, i32 %18, i8* %20)
store i32 0, i32* %5
br label %41
22: ; preds = %4
%23 = load i8*, i8** %9
%24 = load i32, i32* %8
%25 = load i32*, i32** %10
%26 = call i32 @fread(i8* %23, i32 1, i32 %24, i32* %25)
%27 = load i32, i32* %8
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32*, i32** %10
%31 = call i32 @fclose(i32* %30)
store i32 0, i32* %5
br label %41
32: ; preds = %22
%33 = load i32*, i32** %10
%34 = call i32 @fclose(i32* %33)
%35 = load i8*, i8** %7
%36 = load i8*, i8** %9
%37 = load i32, i32* %8
%38 = call i64 @memcmp(i8* %35, i8* %36, i32 %37)
%39 = icmp eq i64 %38, 0
%40 = zext i1 %39 to i32
store i32 %40, i32* %5
br label %41
41: ; preds = %32, %29, %15
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"Can't open file '%s' (err=%d): %s\0A\00"
define i32 @match(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @errno
%7 = icmp eq i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i8* @strerror(i32 %6)
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %6, i8* %10)
br label %20
12: ; preds = %4
%13 = tail call i32 @fread(i8* %3, i32 1, i32 %2, i32* %5)
%14 = icmp eq i32 %13, %2
%15 = tail call i32 @fclose(i32* %5)
br i1 %14, label %16, label %20
16: ; preds = %12
%17 = tail call i64 @memcmp(i8* %1, i8* %3, i32 %2)
%18 = icmp eq i64 %17, 0
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %12, %16, %8
%21 = phi i32 [ 0, %8 ], [ %19, %16 ], [ 0, %12 ]
ret i32 %21
}