#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*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
int strlen(char const *);
scalar_t__ strncmp(char const *, char const *, int);
char *xmalloc(int);
char *cfgfile_subst_path(const char *path, const char *subst,
                         const char *file) {
  if (strlen(path) > 0 && strncmp(file, path, strlen(path)) == 0) {
    int l;
    char *p = xmalloc(strlen(file) + strlen(subst) + 2);
    strcpy(p, subst);
    l = strlen(p);
    while (l > 0 && p[l - 1] == '/')
      p[--l] = '\0';
    l = strlen(path);
    while (file[l] == '/')
      l++;
    strcat(p, "/");
    strcat(p, file + l);
    return p;
  }
  return strdup(file);
}


x86 assembly (clang, -O0):
<cfgfile_subst_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%eax
jle    12a9 <cfgfile_subst_path+0x149>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    12a9 <cfgfile_subst_path+0x149>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x44(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   1060 <xmalloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x45(%rbp)
jle    1220 <cfgfile_subst_path+0xc0>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    1230 <cfgfile_subst_path+0xd0>
jmp    1249 <cfgfile_subst_path+0xe9>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x24(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11f7 <cfgfile_subst_path+0x97>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1278 <cfgfile_subst_path+0x118>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1255 <cfgfile_subst_path+0xf5>
mov    -0x30(%rbp),%rdi
lea    0xd7d(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
call   1070 <strcat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b6 <cfgfile_subst_path+0x156>
mov    -0x20(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cfgfile_subst_path>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
call   1050 <strlen@plt>
test   %eax,%eax
jle    1195 <cfgfile_subst_path+0x35>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11a6 <cfgfile_subst_path+0x46>
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1080 <strdup@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1060 <xmalloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    120b <cfgfile_subst_path+0xab>
mov    %eax,%eax
add    $0x1,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    -0x2(%rax),%ecx
cmpb   $0x2f,0x0(%r13,%rcx,1)
jne    120b <cfgfile_subst_path+0xab>
movb   $0x0,0x0(%r13,%rcx,1)
add    $0xffffffffffffffff,%rax
cmp    $0x1,%rax
ja     11f0 <cfgfile_subst_path+0x90>
mov    %r15,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rax,%r14,1),%rbx
add    $0xffffffffffffffff,%rbx
nopl   (%rax)
cmpb   $0x2f,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     1220 <cfgfile_subst_path+0xc0>
lea    0xdcf(%rip),%rsi
mov    %r13,%rdi
call   1070 <strcat@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <strcat@plt>
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @cfgfile_subst_path(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = call i32 @strlen(i8* %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %77
13: ; preds = %3
%14 = load i8*, i8** %7
%15 = load i8*, i8** %5
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
%18 = call i64 @strncmp(i8* %14, i8* %15, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %77
20: ; preds = %13
%21 = load i8*, i8** %7
%22 = call i32 @strlen(i8* %21)
%23 = load i8*, i8** %6
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %22, %24
%26 = add nsw i32 %25, 2
%27 = call i8* @xmalloc(i32 %26)
store i8* %27, i8** %9
%28 = load i8*, i8** %9
%29 = load i8*, i8** %6
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %9
%32 = call i32 @strlen(i8* %31)
store i32 %32, i32* %8
br label %33
33: ; preds = %47, %20
%34 = load i32, i32* %8
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %33
%37 = load i8*, i8** %9
%38 = load i32, i32* %8
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 47
br label %45
45: ; preds = %36, %33
%46 = phi i1 [ false, %33 ], [ %44, %36 ]
br i1 %46, label %47, label %53
47: ; preds = %45
%48 = load i8*, i8** %9
%49 = load i32, i32* %8
%50 = add nsw i32 %49, -1
store i32 %50, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
store i8 0, i8* %52
br label %33
53: ; preds = %45
%54 = load i8*, i8** %5
%55 = call i32 @strlen(i8* %54)
store i32 %55, i32* %8
br label %56
56: ; preds = %64, %53
%57 = load i8*, i8** %7
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 47
br i1 %63, label %64, label %67
64: ; preds = %56
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %56
67: ; preds = %56
%68 = load i8*, i8** %9
%69 = call i32 @strcat(i8* %68, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%70 = load i8*, i8** %9
%71 = load i8*, i8** %7
%72 = load i32, i32* %8
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
%75 = call i32 @strcat(i8* %70, i8* %74)
%76 = load i8*, i8** %9
store i8* %76, i8** %4
br label %80
77: ; preds = %13, %3
%78 = load i8*, i8** %7
%79 = call i8* @strdup(i8* %78)
store i8* %79, i8** %4
br label %80
80: ; preds = %77, %67
%81 = load i8*, i8** %4
ret i8* %81
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @cfgfile_subst_path(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %43
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = tail call i64 @strncmp(i8* %2, i8* %0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %43
10: ; preds = %6
%11 = tail call i32 @strlen(i8* %2)
%12 = tail call i32 @strlen(i8* %1)
%13 = add i32 %11, 2
%14 = add i32 %13, %12
%15 = tail call i8* @xmalloc(i32 %14)
%16 = tail call i32 @strcpy(i8* %15, i8* %1)
%17 = tail call i32 @strlen(i8* %15)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %10
%20 = zext i32 %17 to i64
br label %21
21: ; preds = %19, %28
%22 = phi i64 [ %20, %19 ], [ %23, %28 ]
%23 = add nsw i64 %22, -1
%24 = and i64 %23, 4294967295
%25 = getelementptr inbounds i8, i8* %15, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 47
br i1 %27, label %28, label %30
28: ; preds = %21
store i8 0, i8* %25
%29 = icmp ugt i64 %22, 1
br i1 %29, label %21, label %30
30: ; preds = %21, %28, %10
%31 = tail call i32 @strlen(i8* %0)
%32 = sext i32 %31 to i64
br label %33
33: ; preds = %33, %30
%34 = phi i64 [ %38, %33 ], [ %32, %30 ]
%35 = getelementptr inbounds i8, i8* %2, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 47
%38 = add i64 %34, 1
br i1 %37, label %33, label %39
39: ; preds = %33
%40 = getelementptr inbounds i8, i8* %2, i64 %34
%41 = tail call i32 @strcat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%42 = tail call i32 @strcat(i8* %15, i8* %40)
br label %45
43: ; preds = %6, %3
%44 = tail call i8* @strdup(i8* %2)
br label %45
45: ; preds = %43, %39
%46 = phi i8* [ %15, %39 ], [ %44, %43 ]
ret i8* %46
}