#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 */
int strlen(char const *);
scalar_t__ strncmp(char const *, char *, size_t const);
char *get_tag(const char *filename) {
  const char *p;
  const size_t seplen = sizeof("__") - 1;
  p = filename + strlen(filename);
  while (p-- > filename) {
    if (strncmp(p, "__", seplen) == 0)
      return (char *)p + seplen;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_tag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x2,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    -0x10(%rbp),%rax
jbe    11a3 <get_tag+0x83>
mov    -0x18(%rbp),%rdi
lea    0xe87(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    119e <get_tag+0x7e>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x8(%rbp)
jmp    11ab <get_tag+0x8b>
jmp    1155 <get_tag+0x35>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_tag>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1040 <strlen@plt>
movslq %eax,%rbx
add    %r15,%rbx
xor    %r14d,%r14d
lea    0xec0(%rip),%r12
cmp    %r15,%rbx
jbe    1165 <get_tag+0x45>
add    $0xffffffffffffffff,%rbx
mov    $0x2,%edx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strncmp@plt>
test   %rax,%rax
jne    1140 <get_tag+0x20>
add    $0x2,%rbx
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"__\00"
define i8* @get_tag(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %3
store i64 2, i64* %5
%6 = load i8*, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %6, i64 %9
store i8* %10, i8** %4
br label %11
11: ; preds = %23, %1
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i32 -1
store i8* %13, i8** %4
%14 = load i8*, i8** %3
%15 = icmp ugt i8* %12, %14
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 2
store i8* %22, i8** %2
br label %25
23: ; preds = %16
br label %11
24: ; preds = %11
store i8* null, i8** %2
br label %25
25: ; preds = %24, %20
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
@.str = [3 x i8] c"__\00"
define i8* @get_tag(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = sext i32 %2 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
br label %5
5: ; preds = %9, %1
%6 = phi i8* [ %4, %1 ], [ %7, %9 ]
%7 = getelementptr inbounds i8, i8* %6, i64 -1
%8 = icmp ugt i8* %6, %0
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = tail call i64 @strncmp(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %5
12: ; preds = %9
%13 = getelementptr inbounds i8, i8* %6, i64 1
br label %14
14: ; preds = %5, %12
%15 = phi i8* [ %13, %12 ], [ null, %5 ]
ret i8* %15
}