#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 */
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
scalar_t__ xmalloc(int);
char *replace_suffix(const char *filename, const char *new_suffix) {
  char *rs;
  int fl, n, sl;
  sl = strlen(new_suffix);
  fl = n = strlen(filename);
  while (filename[n] != '.' && n >= 0)
    n--;
  if (n == -1) {
    rs = (char *)xmalloc((fl + sl + 2) * sizeof(char));
    strcpy(rs, filename);
    strcat(rs, new_suffix);
  } else {
    rs = (char *)xmalloc((n + sl + 2) * sizeof(char));
    strncpy(rs, filename, n);
    rs[n] = '\0';
    strcat(rs, new_suffix);
  }
  return rs;
}


x86 assembly (clang, -O0):
<replace_suffix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x2e,%ecx
mov    %al,-0x25(%rbp)
je     119f <replace_suffix+0x4f>
cmpl   $0x0,-0x20(%rbp)
setge  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11af <replace_suffix+0x5f>
jmp    11bd <replace_suffix+0x6d>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    117b <replace_suffix+0x2b>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1200 <replace_suffix+0xb0>
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
jmp    1243 <replace_suffix+0xf3>
mov    -0x20(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_suffix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
lea    (%r15,%rbx,1),%rcx
xor    %edx,%edx
nopl   (%rax)
lea    -0x1(%rdx),%rbp
cmpb   $0x2e,(%rcx,%rdx,1)
je     1192 <replace_suffix+0x42>
add    %rbx,%rdx
mov    %rbp,%rdx
jns    1180 <replace_suffix+0x30>
mov    %eax,%ecx
lea    (%rcx,%rbp,1),%rdx
cmp    $0xfffffffe,%edx
jne    11bc <replace_suffix+0x6c>
add    $0x2,%r13d
add    %eax,%r13d
mov    %r13d,%edi
call   1060 <xmalloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
jmp    11ea <replace_suffix+0x9a>
lea    (%rcx,%rbp,1),%r12
add    $0x1,%r12
add    %eax,%r13d
lea    0x3(%rbp,%r13,1),%edi
call   1060 <xmalloc@plt>
mov    %rax,%r13
add    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x1(%rbp,%rbx,1)
mov    %r13,%rdi
mov    %r14,%rsi
call   1070 <strcat@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @replace_suffix(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
br label %6
6: ; preds = %6, %2
%7 = phi i64 [ %13, %6 ], [ %5, %2 ]
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 46
%11 = icmp sgt i64 %7, -1
%12 = select i1 %10, i1 %11, i1 false
%13 = add nsw i64 %7, -1
br i1 %12, label %6, label %14
14: ; preds = %6
%15 = trunc i64 %7 to i32
%16 = icmp eq i32 %15, -1
%17 = add i32 %3, 2
br i1 %16, label %18, label %23
18: ; preds = %14
%19 = add i32 %17, %4
%20 = tail call i64 @xmalloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 @strcpy(i8* %21, i8* %0)
br label %29
23: ; preds = %14
%24 = add i32 %17, %15
%25 = tail call i64 @xmalloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
%27 = tail call i32 @strncpy(i8* %26, i8* %0, i32 %15)
%28 = getelementptr inbounds i8, i8* %26, i64 %7
store i8 0, i8* %28
br label %29
29: ; preds = %23, %18
%30 = phi i8* [ %26, %23 ], [ %21, %18 ]
%31 = tail call i32 @strcat(i8* %30, i8* %1)
ret i8* %30
}