#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 *basename(char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
char *strdup(char const *);
int strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char *, int);
char *strrchr(char *, char);
char *suffix(const char *filename, const char *ext) {
  char *copy = strdup(filename);
  char *base = basename(copy);
  int len = strlen(base) + 1 + strlen(ext) + 1;
  char *rv = malloc(len);
  char *dot;
  if ((dot = strrchr(base, '.')) && dot + 4 < base + len &&
      !strncmp(1 + dot, "dwg", 3))
    *dot = '\0';
  snprintf(rv, len, "%s.%s", base, ext);
  free(copy);
  return rv;
}


x86 assembly (clang, -O0):
<suffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <basename@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     123c <suffix+0xbc>
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    123c <suffix+0xbc>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
lea    0xdde(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    123c <suffix+0xbc>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xdb2(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<suffix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
call   10a0 <strdup@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1080 <basename@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%r13d
add    $0x2,%r13d
mov    %r13d,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     120a <suffix+0x8a>
mov    %rax,%rbp
add    $0x4,%rax
movslq %r13d,%rcx
mov    %rbx,%rdx
add    %rcx,%rdx
cmp    %rdx,%rax
jae    120a <suffix+0x8a>
lea    0x1(%rbp),%rdi
lea    0xe08(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    120a <suffix+0x8a>
movb   $0x0,0x0(%rbp)
lea    0xdf3(%rip),%rdx
mov    %r12,%rdi
mov    %r13d,%esi
mov    %rbx,%rcx
mov    %r14,%r8
call   1060 <snprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"dwg\00"
@.str.1 = [6 x i8] c"%s.%s\00"
define i8* @suffix(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = call i8* @basename(i8* %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %16, %18
%20 = add nsw i32 %19, 1
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %8
%23 = load i8*, i8** %6
%24 = call i8* @strrchr(i8* %23, i8 signext 46)
store i8* %24, i8** %9
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %41
26: ; preds = %2
%27 = load i8*, i8** %9
%28 = getelementptr inbounds i8, i8* %27, i64 4
%29 = load i8*, i8** %6
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = icmp ult i8* %28, %32
br i1 %33, label %34, label %41
34: ; preds = %26
%35 = load i8*, i8** %9
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = call i32 @strncmp(i8* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%38 = icmp ne i32 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %34
%40 = load i8*, i8** %9
store i8 0, i8* %40
br label %41
41: ; preds = %39, %34, %26, %2
%42 = load i8*, i8** %8
%43 = load i32, i32* %7
%44 = load i8*, i8** %6
%45 = load i8*, i8** %4
%46 = call i32 @snprintf(i8* %42, i32 %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %44, i8* %45)
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %8
ret i8* %49
}

LLVM IR (-O2):
@.str = [4 x i8] c"dwg\00"
@.str.1 = [6 x i8] c"%s.%s\00"
define i8* @suffix(i8* nocapture %0, i8* %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @basename(i8* %3)
%5 = tail call i32 @strlen(i8* %4)
%6 = tail call i32 @strlen(i8* %1)
%7 = add i32 %5, 2
%8 = add i32 %7, %6
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %4, i8 signext 46)
%11 = icmp ne i8* %10, null
%12 = getelementptr inbounds i8, i8* %10, i64 4
%13 = sext i32 %8 to i64
%14 = getelementptr inbounds i8, i8* %4, i64 %13
%15 = icmp ult i8* %12, %14
%16 = select i1 %11, i1 %15, i1 false
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = getelementptr inbounds i8, i8* %10, i64 1
%19 = tail call i32 @strncmp(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i8 0, i8* %10
br label %22
22: ; preds = %21, %17, %2
%23 = tail call i32 @snprintf(i8* %9, i32 %8, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %1)
%24 = tail call i32 @free(i8* %3)
ret i8* %9
}