#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char const *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, char *);
char *strstr(char *, char const *);
int build_path(const char *partial_path, const char *prefix, char *full_path,
               size_t full_path_len) {
  DIR *dp;
  struct dirent *ep;
  dp = opendir(partial_path);
  if (dp != NULL) {
    while ((ep = readdir(dp))) {
      char *found_string = strstr(ep->d_name, prefix);
      if (found_string != NULL && (ep->d_name - found_string) == 0) {
        snprintf(full_path, full_path_len, "%s/%s", partial_path, ep->d_name);
        (void)closedir(dp);
        return 1;
      }
    }
    (void)closedir(dp);
  } else {
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<build_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     121a <build_path+0xca>
jmp    1185 <build_path+0x35>
mov    -0x30(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     120c <build_path+0xbc>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1207 <build_path+0xb7>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
cmp    $0x0,%rax
jne    1207 <build_path+0xb7>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    (%rax),%r8
lea    0xe13(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    122d <build_path+0xdd>
jmp    1185 <build_path+0x35>
mov    -0x30(%rbp),%rdi
call   1050 <closedir@plt>
jmp    1226 <build_path+0xd6>
movl   $0x0,-0x4(%rbp)
jmp    122d <build_path+0xdd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r15
call   1030 <opendir@plt>
test   %rax,%rax
je     11c4 <build_path+0x74>
mov    %rax,%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11c8 <build_path+0x78>
mov    (%rax),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     1180 <build_path+0x30>
cmp    %rax,%rbp
jne    1180 <build_path+0x30>
lea    0xe54(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %r15,%rcx
mov    %rbp,%r8
call   1040 <snprintf@plt>
mov    $0x1,%ebx
jmp    11ca <build_path+0x7a>
xor    %ebx,%ebx
jmp    11d2 <build_path+0x82>
xor    %ebx,%ebx
mov    %r13,%rdi
call   1050 <closedir@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"%s/%s\00"
define i32 @build_path(i8* %0, i8* nocapture %1, i8* %2, i64 %3) {
%5 = tail call i32* @opendir(i8* %0)
%6 = icmp eq i32* %5, null
br i1 %6, label %22, label %7
7: ; preds = %4, %10
%8 = tail call %struct.dirent* @readdir(i32* %5)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %19, label %10
10: ; preds = %7
%11 = getelementptr inbounds %struct.dirent, %struct.dirent* %8, i64 0, i32 0
%12 = load i8*, i8** %11
%13 = tail call i8* @strstr(i8* dereferenceable(1) %12, i8* dereferenceable(1) %1)
%14 = icmp ne i8* %13, null
%15 = icmp eq i8* %12, %13
%16 = and i1 %14, %15
br i1 %16, label %17, label %7
17: ; preds = %10
%18 = tail call i32 @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %12)
br label %19
19: ; preds = %7, %17
%20 = phi i32 [ 1, %17 ], [ 0, %7 ]
%21 = tail call i32 @closedir(i32* %5)
br label %22
22: ; preds = %19, %4
%23 = phi i32 [ 0, %4 ], [ %20, %19 ]
ret i32 %23
}