#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 strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
void replace_ext(char *buffer, int buflen, char *fname, char *fext) {
  int fnamelen;
  char *targ;
  strncpy(buffer, fname, buflen - 1);
  *(buffer + buflen - 1) = '\0';
  fnamelen = strlen(buffer);
  if ((targ = strrchr(buffer, '.')) == NULL) {
    strncpy(buffer + fnamelen, fext, buflen - fnamelen - 1);
    *(buffer + buflen - 1) = '\0';
  } else {
    while (*targ != 0 && *fext != 0)
      *targ++ = *fext++;
    if (*fext == 0)
      *targ = 0;
  }
  return;
}


x86 assembly (clang, -O0):
<replace_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11be <replace_ext+0x8e>
mov    -0x8(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
sub    -0x24(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    123b <replace_ext+0x10b>
jmp    11c3 <replace_ext+0x93>
mov    -0x30(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
je     11e8 <replace_ext+0xb8>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11f8 <replace_ext+0xc8>
jmp    121f <replace_ext+0xef>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    11c3 <replace_ext+0x93>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1236 <replace_ext+0x106>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    123b <replace_ext+0x10b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_ext>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %esi,%ebx
mov    %rdi,%r15
lea    -0x1(%rbx),%eax
mov    %rdx,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
movslq %ebx,%r12
movb   $0x0,-0x1(%r15,%r12,1)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     119d <replace_ext+0x6d>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11c0 <replace_ext+0x90>
nopw   0x0(%rax,%rax,1)
movzbl (%r14),%ecx
test   %cl,%cl
je     11cc <replace_ext+0x9c>
add    $0x1,%r14
lea    0x1(%rbx),%rax
mov    %cl,(%rbx)
cmpb   $0x0,0x1(%rbx)
mov    %rax,%rbx
jne    1180 <replace_ext+0x50>
jmp    11c3 <replace_ext+0x93>
lea    (%r15,%r12,1),%rbx
add    $0xffffffffffffffff,%rbx
movslq %ebp,%rax
add    %rax,%r15
not    %eax
add    %eax,%r12d
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
jmp    11cc <replace_ext+0x9c>
mov    %rbx,%rax
cmpb   $0x0,(%r14)
mov    %rax,%rbx
jne    11cf <replace_ext+0x9f>
movb   $0x0,(%rbx)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @replace_ext(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = add nsw i32 %1, -1
%6 = tail call i32 @strncpy(i8* %0, i8* %2, i32 %5)
%7 = sext i32 %1 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 -1
store i8 0, i8* %9
%10 = tail call i32 @strlen(i8* %0)
%11 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %4
%14 = load i8, i8* %11
%15 = icmp eq i8 %14, 0
br i1 %15, label %32, label %22
16: ; preds = %4
%17 = sext i32 %10 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = xor i32 %10, -1
%20 = add i32 %19, %1
%21 = tail call i32 @strncpy(i8* %18, i8* %3, i32 %20)
br label %37
22: ; preds = %13, %27
%23 = phi i8* [ %29, %27 ], [ %11, %13 ]
%24 = phi i8* [ %28, %27 ], [ %3, %13 ]
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %22
%28 = getelementptr inbounds i8, i8* %24, i64 1
%29 = getelementptr inbounds i8, i8* %23, i64 1
store i8 %25, i8* %23
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %22
32: ; preds = %27, %13
%33 = phi i8* [ %3, %13 ], [ %28, %27 ]
%34 = phi i8* [ %11, %13 ], [ %29, %27 ]
%35 = load i8, i8* %33
%36 = icmp eq i8 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %22, %32, %16
%38 = phi i8* [ %9, %16 ], [ %34, %32 ], [ %23, %22 ]
store i8 0, i8* %38
br label %39
39: ; preds = %37, %32
ret void
}