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

/* Variables and functions */
scalar_t__ isalnum(unsigned char);
scalar_t__ strncmp(char *, char *, int);
char *strrchr(char *, char);
int directory_type(char *dir) {
  char *p;
  for (;;) {
    p = strrchr(dir, '/');
    if (p == NULL || p[1] != '\0')
      break;
    *p = '\0';
  }
  if (p == NULL)
    p = dir;
  else
    p++;
  if (strncmp(p, "man", 3) == 0) {
    p += 3;
    if (*p == '\0')
      return 0;
    while (isalnum((unsigned char)*p) || *p == '_') {
      if (*++p == '\0')
        return 1;
    }
  }
  return 2;
}


x86 assembly (clang, -O0):
<directory_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116a <directory_type+0x3a>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     116f <directory_type+0x3f>
jmp    117b <directory_type+0x4b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    113c <directory_type+0xc>
cmpq   $0x0,-0x18(%rbp)
jne    1193 <directory_type+0x63>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    119f <directory_type+0x6f>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe56(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    125b <directory_type+0x12b>
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11e6 <directory_type+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    1262 <directory_type+0x132>
jmp    11eb <directory_type+0xbb>
mov    -0x18(%rbp),%rax
movzbl (%rax),%edi
call   1040 <isalnum@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
jne    1219 <directory_type+0xe9>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1229 <directory_type+0xf9>
jmp    1256 <directory_type+0x126>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    1251 <directory_type+0x121>
movl   $0x1,-0x4(%rbp)
jmp    1262 <directory_type+0x132>
jmp    11eb <directory_type+0xbb>
jmp    125b <directory_type+0x12b>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<directory_type>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1164 <directory_type+0x34>
cmpb   $0x0,0x1(%rax)
jne    115d <directory_type+0x2d>
movb   $0x0,(%rax)
mov    %rbx,%rdi
jmp    1140 <directory_type+0x10>
add    $0x1,%rax
mov    %rax,%rbx
lea    0xe95(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
mov    $0x2,%ebp
test   %rax,%rax
jne    11b9 <directory_type+0x89>
mov    0x3(%rbx),%al
test   %al,%al
je     11b7 <directory_type+0x87>
add    $0x4,%rbx
jmp    119b <directory_type+0x6b>
nop
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     11b0 <directory_type+0x80>
movzbl %al,%edi
call   1040 <isalnum@plt>
test   %rax,%rax
jne    1190 <directory_type+0x60>
cmpb   $0x5f,-0x1(%rbx)
je     1190 <directory_type+0x60>
jmp    11b9 <directory_type+0x89>
mov    $0x1,%ebp
jmp    11b9 <directory_type+0x89>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"man\00"
define i32 @directory_type(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
br label %5
5: ; preds = %17, %1
%6 = load i8*, i8** %3
%7 = call i8* @strrchr(i8* %6, i8 signext 47)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %5
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10, %5
br label %19
17: ; preds = %10
%18 = load i8*, i8** %4
store i8 0, i8* %18
br label %5
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i8*, i8** %3
store i8* %23, i8** %4
br label %27
24: ; preds = %19
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %4
br label %27
27: ; preds = %24, %22
%28 = load i8*, i8** %4
%29 = call i64 @strncmp(i8* %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %61
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 3
store i8* %33, i8** %4
%34 = load i8*, i8** %4
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %31
store i32 0, i32* %2
br label %62
39: ; preds = %31
br label %40
40: ; preds = %59, %39
%41 = load i8*, i8** %4
%42 = load i8, i8* %41
%43 = call i64 @isalnum(i8 zeroext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %40
%46 = load i8*, i8** %4
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 95
br label %50
50: ; preds = %45, %40
%51 = phi i1 [ true, %40 ], [ %49, %45 ]
br i1 %51, label %52, label %60
52: ; preds = %50
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %4
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %52
store i32 1, i32* %2
br label %62
59: ; preds = %52
br label %40
60: ; preds = %50
br label %61
61: ; preds = %60, %27
store i32 2, i32* %2
br label %62
62: ; preds = %61, %58, %38
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
@.str = [4 x i8] c"man\00"
define i32 @directory_type(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%3 = icmp eq i8* %2, null
br i1 %3, label %14, label %4
4: ; preds = %1, %9
%5 = phi i8* [ %10, %9 ], [ %2, %1 ]
%6 = getelementptr inbounds i8, i8* %5, i64 1
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4
store i8 0, i8* %5
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %14, label %4
12: ; preds = %4
%13 = getelementptr inbounds i8, i8* %5, i64 1
br label %14
14: ; preds = %9, %12, %1
%15 = phi i8* [ %0, %1 ], [ %13, %12 ], [ %0, %9 ]
%16 = tail call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %15, i64 3
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %18, %30
%23 = phi i8 [ %32, %30 ], [ %20, %18 ]
%24 = phi i8* [ %31, %30 ], [ %19, %18 ]
%25 = tail call i64 @isalnum(i8 zeroext %23)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i8, i8* %24
%29 = icmp eq i8 %28, 95
br i1 %29, label %30, label %34
30: ; preds = %22, %27
%31 = getelementptr inbounds i8, i8* %24, i64 1
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %34, label %22
34: ; preds = %27, %30, %14, %18
%35 = phi i32 [ 0, %18 ], [ 2, %14 ], [ 2, %27 ], [ 1, %30 ]
ret i32 %35
}