#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 *mustmalloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strrchr(char *, char);
char *changesuffix(char *name, char *suffix) {
  char *p, *r;
  if ((p = strrchr(name, '.')) == 0) {
    r = mustmalloc(strlen(name) + strlen(suffix) + 1);
    sprintf(r, "%s%s", name, suffix);
  } else {
    r = mustmalloc(p - name + strlen(suffix) + 1);
    r[0] = '\0';
    strncat(r, name, p - name);
    strcat(r, suffix);
  }
  return (r);
}


x86 assembly (clang, -O0):
<changesuffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d3 <changesuffix+0x73>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1030 <mustmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe37(%rip),%rsi
call   1080 <sprintf@plt>
jmp    122d <changesuffix+0xcd>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1030 <mustmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1060 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<changesuffix>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11b8 <changesuffix+0x58>
mov    %rax,%rbx
sub    %r15,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   1030 <mustmalloc@plt>
mov    %rax,%r12
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <strncat@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
jmp    11f0 <changesuffix+0x90>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
add    $0x1,%rdi
call   1030 <mustmalloc@plt>
mov    %rax,%r12
lea    0xe1e(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1080 <sprintf@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define i8* @changesuffix(i8* %0, i8* %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = add i64 %6, 1
%9 = add i64 %8, %7
%10 = tail call i8* @mustmalloc(i64 %9)
%11 = tail call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %23
12: ; preds = %2
%13 = ptrtoint i8* %3 to i64
%14 = ptrtoint i8* %0 to i64
%15 = sub i64 %13, %14
%16 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%17 = add i64 %15, 1
%18 = add i64 %17, %16
%19 = tail call i8* @mustmalloc(i64 %18)
store i8 0, i8* %19
%20 = trunc i64 %15 to i32
%21 = tail call i32 @strncat(i8* %19, i8* %0, i32 %20)
%22 = tail call i32 @strcat(i8* %19, i8* %1)
br label %23
23: ; preds = %12, %5
%24 = phi i8* [ %10, %5 ], [ %19, %12 ]
ret i8* %24
}