#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*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ strncat(char *, char *, scalar_t__);
char *strrchr(char *, char);
char *replace_path_ext(char const *pathname) {
  char *ext = ".cue";
  char *tempstring = malloc(strlen(pathname) + 1);
  if (tempstring == NULL) {
    perror("malloc");
    return NULL;
  }
  strcpy(tempstring, pathname);
  strcpy(strrchr(tempstring, '.'), "");
  char *cuepath = malloc(strlen(tempstring) + strlen(ext) + 1);
  if (cuepath == NULL) {
    perror("malloc");
    return NULL;
  }
  strcpy(cuepath, tempstring);
  strncat(cuepath, ext, strlen(ext));
  free(tempstring);
  return cuepath;
}


x86 assembly (clang, -O0):
<replace_path_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7d(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <replace_path_ext+0x54>
lea    0xe53(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1283 <replace_path_ext+0x113>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,%rdi
lea    0xe22(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123c <replace_path_ext+0xcc>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1283 <replace_path_ext+0x113>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
call   1070 <strncat@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_path_ext>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11ff <replace_path_ext+0x8f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
lea    0xe5f(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x5(%rax),%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     11ff <replace_path_ext+0x8f>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe21(%rip),%rsi
mov    $0x4,%edx
mov    %r14,%rdi
call   1070 <strncat@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
xor    %r14d,%r14d
jmp    11f4 <replace_path_ext+0x84>

LLVM IR (-O0):
@.str = [5 x i8] c".cue\00"
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @replace_path_ext(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %4
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %8, 1
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %45
15: ; preds = %1
%16 = load i8*, i8** %5
%17 = load i8*, i8** %3
%18 = call i32 @strcpy(i8* %16, i8* %17)
%19 = load i8*, i8** %5
%20 = call i8* @strrchr(i8* %19, i8 signext 46)
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%22 = load i8*, i8** %5
%23 = call i64 @strlen(i8* %22)
%24 = load i8*, i8** %4
%25 = call i64 @strlen(i8* %24)
%26 = add nsw i64 %23, %25
%27 = add nsw i64 %26, 1
%28 = call i8* @malloc(i64 %27)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %45
33: ; preds = %15
%34 = load i8*, i8** %6
%35 = load i8*, i8** %5
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = load i8*, i8** %6
%38 = load i8*, i8** %4
%39 = load i8*, i8** %4
%40 = call i64 @strlen(i8* %39)
%41 = call i32 @strncat(i8* %37, i8* %38, i64 %40)
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i8*, i8** %6
store i8* %44, i8** %2
br label %45
45: ; preds = %33, %31, %13
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@.str = [5 x i8] c".cue\00"
@.str.1 = [7 x i8] c"malloc\00"
@.str.2 = [1 x i8] zeroinitializer
define i8* @replace_path_ext(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add nsw i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
8: ; preds = %1
%9 = tail call i32 @strcpy(i8* %4, i8* %0)
%10 = tail call i8* @strrchr(i8* %4, i8 signext 46)
%11 = tail call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i64 @strlen(i8* %4)
%13 = add nsw i64 %12, 5
%14 = tail call i8* @malloc(i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
18: ; preds = %8
%19 = tail call i32 @strcpy(i8* %14, i8* %4)
%20 = tail call i32 @strncat(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 4)
%21 = tail call i32 @free(i8* %4)
br label %22
22: ; preds = %16, %18, %6
%23 = phi i8* [ null, %6 ], [ null, %16 ], [ %14, %18 ]
ret i8* %23
}