#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 const);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
scalar_t__ strncmp(char const *, char const *, int const);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *change_path(const char *old_name, const char *old_dir,
                  const char *new_dir, int prefix_len) {
  const int new_dir_len = strlen(new_dir);
  const int old_dir_len = strlen(old_dir);
  const int old_name_len = strlen(old_name);
  if (strncmp(old_dir, old_name + prefix_len, old_dir_len) != 0) {
    return (char *)old_name;
  }
  const int new_len = new_dir_len + old_name_len - old_dir_len + 1;
  char *new_name = malloc(new_len + 1);
  strncpy(new_name, old_name, prefix_len);
  strcpy(new_name + prefix_len, new_dir);
  strcpy(new_name + prefix_len + new_dir_len,
         old_name + prefix_len + old_dir_len);
  new_name[new_len] = 0;
  return new_name;
}


x86 assembly (clang, -O0):
<change_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     11b9 <change_path+0x69>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1238 <change_path+0xe8>
mov    -0x28(%rbp),%eax
add    -0x30(%rbp),%eax
sub    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x40(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
call   1050 <strcpy@plt>
mov    -0x40(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %rdx,0x10(%rsp)
mov    %rdx,%rdi
call   1060 <strlen@plt>
mov    %eax,0x4(%rsp)
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
movslq %ebx,%rbx
lea    (%rbx,%rbp,1),%r13
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    120c <change_path+0xbc>
mov    0x4(%rsp),%r15d
mov    %r15d,%eax
sub    %r12d,%eax
lea    (%rax,%r14,1),%ecx
movslq %ecx,%rcx
mov    %rcx,0x8(%rsp)
lea    (%rax,%r14,1),%edi
add    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
add    %r14,%rbx
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   1050 <strcpy@plt>
movslq %r15d,%rdi
add    %rbx,%rdi
movslq %r12d,%rax
add    %rax,%r13
mov    %r13,%rsi
call   1050 <strcpy@plt>
mov    0x8(%rsp),%rax
movb   $0x0,0x1(%rax,%r14,1)
mov    %r14,%rbp
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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