#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 *calloc(size_t, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strlcat(char *, char *, size_t);
int /*<<< orphan*/ strlcpy(char *, char *, size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncat(char *, char *, uintptr_t);
char *strstr(char *, char const *);
void strnsubst(char **str, const char *match, const char *replstr,
               size_t maxsize) {
  char *s1, *s2, *this;
  s1 = *str;
  if (s1 == NULL)
    return;
  if (maxsize == 0) {
    match = NULL;
    maxsize = strlen(s1) + 1;
  }
  s2 = calloc(maxsize, 1);
  if (s2 == NULL)
    err(1, "calloc");
  if (replstr == NULL)
    replstr = "";
  if (match == NULL || replstr == NULL || maxsize == strlen(s1)) {
    (void)strlcpy(s2, s1, maxsize);
    goto done;
  }
  for (;;) {
    this = strstr(s1, match);
    if (this == NULL)
      break;
    if ((strlen(s2) + strlen(s1) + strlen(replstr) - strlen(match) + 1) >
        maxsize) {
      (void)strlcat(s2, s1, maxsize);
      goto done;
    }
    (void)strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1);
    (void)strcat(s2, replstr);
    s1 = this + strlen(match);
  }
  (void)strcat(s2, s1);
done:
  *str = s2;
  return;
}


