#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*/ assert(int);
int /*<<< orphan*/ die(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *replace_ext(const char *str, const char *ext, size_t extlen) {
  size_t i, len;
  char *new;
  len = strlen(str);
  assert(extlen <= len);
  new = malloc(len + 1);
  if (!new)
    die("Unable to allocate memory");
  memcpy(new, str, len - extlen);
  for (i = 0; i < extlen; i++)
    new[i + len - extlen] = ext[i];
  new[len] = '\0';
  return new;
}


x86 assembly (clang, -O0):
<replace_ext>:
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   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ae <replace_ext+0x5e>
lea    0xe57(%rip),%rdi
call   1070 <die@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
sub    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1208 <replace_ext+0xb8>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
sub    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11cb <replace_ext+0x7b>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_ext>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r13
call   1030 <strlen@plt>
mov    %rax,%r12
xor    %edi,%edi
mov    %rax,%rbx
sub    %r15,%rbx
setae  %dil
call   1040 <assert@plt>
lea    0x1(%r12),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    119b <replace_ext+0x4b>
lea    0xe6a(%rip),%rdi
call   1070 <die@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
test   %r15,%r15
je     11bf <replace_ext+0x6f>
mov    %rbp,%rdi
add    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
movb   $0x0,0x0(%rbp,%r12,1)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Unable to allocate memory\00"
define i8* @replace_ext(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
store i64 %11, i64* %8
%12 = load i64, i64* %6
%13 = load i64, i64* %8
%14 = icmp ule i64 %12, %13
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i64, i64* %8
%18 = add i64 %17, 1
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %24, label %22
22: ; preds = %3
%23 = call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %3
%25 = load i8*, i8** %9
%26 = load i8*, i8** %4
%27 = load i64, i64* %8
%28 = load i64, i64* %6
%29 = sub i64 %27, %28
%30 = call i32 @memcpy(i8* %25, i8* %26, i64 %29)
store i64 0, i64* %7
br label %31
31: ; preds = %47, %24
%32 = load i64, i64* %7
%33 = load i64, i64* %6
%34 = icmp ult i64 %32, %33
br i1 %34, label %35, label %50
35: ; preds = %31
%36 = load i8*, i8** %5
%37 = load i64, i64* %7
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %9
%41 = load i64, i64* %7
%42 = load i64, i64* %8
%43 = add i64 %41, %42
%44 = load i64, i64* %6
%45 = sub i64 %43, %44
%46 = getelementptr inbounds i8, i8* %40, i64 %45
store i8 %39, i8* %46
br label %47
47: ; preds = %35
%48 = load i64, i64* %7
%49 = add i64 %48, 1
store i64 %49, i64* %7
br label %31
50: ; preds = %31
%51 = load i8*, i8** %9
%52 = load i64, i64* %8
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %9
ret i8* %54
}

LLVM IR (-O2):
@.str = [26 x i8] c"Unable to allocate memory\00"
define i8* @replace_ext(i8* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = icmp uge i64 %4, %2
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = add i64 %4, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = sub i64 %4, %2
%15 = tail call i32 @memcpy(i8* %9, i8* %0, i64 %14)
%16 = icmp eq i64 %2, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = getelementptr i8, i8* %9, i64 %14
br label %19
19: ; preds = %17, %13
%20 = getelementptr inbounds i8, i8* %9, i64 %4
store i8 0, i8* %20
ret i8* %9
}