#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 *strcat(char *, char const *);
char *strdup(char const *);
scalar_t__ strend(char const *, char const *);
scalar_t__ strlen(char const *);
char *strndup(char const *, scalar_t__);
char *convert_extension(const char *old_name, const char *old_extension,
                        const char *new_extension) {
  char *new_name;
  if (strend(old_name, old_extension)) {
    new_name = strndup(old_name, strlen(old_name) - strlen(old_extension));
    new_name = strcat(new_name, new_extension);
  } else {
    new_name = strdup(old_name);
    new_name = strcat(new_name, new_extension);
  }
  return new_name;
}


x86 assembly (clang, -O0):
<convert_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strend@plt>
cmp    $0x0,%rax
je     11c3 <convert_extension+0x73>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdi
sub    %rax,%rsi
call   1040 <strndup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x20(%rbp)
jmp    11e1 <convert_extension+0x91>
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_extension>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1050 <strend@plt>
test   %rax,%rax
je     118e <convert_extension+0x3e>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1030 <strlen@plt>
sub    %rax,%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <strndup@plt>
jmp    1196 <convert_extension+0x46>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <strcat@plt>

LLVM IR (-O0):
define i8* @convert_extension(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i64 @strend(i8* %8, i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %3
%13 = load i8*, i8** %4
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = sub nsw i64 %15, %17
%19 = call i8* @strndup(i8* %13, i64 %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = load i8*, i8** %6
%22 = call i8* @strcat(i8* %20, i8* %21)
store i8* %22, i8** %7
br label %29
23: ; preds = %3
%24 = load i8*, i8** %4
%25 = call i8* @strdup(i8* %24)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = load i8*, i8** %6
%28 = call i8* @strcat(i8* %26, i8* %27)
store i8* %28, i8** %7
br label %29
29: ; preds = %23, %12
%30 = load i8*, i8** %7
ret i8* %30
}

LLVM IR (-O2):
define i8* @convert_extension(i8* %0, i8* %1, i8* nocapture %2) {
%4 = tail call i64 @strend(i8* %0, i8* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = sub nsw i64 %7, %8
%10 = tail call i8* @strndup(i8* %0, i64 %9)
br label %13
11: ; preds = %3
%12 = tail call i8* @strdup(i8* %0)
br label %13
13: ; preds = %11, %6
%14 = phi i8* [ %12, %11 ], [ %10, %6 ]
%15 = tail call i8* @strcat(i8* dereferenceable(1) %14, i8* dereferenceable(1) %2)
ret i8* %14
}