#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int asprintf(char **, char *, char const *, char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die_alloc();
int /*<<< orphan*/ die_errno(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
scalar_t__ memcmp(char const *, char *, size_t);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
size_t strlen(char const *);
int name_file_matches(const char *dir, const char *subdir, const char *want) {
  char *path;
  char *buf;
  int fd;
  int res = 0;
  size_t len = strlen(want);
  ssize_t got;
  if (asprintf(&path, "%s/%s/name", dir, subdir) < 2)
    die_alloc();
  fd = open(path, O_RDONLY);
  if (fd < 0)
    die_errno("opening %s", path);
  free(path);
  buf = malloc(len + 2);
  got = read(fd, buf, len + 2);
  if (got < 0)
    die_errno("reading %s", path);
  if (got == len || (got == len + 1 && buf[len] == '\n'))
    res = (memcmp(want, buf, len) == 0);
  close(fd);
  free(buf);
  return res;
}


x86 assembly (clang, -O0):
<name_file_matches>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x20(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   10b0 <asprintf@plt>
cmp    $0x2,%eax
jge    11f0 <name_file_matches+0x50>
mov    $0x0,%al
call   1050 <die_alloc@plt>
mov    -0x20(%rbp),%rdi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    121f <name_file_matches+0x7f>
mov    -0x20(%rbp),%rsi
lea    0xdf1(%rip),%rdi
call   10a0 <die_errno@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
add    $0x2,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
add    $0x2,%rdx
call   1070 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jae    126c <name_file_matches+0xcc>
mov    -0x20(%rbp),%rsi
lea    0xdaf(%rip),%rdi
call   10a0 <die_errno@plt>
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     12a4 <name_file_matches+0x104>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jne    12c4 <name_file_matches+0x124>
mov    -0x28(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12c4 <name_file_matches+0x124>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name_file_matches>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0xe39(%rip),%rsi
lea    0x8(%rsp),%rdi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   10b0 <asprintf@plt>
cmp    $0x1,%eax
jg     11e3 <name_file_matches+0x43>
xor    %eax,%eax
call   1050 <die_alloc@plt>
mov    0x8(%rsp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    120e <name_file_matches+0x6e>
mov    0x8(%rsp),%rsi
lea    0xe02(%rip),%rdi
call   10a0 <die_errno@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    0x2(%r15),%rbp
mov    %rbp,%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
mov    %r12d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1070 <read@plt>
cmp    %r15,%rax
je     124f <name_file_matches+0xaf>
mov    %r15,%rcx
add    $0x1,%rcx
xor    %ebp,%ebp
cmp    %rcx,%rax
jne    1266 <name_file_matches+0xc6>
cmpb   $0xa,(%rbx,%r15,1)
jne    1266 <name_file_matches+0xc6>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %r12d,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"%s/%s/name\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"opening %s\00"
@.str.2 = [11 x i8] c"reading %s\00"
define i32 @name_file_matches(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %10
%13 = load i8*, i8** %6
%14 = call i64 @strlen(i8* %13)
store i64 %14, i64* %11
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = call i32 (...) @die_alloc()
br label %21
21: ; preds = %19, %3
%22 = load i8*, i8** %7
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i8*, i8** %7
%29 = call i32 @die_errno(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %21
%31 = load i8*, i8** %7
%32 = call i32 @free(i8* %31)
%33 = load i64, i64* %11
%34 = add i64 %33, 2
%35 = call i8* @malloc(i64 %34)
store i8* %35, i8** %8
%36 = load i32, i32* %9
%37 = load i8*, i8** %8
%38 = load i64, i64* %11
%39 = add i64 %38, 2
%40 = call i64 @read(i32 %36, i8* %37, i64 %39)
store i64 %40, i64* %12
%41 = load i64, i64* %12
%42 = icmp ult i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %30
%44 = load i8*, i8** %7
%45 = call i32 @die_errno(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %43, %30
%47 = load i64, i64* %12
%48 = load i64, i64* %11
%49 = icmp eq i64 %47, %48
br i1 %49, label %62, label %50
50: ; preds = %46
%51 = load i64, i64* %12
%52 = load i64, i64* %11
%53 = add i64 %52, 1
%54 = icmp eq i64 %51, %53
br i1 %54, label %55, label %69
55: ; preds = %50
%56 = load i8*, i8** %8
%57 = load i64, i64* %11
%58 = getelementptr inbounds i8, i8* %56, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 10
br i1 %61, label %62, label %69
62: ; preds = %55, %46
%63 = load i8*, i8** %6
%64 = load i8*, i8** %8
%65 = load i64, i64* %11
%66 = call i64 @memcmp(i8* %63, i8* %64, i64 %65)
%67 = icmp eq i64 %66, 0
%68 = zext i1 %67 to i32
store i32 %68, i32* %10
br label %69
69: ; preds = %62, %55, %50
%70 = load i32, i32* %9
%71 = call i32 @close(i32 %70)
%72 = load i8*, i8** %8
%73 = call i32 @free(i8* %72)
%74 = load i32, i32* %10
ret i32 %74
}

LLVM IR (-O2):
@.str = [11 x i8] c"%s/%s/name\00"
@O_RDONLY = global i32 0
@.str.1 = [11 x i8] c"opening %s\00"
define i32 @name_file_matches(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = call i32 (...) @die_alloc()
br label %11
11: ; preds = %9, %3
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = call i32 @die_errno(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %11
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
%22 = add i64 %6, 2
%23 = call i8* @malloc(i64 %22)
%24 = call i64 @read(i32 %14, i8* %23, i64 %22)
%25 = icmp eq i64 %24, %6
br i1 %25, label %33, label %26
26: ; preds = %19
%27 = add i64 %6, 1
%28 = icmp eq i64 %24, %27
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = getelementptr inbounds i8, i8* %23, i64 %6
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 10
br i1 %32, label %33, label %37
33: ; preds = %29, %19
%34 = call i64 @memcmp(i8* %2, i8* %23, i64 %6)
%35 = icmp eq i64 %34, 0
%36 = zext i1 %35 to i32
br label %37
37: ; preds = %33, %29, %26
%38 = phi i32 [ %36, %33 ], [ 0, %29 ], [ 0, %26 ]
%39 = call i32 @close(i32 %14)
%40 = call i32 @free(i8* %23)
ret i32 %38
}