#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 strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *fname_with_ext(const char *fname, const char *ext) {
  int len = strlen(fname);
  if (len > 4 && fname[len - 4] == '.') {
    char *buf = malloc(len + 1);
    strncpy(buf, fname, len + 1);
    strncpy(buf + (len - 3), ext, 3);
    return buf;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<fname_with_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x4,-0x1c(%rbp)
jle    11bc <fname_with_ext+0x8c>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    11bc <fname_with_ext+0x8c>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
sub    $0x3,%eax
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c4 <fname_with_ext+0x94>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fname_with_ext>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <strlen@plt>
cmp    $0x5,%eax
jl     1186 <fname_with_ext+0x56>
mov    %eax,%ebx
lea    -0x4(%rbx),%eax
cmpb   $0x2e,(%r12,%rax,1)
jne    1186 <fname_with_ext+0x56>
lea    0x1(%rbx),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
add    $0xfffffffd,%ebx
add    %r15,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
jmp    1189 <fname_with_ext+0x59>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @fname_with_ext(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %3, 4
br i1 %4, label %5, label %19
5: ; preds = %2
%6 = add nsw i32 %3, -4
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 46
br i1 %10, label %11, label %19
11: ; preds = %5
%12 = add nuw nsw i32 %3, 1
%13 = tail call i8* @malloc(i32 %12)
%14 = tail call i32 @strncpy(i8* %13, i8* %0, i32 %12)
%15 = add nsw i32 %3, -3
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = tail call i32 @strncpy(i8* %17, i8* %1, i32 3)
br label %19
19: ; preds = %2, %5, %11
%20 = phi i8* [ %13, %11 ], [ null, %5 ], [ null, %2 ]
ret i8* %20
}