#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 */
char *malloc(scalar_t__);
char *strcpy(char *, char *);
scalar_t__ strlen(char *);
char *strrchr(char const *, char);
scalar_t__ tolower(int);
char *misc_get_ext(const char *path, int if_lower) {
  char *p, *ret, *r;
  if ((p = strrchr(path, '.')) == NULL)
    return NULL;
  p++;
  ret = r = malloc(strlen(p) + 1);
  if (!if_lower)
    return strcpy(r, p);
  while (*p) {
    *r++ = (char)tolower((int)*p);
    p++;
  }
  *r = '\0';
  return ret;
}


x86 assembly (clang, -O0):
<misc_get_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1188 <misc_get_ext+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1222 <misc_get_ext+0xd2>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11d1 <misc_get_ext+0x81>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    1222 <misc_get_ext+0xd2>
jmp    11d6 <misc_get_ext+0x86>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1213 <misc_get_ext+0xc3>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1060 <tolower@plt>
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11d6 <misc_get_ext+0x86>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_get_ext>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11c0 <misc_get_ext+0x70>
mov    %rax,%rbx
lea    0x1(%rax),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %r15d,%r15d
je     11c5 <misc_get_ext+0x75>
mov    0x0(%rbp),%al
mov    %r14,%rbp
test   %al,%al
je     11ba <misc_get_ext+0x6a>
add    $0x2,%rbx
mov    %r14,%rbp
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1060 <tolower@plt>
mov    %al,0x0(%rbp)
add    $0x1,%rbp
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11a0 <misc_get_ext+0x50>
movb   $0x0,0x0(%rbp)
jmp    11d0 <misc_get_ext+0x80>
xor    %r14d,%r14d
jmp    11d0 <misc_get_ext+0x80>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @misc_get_ext(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i8* @strrchr(i8* %9, i8 signext 46)
store i8* %10, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i8* null, i8** %3
br label %44
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %8
store i8* %19, i8** %7
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %13
%23 = load i8*, i8** %8
%24 = load i8*, i8** %6
%25 = call i8* @strcpy(i8* %23, i8* %24)
store i8* %25, i8** %3
br label %44
26: ; preds = %13
br label %27
27: ; preds = %31, %26
%28 = load i8*, i8** %6
%29 = load i8, i8* %28
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %41
31: ; preds = %27
%32 = load i8*, i8** %6
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = call i64 @tolower(i32 %34)
%36 = trunc i64 %35 to i8
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %8
store i8 %36, i8* %37
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
br label %27
41: ; preds = %27
%42 = load i8*, i8** %8
store i8 0, i8* %42
%43 = load i8*, i8** %7
store i8* %43, i8** %3
br label %44
44: ; preds = %41, %22, %12
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
define i8* @misc_get_ext(i8* %0, i32 %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%4 = icmp eq i8* %3, null
br i1 %4, label %29, label %5
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %3, i64 1
%7 = tail call i64 @strlen(i8* %6)
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i32 %1, 0
br i1 %10, label %14, label %11
11: ; preds = %5
%12 = load i8, i8* %6
%13 = icmp eq i8 %12, 0
br i1 %13, label %27, label %16
14: ; preds = %5
%15 = tail call i8* @strcpy(i8* dereferenceable(1) %9, i8* %6)
br label %29
16: ; preds = %11, %16
%17 = phi i8 [ %25, %16 ], [ %12, %11 ]
%18 = phi i8* [ %23, %16 ], [ %9, %11 ]
%19 = phi i8* [ %24, %16 ], [ %6, %11 ]
%20 = sext i8 %17 to i32
%21 = tail call i64 @tolower(i32 %20)
%22 = trunc i64 %21 to i8
%23 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %22, i8* %18
%24 = getelementptr inbounds i8, i8* %19, i64 1
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %16
27: ; preds = %16, %11
%28 = phi i8* [ %9, %11 ], [ %23, %16 ]
store i8 0, i8* %28
br label %29
29: ; preds = %2, %27, %14
%30 = phi i8* [ %9, %27 ], [ %9, %14 ], [ null, %2 ]
ret i8* %30
}