#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 /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strcspn(char const *, char *);
size_t strlen(char const *);
char *replace_wildcard(const char *match_str, const char *new_substr) {
  char *new_str;
  size_t star_idx;
  size_t match_str_len;
  size_t new_substr_len;
  assert(match_str != NULL);
  assert(new_substr != NULL);
  match_str_len = strlen(match_str);
  new_substr_len = strlen(new_substr);
  if ((new_str = malloc(match_str_len + new_substr_len + 1)) == NULL) {
    return NULL;
  }
  star_idx = strcspn(match_str, "*");
  if ((star_idx == 0) || (star_idx >= match_str_len))
    return NULL;
  memcpy(new_str, match_str, star_idx);
  memcpy(new_str + star_idx, new_substr, new_substr_len);
  memcpy(new_str + star_idx + new_substr_len, match_str + star_idx + 1,
         match_str_len - star_idx);
  new_str[match_str_len + new_substr_len] = '\0';
  return new_str;
}


x86 assembly (clang, -O0):
<replace_wildcard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ca <replace_wildcard+0x7a>
movq   $0x0,-0x8(%rbp)
jmp    1267 <replace_wildcard+0x117>
mov    -0x10(%rbp),%rdi
lea    0xe2b(%rip),%rsi
call   1040 <strcspn@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f7 <replace_wildcard+0xa7>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     1204 <replace_wildcard+0xb4>
movq   $0x0,-0x8(%rbp)
jmp    1267 <replace_wildcard+0x117>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
mov    -0x30(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
add    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_wildcard>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %edi,%edi
test   %r12,%r12
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
lea    (%rax,%r15,1),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1213 <replace_wildcard+0xc3>
mov    %rax,%r14
lea    0xe51(%rip),%rsi
mov    %r12,%rdi
call   1040 <strcspn@plt>
mov    %rax,%rbp
xor    %eax,%eax
test   %rbp,%rbp
je     1215 <replace_wildcard+0xc5>
mov    %r15,%rcx
sub    %rbp,%rcx
jbe    1215 <replace_wildcard+0xc5>
add    %r13,%r15
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rcx,(%rsp)
mov    %rbp,%rdx
call   1060 <memcpy@plt>
mov    %rbx,%rsi
mov    %r14,%rbx
add    %rbp,%rbx
mov    %rbx,%rdi
mov    %r13,%rdx
call   1060 <memcpy@plt>
add    %r13,%rbx
lea    (%r12,%rbp,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
mov    (%rsp),%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r14,%r15,1)
mov    %r14,%rax
jmp    1215 <replace_wildcard+0xc5>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"*\00"
define i8* @replace_wildcard(i8* %0, i8* %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i8* %1, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%11 = add i64 %10, %9
%12 = add i64 %11, 1
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %30, label %15
15: ; preds = %2
%16 = tail call i64 @strcspn(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp ne i64 %16, 0
%18 = icmp ugt i64 %9, %16
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %20, label %30
20: ; preds = %15
%21 = tail call i32 @memcpy(i8* %13, i8* %0, i64 %16)
%22 = getelementptr inbounds i8, i8* %13, i64 %16
%23 = tail call i32 @memcpy(i8* %22, i8* %1, i64 %10)
%24 = getelementptr inbounds i8, i8* %22, i64 %10
%25 = getelementptr inbounds i8, i8* %0, i64 %16
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = sub i64 %9, %16
%28 = tail call i32 @memcpy(i8* %24, i8* %26, i64 %27)
%29 = getelementptr inbounds i8, i8* %13, i64 %11
store i8 0, i8* %29
br label %30
30: ; preds = %15, %2, %20
%31 = phi i8* [ %13, %20 ], [ null, %2 ], [ null, %15 ]
ret i8* %31
}