#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(int);
int /*<<< orphan*/ strcmp(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *resuffix(const char *name, const char *oldsuff, const char *newsuff) {
  int namelen = strlen(name);
  int oldsufflen = strlen(oldsuff);
  int newsufflen = strlen(newsuff);
  char *newname = malloc(namelen + newsufflen + 2);
  strcpy(newname, name);
  if (namelen > oldsufflen && !strcmp(newname + namelen - oldsufflen, oldsuff))
    strcpy(newname + namelen - oldsufflen, newsuff);
  else
    strcpy(newname + namelen, newsuff);
  return newname;
}


x86 assembly (clang, -O0):
<resuffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
add    -0x24(%rbp),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    11f1 <resuffix+0xb1>
mov    -0x30(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
movslq -0x20(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11f1 <resuffix+0xb1>
mov    -0x30(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
movslq -0x20(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1205 <resuffix+0xc5>
mov    -0x30(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resuffix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r13,%rsi
call   1030 <strcpy@plt>
movslq %ebx,%r13
cmp    %r12d,%ebx
jle    11aa <resuffix+0x6a>
mov    %rbp,%rbx
add    %r13,%rbx
movslq %r12d,%rax
sub    %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11b0 <resuffix+0x70>
mov    %rbp,%rbx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @resuffix(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %8
%15 = load i8*, i8** %6
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %9
%17 = load i32, i32* %7
%18 = load i32, i32* %9
%19 = add nsw i32 %17, %18
%20 = add nsw i32 %19, 2
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = load i8*, i8** %4
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %51
28: ; preds = %3
%29 = load i8*, i8** %10
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = sub i64 0, %34
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i8*, i8** %5
%38 = call i32 @strcmp(i8* %36, i8* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %51, label %40
40: ; preds = %28
%41 = load i8*, i8** %10
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = sub i64 0, %46
%48 = getelementptr inbounds i8, i8* %44, i64 %47
%49 = load i8*, i8** %6
%50 = call i32 @strcpy(i8* %48, i8* %49)
br label %58
51: ; preds = %28, %3
%52 = load i8*, i8** %10
%53 = load i32, i32* %7
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8*, i8** %6
%57 = call i32 @strcpy(i8* %55, i8* %56)
br label %58
58: ; preds = %51, %40
%59 = load i8*, i8** %10
ret i8* %59
}

LLVM IR (-O2):
define i8* @resuffix(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32 @strlen(i8* %2)
%7 = add i32 %4, 2
%8 = add i32 %7, %6
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i32 @strcpy(i8* %9, i8* %0)
%11 = icmp sgt i32 %4, %5
%12 = sext i32 %4 to i64
br i1 %11, label %13, label %20
13: ; preds = %3
%14 = getelementptr inbounds i8, i8* %9, i64 %12
%15 = sext i32 %5 to i64
%16 = sub nsw i64 0, %15
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = tail call i32 @strcmp(i8* dereferenceable(1) %17, i8* dereferenceable(1) %1)
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %3, %13
%21 = getelementptr inbounds i8, i8* %9, i64 %12
br label %22
22: ; preds = %13, %20
%23 = phi i8* [ %21, %20 ], [ %17, %13 ]
%24 = tail call i32 @strcpy(i8* %23, i8* %2)
ret i8* %9
}