#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_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strnlen(char *, int);
int resolve_fullpath(char *fullpath, const char *filename, char *path_str,
                     size_t path_str_len) {
  char *p = path_str;
  struct stat st;
  while (p + 1 < path_str + path_str_len) {
    snprintf(fullpath, 1024, "%s/%s", p, filename);
    if (!stat(fullpath, &st) && S_ISREG(st.st_mode))
      return (0);
    p += strnlen(p, 1024 - ((intptr_t)p + (intptr_t)path_str)) + 1;
  }
  *fullpath = '\0';
  return (-1);
}


x86 assembly (clang, -O0):
<resolve_fullpath>:
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    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    -0x20(%rbp),%rcx
add    -0x28(%rbp),%rcx
cmp    %rcx,%rax
jae    11f9 <resolve_fullpath+0xb9>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xe6f(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%eax
jne    11ca <resolve_fullpath+0x8a>
mov    -0x38(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     11ca <resolve_fullpath+0x8a>
movl   $0x0,-0x4(%rbp)
jmp    1207 <resolve_fullpath+0xc7>
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%esi
call   1040 <strnlen@plt>
add    $0x1,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1160 <resolve_fullpath+0x20>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_fullpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
cmp    $0x2,%rcx
jl     11d1 <resolve_fullpath+0x91>
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,%r12
add    %rdx,%r15
mov    %rsp,%rbp
mov    %rdx,%r14
jmp    1197 <resolve_fullpath+0x57>
nopl   0x0(%rax,%rax,1)
mov    $0x400,%esi
sub    %r14d,%esi
sub    %r13d,%esi
mov    %r14,%rdi
call   1040 <strnlen@plt>
lea    (%rax,%r14,1),%rcx
add    $0x2,%rcx
add    %rax,%r14
add    $0x1,%r14
cmp    %r15,%rcx
jae    11d1 <resolve_fullpath+0x91>
mov    %rbx,%rdi
mov    $0x400,%esi
lea    0xe5a(%rip),%rdx
mov    %r14,%rcx
mov    %r12,%r8
call   1030 <snprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <stat@plt>
test   %eax,%eax
jne    1170 <resolve_fullpath+0x30>
mov    (%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     1170 <resolve_fullpath+0x30>
xor    %eax,%eax
jmp    11d9 <resolve_fullpath+0x99>
movb   $0x0,(%rbx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
define i32 @resolve_fullpath(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 i8*
%11 = alloca %struct.stat
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%12 = load i8*, i8** %8
store i8* %12, i8** %10
br label %13
13: ; preds = %34, %4
%14 = load i8*, i8** %10
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8*, i8** %8
%17 = load i64, i64* %9
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = icmp ult i8* %15, %18
br i1 %19, label %20, label %47
20: ; preds = %13
%21 = load i8*, i8** %6
%22 = load i8*, i8** %10
%23 = load i8*, i8** %7
%24 = call i32 @snprintf(i8* %21, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %23)
%25 = load i8*, i8** %6
%26 = call i32 @stat(i8* %25, %struct.stat* %11)
%27 = icmp ne i32 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %20
%29 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISREG(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 0, i32* %5
br label %49
34: ; preds = %28, %20
%35 = load i8*, i8** %10
%36 = load i8*, i8** %10
%37 = ptrtoint i8* %36 to i64
%38 = load i8*, i8** %8
%39 = ptrtoint i8* %38 to i64
%40 = add nsw i64 %37, %39
%41 = sub nsw i64 1024, %40
%42 = trunc i64 %41 to i32
%43 = call i64 @strnlen(i8* %35, i32 %42)
%44 = add nsw i64 %43, 1
%45 = load i8*, i8** %10
%46 = getelementptr inbounds i8, i8* %45, i64 %44
store i8* %46, i8** %10
br label %13
47: ; preds = %13
%48 = load i8*, i8** %6
store i8 0, i8* %48
store i32 -1, i32* %5
br label %49
49: ; preds = %47, %33
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"%s/%s\00"
define i32 @resolve_fullpath(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = getelementptr inbounds i8, i8* %2, i64 %3
%8 = icmp sgt i64 %3, 1
br i1 %8, label %9, label %31
9: ; preds = %4
%10 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%11 = ptrtoint i8* %2 to i64
br label %12
12: ; preds = %9, %21
%13 = phi i8* [ %2, %9 ], [ %28, %21 ]
%14 = tail call i32 @snprintf(i8* %0, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %1)
%15 = call i32 @stat(i8* %0, %struct.stat* %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i32, i32* %10
%19 = tail call i64 @S_ISREG(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %17, %12
%22 = ptrtoint i8* %13 to i64
%23 = add nsw i64 %22, %11
%24 = trunc i64 %23 to i32
%25 = sub i32 1024, %24
%26 = tail call i64 @strnlen(i8* %13, i32 %25)
%27 = add nsw i64 %26, 1
%28 = getelementptr inbounds i8, i8* %13, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = icmp ult i8* %29, %7
br i1 %30, label %12, label %31
31: ; preds = %21, %4
store i8 0, i8* %0
br label %32
32: ; preds = %17, %31
%33 = phi i32 [ -1, %31 ], [ 0, %17 ]
ret i32 %33
}