x86 assembly (clang, -O0):
<strnsubst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b3 <strnsubst+0x33>
jmp    1361 <strnsubst+0x1e1>
cmpq   $0x0,-0x20(%rbp)
jne    11d7 <strnsubst+0x57>
movq   $0x0,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1205 <strnsubst+0x85>
mov    $0x1,%edi
lea    0xe00(%rip),%rsi
call   1070 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jne    121b <strnsubst+0x9b>
lea    0xdef(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1252 <strnsubst+0xd2>
cmpq   $0x0,-0x18(%rbp)
je     1252 <strnsubst+0xd2>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jne    1268 <strnsubst+0xe8>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <strlcpy@plt>
jmp    1356 <strnsubst+0x1d6>
jmp    126d <strnsubst+0xed>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    128e <strnsubst+0x10e>
jmp    1349 <strnsubst+0x1c9>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x50(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
add    $0x1,%rax
cmp    -0x20(%rbp),%rax
jbe    1300 <strnsubst+0x180>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strlcat@plt>
jmp    1356 <strnsubst+0x1d6>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rax
sub    %rax,%rdx
call   1050 <strncat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    126d <strnsubst+0xed>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strnsubst>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    (%rdi),%rbx
test   %rbx,%rbx
je     130e <strnsubst+0x18e>
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
test   %rcx,%rcx
jne    11bd <strnsubst+0x3d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
add    $0x1,%r13
xor    %r12d,%r12d
mov    %r13,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11e3 <strnsubst+0x63>
lea    0xe27(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
test   %r14,%r14
lea    0xe19(%rip),%rax
cmovne %r14,%rax
mov    %rax,0x10(%rsp)
test   %r12,%r12
je     1208 <strnsubst+0x88>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    %rax,%r13
jne    121b <strnsubst+0x9b>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1080 <strlcpy@plt>
jmp    130b <strnsubst+0x18b>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
mov    %r15,(%rsp)
je     12e2 <strnsubst+0x162>
mov    %rax,%r14
mov    %r12,0x8(%rsp)
mov    0x10(%rsp),%r15
mov    %r13,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbp,%r12
mov    %rax,0x20(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%r13
mov    0x8(%rsp),%rdi
call   1040 <strlen@plt>
add    0x20(%rsp),%rbp
mov    0x18(%rsp),%rcx
add    %r13,%rbp
sub    %rax,%rbp
add    $0x1,%rbp
cmp    %rcx,%rbp
ja     12f3 <strnsubst+0x173>
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %r12,%rdi
mov    %rbx,%rsi
call   1050 <strncat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1090 <strcat@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    %r14,%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <strstr@plt>
mov    %rax,%r14
test   %rax,%rax
mov    %r12,%rbp
jne    1250 <strnsubst+0xd0>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    (%rsp),%r15
jmp    130b <strnsubst+0x18b>
mov    %rcx,%r13
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rcx,%rdx
call   1030 <strlcat@plt>
mov    (%rsp),%r15
mov    %r12,%rbp
mov    %rbp,(%r15)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"calloc\00"
@.str.1 = [1 x i8] zeroinitializer
define void @strnsubst(i8** %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%12 = load i8**, i8*** %5
%13 = load i8*, i8** %12
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %4
br label %102
17: ; preds = %4
%18 = load i64, i64* %8
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %17
store i8* null, i8** %6
%21 = load i8*, i8** %9
%22 = call i64 @strlen(i8* %21)
%23 = add i64 %22, 1
store i64 %23, i64* %8
br label %24
24: ; preds = %20, %17
%25 = load i64, i64* %8
%26 = call i8* @calloc(i64 %25, i32 1)
store i8* %26, i8** %10
%27 = load i8*, i8** %10
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = load i8*, i8** %7
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %31
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %35
35: ; preds = %34, %31
%36 = load i8*, i8** %6
%37 = icmp eq i8* %36, null
br i1 %37, label %46, label %38
38: ; preds = %35
%39 = load i8*, i8** %7
%40 = icmp eq i8* %39, null
br i1 %40, label %46, label %41
41: ; preds = %38
%42 = load i64, i64* %8
%43 = load i8*, i8** %9
%44 = call i64 @strlen(i8* %43)
%45 = icmp eq i64 %42, %44
br i1 %45, label %46, label %51
46: ; preds = %41, %38, %35
%47 = load i8*, i8** %10
%48 = load i8*, i8** %9
%49 = load i64, i64* %8
%50 = call i32 @strlcpy(i8* %47, i8* %48, i64 %49)
br label %99
51: ; preds = %41
br label %52
52: ; preds = %79, %51
%53 = load i8*, i8** %9
%54 = load i8*, i8** %6
%55 = call i8* @strstr(i8* %53, i8* %54)
store i8* %55, i8** %11
%56 = load i8*, i8** %11
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %59
58: ; preds = %52
br label %95
59: ; preds = %52
%60 = load i8*, i8** %10
%61 = call i64 @strlen(i8* %60)
%62 = load i8*, i8** %9
%63 = call i64 @strlen(i8* %62)
%64 = add i64 %61, %63
%65 = load i8*, i8** %7
%66 = call i64 @strlen(i8* %65)
%67 = add i64 %64, %66
%68 = load i8*, i8** %6
%69 = call i64 @strlen(i8* %68)
%70 = sub i64 %67, %69
%71 = add i64 %70, 1
%72 = load i64, i64* %8
%73 = icmp ugt i64 %71, %72
br i1 %73, label %74, label %79
74: ; preds = %59
%75 = load i8*, i8** %10
%76 = load i8*, i8** %9
%77 = load i64, i64* %8
%78 = call i32 @strlcat(i8* %75, i8* %76, i64 %77)
br label %99
79: ; preds = %59
%80 = load i8*, i8** %10
%81 = load i8*, i8** %9
%82 = load i8*, i8** %11
%83 = ptrtoint i8* %82 to i64
%84 = load i8*, i8** %9
%85 = ptrtoint i8* %84 to i64
%86 = sub i64 %83, %85
%87 = call i32 @strncat(i8* %80, i8* %81, i64 %86)
%88 = load i8*, i8** %10
%89 = load i8*, i8** %7
%90 = call i32 @strcat(i8* %88, i8* %89)
%91 = load i8*, i8** %11
%92 = load i8*, i8** %6
%93 = call i64 @strlen(i8* %92)
%94 = getelementptr inbounds i8, i8* %91, i64 %93
store i8* %94, i8** %9
br label %52
95: ; preds = %58
%96 = load i8*, i8** %10
%97 = load i8*, i8** %9
%98 = call i32 @strcat(i8* %96, i8* %97)
br label %99
99: ; preds = %95, %74, %46
%100 = load i8*, i8** %10
%101 = load i8**, i8*** %5
store i8* %100, i8** %101
br label %102
102: ; preds = %99, %16
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"calloc\00"
@.str.1 = [1 x i8] zeroinitializer
define void @strnsubst(i8** nocapture %0, i8* %1, i8* %2, i64 %3) {
%5 = load i8*, i8** %0
%6 = icmp eq i8* %5, null
br i1 %6, label %59, label %7
7: ; preds = %4
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %12
9: ; preds = %7
%10 = tail call i64 @strlen(i8* %5)
%11 = add i64 %10, 1
br label %12
12: ; preds = %9, %7
%13 = phi i64 [ %11, %9 ], [ %3, %7 ]
%14 = phi i8* [ null, %9 ], [ %1, %7 ]
%15 = tail call i8* @calloc(i64 %13, i32 1)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = icmp eq i8* %2, null
%21 = select i1 %20, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %2
%22 = icmp eq i8* %14, null
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = tail call i64 @strlen(i8* %5)
%25 = icmp eq i64 %13, %24
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = tail call i8* @strstr(i8* dereferenceable(1) %5, i8* %14)
%28 = icmp eq i8* %27, null
br i1 %28, label %55, label %31
29: ; preds = %23, %19
%30 = tail call i32 @strlcpy(i8* %15, i8* %5, i64 %13)
br label %58
31: ; preds = %26, %45
%32 = phi i8* [ %53, %45 ], [ %27, %26 ]
%33 = phi i8* [ %52, %45 ], [ %5, %26 ]
%34 = tail call i64 @strlen(i8* dereferenceable(1) %15)
%35 = tail call i64 @strlen(i8* dereferenceable(1) %33)
%36 = tail call i64 @strlen(i8* dereferenceable(1) %21)
%37 = tail call i64 @strlen(i8* %14)
%38 = add i64 %34, 1
%39 = add i64 %38, %35
%40 = add i64 %39, %36
%41 = sub i64 %40, %37
%42 = icmp ugt i64 %41, %13
br i1 %42, label %43, label %45
43: ; preds = %31
%44 = tail call i32 @strlcat(i8* %15, i8* %33, i64 %13)
br label %58
45: ; preds = %31
%46 = ptrtoint i8* %32 to i64
%47 = ptrtoint i8* %33 to i64
%48 = sub i64 %46, %47
%49 = tail call i32 @strncat(i8* %15, i8* %33, i64 %48)
%50 = tail call i32 @strcat(i8* %15, i8* %21)
%51 = tail call i64 @strlen(i8* %14)
%52 = getelementptr inbounds i8, i8* %32, i64 %51
%53 = tail call i8* @strstr(i8* dereferenceable(1) %52, i8* %14)
%54 = icmp eq i8* %53, null
br i1 %54, label %55, label %31
55: ; preds = %45, %26
%56 = phi i8* [ %5, %26 ], [ %52, %45 ]
%57 = tail call i32 @strcat(i8* %15, i8* %56)
br label %58
58: ; preds = %55, %43, %29
store i8* %15, i8** %0
br label %59
59: ; preds = %4, %58
ret void
}