#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ send_possible_paths(char *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
void handle_complete_path_request(const char *path) {
  size_t path_len = strlen(path);
  char *glob_path = malloc(path_len + 3);
  struct stat st;
  strcpy(glob_path, path);
  fprintf(stderr, "complete path: %s\n", path);
  if (path[path_len - 1] != '/' && stat(path, &st) == 0 &&
      S_ISDIR(st.st_mode)) {
    strcat(glob_path, "/");
  }
  if (path[path_len - 1] != '*') {
    strcat(glob_path, "*");
  }
  fprintf(stderr, "complete glob path: %s\n", glob_path);
  send_possible_paths(glob_path, 0);
  free(glob_path);
}


x86 assembly (clang, -O0):
<handle_complete_path_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x3,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe25(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1232 <handle_complete_path_request+0xa2>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    1232 <handle_complete_path_request+0xa2>
mov    -0x20(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1232 <handle_complete_path_request+0xa2>
mov    -0x18(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2a,%eax
je     125b <handle_complete_path_request+0xcb>
mov    -0x18(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   10b0 <strcat@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda8(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1070 <send_possible_paths@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_complete_path_request>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %rax,%r15
lea    0x3(%rax),%rdi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    0x2e1f(%rip),%r12
mov    (%r12),%edi
lea    0xe34(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
cmpb   $0x2f,-0x1(%r15,%rbx,1)
je     1210 <handle_complete_path_request+0x80>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
test   %rax,%rax
jne    1208 <handle_complete_path_request+0x78>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     1208 <handle_complete_path_request+0x78>
lea    0xe13(%rip),%rsi
mov    %r14,%rdi
call   10b0 <strcat@plt>
cmpb   $0x2a,-0x1(%rbx,%r15,1)
je     121f <handle_complete_path_request+0x8f>
lea    0xdfe(%rip),%rsi
mov    %r14,%rdi
call   10b0 <strcat@plt>
mov    (%r12),%edi
lea    0xded(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1070 <send_possible_paths@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"complete path: %s\0A\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c"*\00"
@.str.3 = [24 x i8] c"complete glob path: %s\0A\00"
define void @handle_complete_path_request(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca %struct.stat
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %3
%8 = load i64, i64* %3
%9 = add i64 %8, 3
%10 = call i8* @malloc(i64 %9)
store i8* %10, i8** %4
%11 = load i8*, i8** %4
%12 = load i8*, i8** %2
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %2
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %2
%18 = load i64, i64* %3
%19 = sub i64 %18, 1
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 47
br i1 %23, label %24, label %36
24: ; preds = %1
%25 = load i8*, i8** %2
%26 = call i64 @stat(i8* %25, %struct.stat* %5)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISDIR(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i8*, i8** %4
%35 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %33, %28, %24, %1
%37 = load i8*, i8** %2
%38 = load i64, i64* %3
%39 = sub i64 %38, 1
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 42
br i1 %43, label %44, label %47
44: ; preds = %36
%45 = load i8*, i8** %4
%46 = call i32 @strcat(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %44, %36
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %4
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %4
%52 = call i32 @send_possible_paths(i8* %51, i32 0)
%53 = load i8*, i8** %4
%54 = call i32 @free(i8* %53)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"complete path: %s\0A\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [2 x i8] c"*\00"
@.str.3 = [24 x i8] c"complete glob path: %s\0A\00"
define void @handle_complete_path_request(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add i64 %3, 3
%5 = tail call i8* @malloc(i64 %4)
%6 = bitcast %struct.stat* %2 to i8*
%7 = tail call i32 @strcpy(i8* %5, i8* %0)
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = add i64 %3, -1
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 47
br i1 %13, label %27, label %14
14: ; preds = %1
%15 = call i64 @stat(i8* %0, %struct.stat* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i64 @S_ISDIR(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %17, %14
%25 = load i8, i8* %11
%26 = icmp eq i8 %25, 42
br i1 %26, label %29, label %27
27: ; preds = %1, %24
%28 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%32 = call i32 @send_possible_paths(i8* %5, i32 0)
%33 = call i32 @free(i8* %5)
ret void
}