#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ we_wordc;
  int /*<<< orphan*/ **we_wordv;
  int /*<<< orphan*/ member_0;
};
typedef TYPE_1__ wordexp_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *join_args(int /*<<< orphan*/ **, int /*<<< orphan*/);
int /*<<< orphan*/ memmove(char *, char *, scalar_t__);
char *realloc(char *, scalar_t__);
scalar_t__ strlen(char *);
char *strstr(char *, char *);
scalar_t__ wordexp(char *, TYPE_1__ *, int /*<<< orphan*/);
int /*<<< orphan*/ wordfree(TYPE_1__ *);
bool expand_path(char **path) {
  wordexp_t p = {0};
  while (strstr(*path, "  ")) {
    *path = realloc(*path, strlen(*path) + 2);
    char *ptr = strstr(*path, "  ") + 1;
    memmove(ptr + 1, ptr, strlen(ptr) + 1);
    *ptr = '\\';
  }
  if (wordexp(*path, &p, 0) != 0 || p.we_wordv[0] == NULL) {
    wordfree(&p);
    return false;
  }
  free(*path);
  *path = join_args(p.we_wordv, p.we_wordc);
  wordfree(&p);
  return true;
}


x86 assembly (clang, -O0):
<expand_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe46(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1252 <expand_path+0xc2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
add    $0x2,%rsi
call   1070 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xdf8(%rip),%rsi
call   10b0 <strstr@plt>
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1080 <memmove@plt>
mov    -0x30(%rbp),%rax
movb   $0x5c,(%rax)
jmp    11ac <expand_path+0x1c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   10a0 <wordexp@plt>
cmp    $0x0,%rax
jne    127c <expand_path+0xec>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1291 <expand_path+0x101>
lea    -0x28(%rbp),%rdi
call   1090 <wordfree@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c3 <expand_path+0x133>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1060 <join_args@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
lea    -0x28(%rbp),%rdi
call   1090 <wordfree@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r15
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    (%rdi),%rbx
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strstr@plt>
test   %rax,%rax
je     121b <expand_path+0x9b>
lea    0xe43(%rip),%r14
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x2(%rax),%rsi
mov    %rbx,%rdi
call   1060 <realloc@plt>
mov    %rax,(%r15)
mov    %rax,%rdi
mov    %r14,%rsi
call   10a0 <strstr@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rbp
lea    0x2(%rax),%r12
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r12,%rdi
mov    %rbp,%rsi
call   1070 <memmove@plt>
movb   $0x5c,0x1(%rbx)
mov    (%r15),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
jne    11c0 <expand_path+0x40>
mov    %rsp,%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <wordexp@plt>
test   %rax,%rax
jne    1257 <expand_path+0xd7>
mov    0x8(%rsp),%rax
cmpq   $0x0,(%rax)
je     1257 <expand_path+0xd7>
mov    (%r15),%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
mov    (%rsp),%esi
call   1050 <join_args@plt>
mov    %rax,(%r15)
mov    $0x1,%ebp
mov    %rsp,%rdi
call   1080 <wordfree@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32**, i32 }
@.str = [3 x i8] c" \00"
define i32 @expand_path(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca %struct.TYPE_4__
%5 = alloca i8*
store i8** %0, i8*** %3
%6 = bitcast %struct.TYPE_4__* %4 to i8*
br label %7
7: ; preds = %12, %1
%8 = load i8**, i8*** %3
%9 = load i8*, i8** %8
%10 = call i8* @strstr(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %33
12: ; preds = %7
%13 = load i8**, i8*** %3
%14 = load i8*, i8** %13
%15 = load i8**, i8*** %3
%16 = load i8*, i8** %15
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 2
%19 = call i8* @realloc(i8* %14, i64 %18)
%20 = load i8**, i8*** %3
store i8* %19, i8** %20
%21 = load i8**, i8*** %3
%22 = load i8*, i8** %21
%23 = call i8* @strstr(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8*, i8** %5
%28 = load i8*, i8** %5
%29 = call i64 @strlen(i8* %28)
%30 = add nsw i64 %29, 1
%31 = call i32 @memmove(i8* %26, i8* %27, i64 %30)
%32 = load i8*, i8** %5
store i8 92, i8* %32
br label %7
33: ; preds = %7
%34 = load i8**, i8*** %3
%35 = load i8*, i8** %34
%36 = call i64 @wordexp(i8* %35, %struct.TYPE_4__* %4, i32 0)
%37 = icmp ne i64 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %33
%39 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 1
%40 = load i32**, i32*** %39
%41 = getelementptr inbounds i32*, i32** %40, i64 0
%42 = load i32*, i32** %41
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %46
44: ; preds = %38, %33
%45 = call i32 @wordfree(%struct.TYPE_4__* %4)
store i32 0, i32* %2
br label %57
46: ; preds = %38
%47 = load i8**, i8*** %3
%48 = load i8*, i8** %47
%49 = call i32 @free(i8* %48)
%50 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 1
%51 = load i32**, i32*** %50
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %4, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i8* @join_args(i32** %51, i32 %53)
%55 = load i8**, i8*** %3
store i8* %54, i8** %55
%56 = call i32 @wordfree(%struct.TYPE_4__* %4)
store i32 1, i32* %2
br label %57
57: ; preds = %46, %44
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32**, i32 }
@.str = [3 x i8] c" \00"
define i32 @expand_path(i8** nocapture %0) {
%2 = alloca %struct.TYPE_4__
%3 = bitcast %struct.TYPE_4__* %2 to i8*
%4 = load i8*, i8** %0
%5 = tail call i8* @strstr(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %21, label %7
7: ; preds = %1, %7
%8 = phi i8* [ %18, %7 ], [ %4, %1 ]
%9 = tail call i64 @strlen(i8* dereferenceable(1) %8)
%10 = add nsw i64 %9, 2
%11 = tail call i8* @realloc(i8* %8, i64 %10)
store i8* %11, i8** %0
%12 = tail call i8* @strstr(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds i8, i8* %12, i64 1
%14 = getelementptr inbounds i8, i8* %12, i64 2
%15 = tail call i64 @strlen(i8* %13)
%16 = add nsw i64 %15, 1
%17 = tail call i32 @memmove(i8* %14, i8* %13, i64 %16)
store i8 92, i8* %13
%18 = load i8*, i8** %0
%19 = tail call i8* @strstr(i8* dereferenceable(1) %18, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %7
21: ; preds = %7, %1
%22 = phi i8* [ %4, %1 ], [ %18, %7 ]
%23 = call i64 @wordexp(i8* %22, %struct.TYPE_4__* %2, i32 0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 1
%27 = load i32**, i32*** %26
%28 = load i32*, i32** %27
%29 = icmp eq i32* %28, null
br i1 %29, label %37, label %30
30: ; preds = %25
%31 = load i8*, i8** %0
%32 = call i32 @free(i8* %31)
%33 = load i32**, i32*** %26
%34 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @join_args(i32** %33, i32 %35)
store i8* %36, i8** %0
br label %37
37: ; preds = %21, %25, %30
%38 = phi i32 [ 1, %30 ], [ 0, %25 ], [ 0, %21 ]
%39 = call i32 @wordfree(%struct.TYPE_4__* %2)
ret i32 %38
}