#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 *alloc(unsigned int);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
char *strrchr(char *, char);
char *base_filename_with_suffix(char *name, char *suffix) {
  char *p1, *p2;
  unsigned int n, sn;
  char *buf;
  if (suffix)
    sn = strlen(suffix) + 1;
  else
    sn = 0;
  p1 = strrchr(name, '/');
  if (p1)
    p1++;
  else
    p1 = name;
  p2 = strchr(p1, '.');
  if (p2)
    n = p2 - p1;
  else
    n = strlen(p1);
  buf = alloc(n + sn + 1);
  strncpy(buf, p1, n);
  if (suffix) {
    buf[n] = '.';
    strcpy(buf + n + 1, suffix);
  } else
    buf[n] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<base_filename_with_suffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     118f <base_filename_with_suffix+0x2f>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1196 <base_filename_with_suffix+0x36>
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c4 <base_filename_with_suffix+0x64>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11cc <base_filename_with_suffix+0x6c>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fc <base_filename_with_suffix+0x9c>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    1208 <base_filename_with_suffix+0xa8>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    -0x28(%rbp),%edi
add    $0x1,%edi
call   1080 <alloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x10(%rbp)
je     125c <base_filename_with_suffix+0xfc>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1267 <base_filename_with_suffix+0x107>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<base_filename_with_suffix>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbp
test   %rsi,%rsi
je     1183 <base_filename_with_suffix+0x23>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
add    $0x2,%r15d
jmp    1189 <base_filename_with_suffix+0x29>
mov    $0x1,%r15d
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %rbp,%rbx
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11ba <base_filename_with_suffix+0x5a>
mov    %rax,%rbp
sub    %ebx,%ebp
jmp    11c4 <base_filename_with_suffix+0x64>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
add    %ebp,%r15d
mov    %r15d,%edi
call   1080 <alloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %ebp,%edi
add    %r15,%rdi
test   %r14,%r14
je     11fa <base_filename_with_suffix+0x9a>
movb   $0x2e,(%rdi)
add    $0x1,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    11fd <base_filename_with_suffix+0x9d>
movb   $0x0,(%rdi)
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @base_filename_with_suffix(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
store i32 %15, i32* %8
br label %17
16: ; preds = %2
store i32 0, i32* %8
br label %17
17: ; preds = %16, %12
%18 = load i8*, i8** %3
%19 = call i8* @strrchr(i8* %18, i8 signext 47)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
br label %27
25: ; preds = %17
%26 = load i8*, i8** %3
store i8* %26, i8** %5
br label %27
27: ; preds = %25, %22
%28 = load i8*, i8** %5
%29 = call i8* @strchr(i8* %28, i8 signext 46)
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %39
32: ; preds = %27
%33 = load i8*, i8** %6
%34 = load i8*, i8** %5
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
store i32 %38, i32* %7
br label %42
39: ; preds = %27
%40 = load i8*, i8** %5
%41 = call i32 @strlen(i8* %40)
store i32 %41, i32* %7
br label %42
42: ; preds = %39, %32
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = add i32 %43, %44
%46 = add i32 %45, 1
%47 = call i8* @alloc(i32 %46)
store i8* %47, i8** %9
%48 = load i8*, i8** %9
%49 = load i8*, i8** %5
%50 = load i32, i32* %7
%51 = call i32 @strncpy(i8* %48, i8* %49, i32 %50)
%52 = load i8*, i8** %4
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %66
54: ; preds = %42
%55 = load i8*, i8** %9
%56 = load i32, i32* %7
%57 = zext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 46, i8* %58
%59 = load i8*, i8** %9
%60 = load i32, i32* %7
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = getelementptr inbounds i8, i8* %62, i64 1
%64 = load i8*, i8** %4
%65 = call i32 @strcpy(i8* %63, i8* %64)
br label %71
66: ; preds = %42
%67 = load i8*, i8** %9
%68 = load i32, i32* %7
%69 = zext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
store i8 0, i8* %70
br label %71
71: ; preds = %66, %54
%72 = load i8*, i8** %9
ret i8* %72
}

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