#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*/ makespace(char **, int);
int /*<<< orphan*/ slurm_xfree(void **, char *, int, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strstr(char *, char const *);
char *xstrdup(char *);
void _xstrsubstitute(char **str, const char *pattern, const char *replacement) {
  int pat_len, rep_len;
  char *ptr, *end_copy;
  int pat_offset;
  if (*str == NULL || pattern == NULL || pattern[0] == '\0')
    return;
  if ((ptr = strstr(*str, pattern)) == NULL)
    return;
  pat_offset = ptr - (*str);
  pat_len = strlen(pattern);
  if (replacement == NULL)
    rep_len = 0;
  else
    rep_len = strlen(replacement);
  end_copy = xstrdup(ptr + pat_len);
  if (rep_len != 0) {
    makespace(str, rep_len - pat_len);
    strcpy((*str) + pat_offset, replacement);
  }
  strcpy((*str) + pat_offset + rep_len, end_copy);
  slurm_xfree((void **)&(end_copy),
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/Q-Leap-Networks/debian-slurm/refs/"
              "heads/master/src/common/xstring.c",
              457, "");
}


x86 assembly (clang, -O0):
<_xstrsubstitute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     119d <_xstrsubstitute+0x3d>
cmpq   $0x0,-0x10(%rbp)
je     119d <_xstrsubstitute+0x3d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11a2 <_xstrsubstitute+0x42>
jmp    1283 <_xstrsubstitute+0x123>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11c5 <_xstrsubstitute+0x65>
jmp    1283 <_xstrsubstitute+0x123>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    (%rcx),%rcx
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f9 <_xstrsubstitute+0x99>
movl   $0x0,-0x20(%rbp)
jmp    1205 <_xstrsubstitute+0xa5>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
call   1060 <xstrdup@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1249 <_xstrsubstitute+0xe9>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
sub    -0x1c(%rbp),%esi
call   1040 <makespace@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rdi
lea    0xd8e(%rip),%rsi
mov    $0x1c9,%edx
lea    0xe1f(%rip),%rcx
call   1070 <slurm_xfree@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_xstrsubstitute>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     123c <_xstrsubstitute+0xdc>
mov    %rdi,%r12
mov    (%rdi),%rbp
test   %rbp,%rbp
je     123c <_xstrsubstitute+0xdc>
mov    %rsi,%rbx
cmpb   $0x0,(%rsi)
je     123c <_xstrsubstitute+0xdc>
mov    %rdx,%r14
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     123c <_xstrsubstitute+0xdc>
mov    %rax,%r13
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %r14,%r14
je     11c7 <_xstrsubstitute+0x67>
mov    %r14,%rdi
mov    %eax,%ebx
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %ebx,%eax
jmp    11ca <_xstrsubstitute+0x6a>
xor    %r15d,%r15d
mov    %r13,%rbx
sub    %rbp,%rbx
movslq %eax,%rdi
add    %r13,%rdi
mov    %eax,%r13d
call   1060 <xstrdup@plt>
mov    %rax,%rbp
mov    %rax,(%rsp)
test   %r15d,%r15d
je     120c <_xstrsubstitute+0xac>
mov    %r15d,%esi
sub    %r13d,%esi
mov    %r12,%rdi
call   1040 <makespace@plt>
movslq %ebx,%rbx
mov    (%r12),%rdi
add    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    120f <_xstrsubstitute+0xaf>
movslq %ebx,%rbx
add    (%r12),%rbx
movslq %r15d,%rdi
add    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0xdd8(%rip),%rsi
lea    0xe6e(%rip),%rcx
mov    %rsp,%rdi
mov    $0x1c9,%edx
call   1070 <slurm_xfree@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [158 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Q-Leap-Networks/debian-slurm/refs/heads/master/src/common/xstring.c\00"
@.str.1 = [1 x i8] zeroinitializer
define void @_xstrsubstitute(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = load i8**, i8*** %4
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %24, label %15
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = icmp eq i8* %16, null
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18, %15, %3
br label %81
25: ; preds = %18
%26 = load i8**, i8*** %4
%27 = load i8*, i8** %26
%28 = load i8*, i8** %5
%29 = call i8* @strstr(i8* %27, i8* %28)
store i8* %29, i8** %9
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %32
31: ; preds = %25
br label %81
32: ; preds = %25
%33 = load i8*, i8** %9
%34 = load i8**, i8*** %4
%35 = load i8*, i8** %34
%36 = ptrtoint i8* %33 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = trunc i64 %38 to i32
store i32 %39, i32* %11
%40 = load i8*, i8** %5
%41 = call i32 @strlen(i8* %40)
store i32 %41, i32* %7
%42 = load i8*, i8** %6
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %45
44: ; preds = %32
store i32 0, i32* %8
br label %48
45: ; preds = %32
%46 = load i8*, i8** %6
%47 = call i32 @strlen(i8* %46)
store i32 %47, i32* %8
br label %48
48: ; preds = %45, %44
%49 = load i8*, i8** %9
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = call i8* @xstrdup(i8* %52)
store i8* %53, i8** %10
%54 = load i32, i32* %8
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %69
56: ; preds = %48
%57 = load i8**, i8*** %4
%58 = load i32, i32* %8
%59 = load i32, i32* %7
%60 = sub nsw i32 %58, %59
%61 = call i32 @makespace(i8** %57, i32 %60)
%62 = load i8**, i8*** %4
%63 = load i8*, i8** %62
%64 = load i32, i32* %11
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i8*, i8** %6
%68 = call i32 @strcpy(i8* %66, i8* %67)
br label %69
69: ; preds = %56, %48
%70 = load i8**, i8*** %4
%71 = load i8*, i8** %70
%72 = load i32, i32* %11
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
%75 = load i32, i32* %8
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i8*, i8** %10
%79 = call i32 @strcpy(i8* %77, i8* %78)
%80 = call i32 @slurm_xfree(i8** %10, i8* getelementptr inbounds ([158 x i8], [158 x i8]* @.str, i64 0, i64 0), i32 457, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %81
81: ; preds = %69, %31, %24
ret void
}

LLVM IR (-O2):
@.str = [158 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Q-Leap-Networks/debian-slurm/refs/heads/master/src/common/xstring.c\00"
@.str.1 = [1 x i8] zeroinitializer
define void @_xstrsubstitute(i8** %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = load i8*, i8** %0
%7 = icmp eq i8* %6, null
%8 = icmp eq i8* %1, null
%9 = or i1 %8, %7
br i1 %9, label %49, label %10
10: ; preds = %3
%11 = load i8, i8* %1
%12 = icmp eq i8 %11, 0
br i1 %12, label %49, label %13
13: ; preds = %10
%14 = tail call i8* @strstr(i8* dereferenceable(1) %6, i8* %1)
%15 = icmp eq i8* %14, null
br i1 %15, label %49, label %16
16: ; preds = %13
%17 = ptrtoint i8* %14 to i64
%18 = ptrtoint i8* %6 to i64
%19 = sub i64 %17, %18
%20 = tail call i32 @strlen(i8* %1)
%21 = icmp eq i8* %2, null
br i1 %21, label %24, label %22
22: ; preds = %16
%23 = tail call i32 @strlen(i8* %2)
br label %24
24: ; preds = %16, %22
%25 = phi i32 [ %23, %22 ], [ 0, %16 ]
%26 = sext i32 %20 to i64
%27 = getelementptr inbounds i8, i8* %14, i64 %26
%28 = tail call i8* @xstrdup(i8* %27)
store i8* %28, i8** %4
%29 = icmp eq i32 %25, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = shl i64 %19, 32
%32 = ashr exact i64 %31, 32
br label %41
33: ; preds = %24
%34 = sub nsw i32 %25, %20
%35 = tail call i32 @makespace(i8** %0, i32 %34)
%36 = load i8*, i8** %0
%37 = shl i64 %19, 32
%38 = ashr exact i64 %37, 32
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = tail call i32 @strcpy(i8* %39, i8* %2)
br label %41
41: ; preds = %30, %33
%42 = phi i64 [ %32, %30 ], [ %38, %33 ]
%43 = load i8*, i8** %0
%44 = getelementptr inbounds i8, i8* %43, i64 %42
%45 = sext i32 %25 to i64
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = tail call i32 @strcpy(i8* %46, i8* %28)
%48 = call i32 @slurm_xfree(i8** %4, i8* getelementptr inbounds ([158 x i8], [158 x i8]* @.str, i64 0, i64 0), i32 457, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %13, %3, %10, %41
ret void
}