#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;
};
typedef int /*<<< orphan*/ dir_path;

/* Variables and functions */
int ENOBUFS;
int ENOENT;
int PATH_MAX;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int errno;
int get_dirname(char *, int, char *);
scalar_t__ lstat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int find_existing_directory_ancestor(char *ancestor, size_t ancestor_size,
                                     const char *path) {
  int error;
  char dir_path[PATH_MAX];
  struct stat info;
  if (!ancestor || ancestor_size < 1)
    return ENOBUFS;
  *ancestor = 0;
  strncpy(dir_path, path, sizeof(dir_path));
  if (dir_path[sizeof(dir_path) - 1] != 0)
    return ENOBUFS;
  while (strcmp(dir_path, "/") != 0) {
    if (lstat(dir_path, &info) < 0) {
      error = errno;
      if (error != ENOENT)
        return error;
    } else {
      if (S_ISDIR(info.st_mode))
        break;
    }
    error = get_dirname(dir_path, sizeof(dir_path), dir_path);
    if (error != 0) {
      return error;
    }
  }
  strncpy(ancestor, dir_path, ancestor_size);
  if (ancestor[ancestor_size - 1] != 0)
    return ENOBUFS;
  return 0;
}


x86 assembly (clang, -O0):
<find_existing_directory_ancestor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11aa <find_existing_directory_ancestor+0x5a>
cmpq   $0x1,-0x18(%rbp)
jae    11c2 <find_existing_directory_ancestor+0x72>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1318 <find_existing_directory_ancestor+0x1c8>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rdx
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rsi
call   1030 <strncpy@plt>
mov    -0x58(%rbp),%rcx
mov    -0x50(%rbp),%rax
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     120b <find_existing_directory_ancestor+0xbb>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1318 <find_existing_directory_ancestor+0x1c8>
jmp    1210 <find_existing_directory_ancestor+0xc0>
mov    -0x50(%rbp),%rdi
lea    0xde5(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12c8 <find_existing_directory_ancestor+0x178>
mov    -0x50(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
jge    1276 <find_existing_directory_ancestor+0x126>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2d69(%rip),%rcx
cmp    (%rcx),%eax
je     1271 <find_existing_directory_ancestor+0x121>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1318 <find_existing_directory_ancestor+0x1c8>
jmp    1292 <find_existing_directory_ancestor+0x142>
mov    -0x40(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     128d <find_existing_directory_ancestor+0x13d>
jmp    12c8 <find_existing_directory_ancestor+0x178>
jmp    1292 <find_existing_directory_ancestor+0x142>
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    %rdx,%rdi
call   1070 <get_dirname@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12c3 <find_existing_directory_ancestor+0x173>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1318 <find_existing_directory_ancestor+0x1c8>
jmp    1210 <find_existing_directory_ancestor+0xc0>
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     130a <find_existing_directory_ancestor+0x1ba>
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1318 <find_existing_directory_ancestor+0x1c8>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_existing_directory_ancestor>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e83(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %rdi,%rdi
je     11a3 <find_existing_directory_ancestor+0x53>
mov    %rsi,%r14
test   %rsi,%rsi
je     11a3 <find_existing_directory_ancestor+0x53>
mov    %rdi,%r15
movb   $0x0,(%rdi)
mov    %rbx,%rdi
mov    %rdx,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
cmpb   $0x0,-0x1(%r12,%rbx,1)
je     11bb <find_existing_directory_ancestor+0x6b>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3e(%rip),%r13
jmp    11f6 <find_existing_directory_ancestor+0xa6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%eax
jne    11ac <find_existing_directory_ancestor+0x5c>
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %rbx,%rdx
call   1070 <get_dirname@plt>
test   %eax,%eax
jne    11ac <find_existing_directory_ancestor+0x5c>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1224 <find_existing_directory_ancestor+0xd4>
mov    %rbx,%rdi
lea    -0x30(%rbp),%rsi
call   1050 <lstat@plt>
test   %rax,%rax
js     11d0 <find_existing_directory_ancestor+0x80>
mov    -0x30(%rbp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     11e4 <find_existing_directory_ancestor+0x94>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
xor    %eax,%eax
cmpb   $0x0,-0x1(%r14,%r15,1)
jne    11a3 <find_existing_directory_ancestor+0x53>
jmp    11ac <find_existing_directory_ancestor+0x5c>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@ENOBUFS = global i32 0
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@ENOENT = global i32 0
define i32 @find_existing_directory_ancestor(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca %struct.stat
%12 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i64, i64* %6
%21 = icmp ult i64 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %19, %3
%23 = load i32, i32* @ENOBUFS
store i32 %23, i32* %4
store i32 1, i32* %12
br label %79
24: ; preds = %19
%25 = load i8*, i8** %5
store i8 0, i8* %25
%26 = load i8*, i8** %7
%27 = call i32 @strncpy(i8* %16, i8* %26, i64 %14)
%28 = sub i64 %14, 1
%29 = getelementptr inbounds i8, i8* %16, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %24
%34 = load i32, i32* @ENOBUFS
store i32 %34, i32* %4
store i32 1, i32* %12
br label %79
35: ; preds = %24
br label %36
36: ; preds = %64, %35
%37 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %65
39: ; preds = %36
%40 = call i64 @lstat(i8* %16, %struct.stat* %11)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i32, i32* @errno
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = load i32, i32* @ENOENT
%46 = icmp ne i32 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = load i32, i32* %8
store i32 %48, i32* %4
store i32 1, i32* %12
br label %79
49: ; preds = %42
br label %57
50: ; preds = %39
%51 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i64 @S_ISDIR(i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %65
56: ; preds = %50
br label %57
57: ; preds = %56, %49
%58 = trunc i64 %14 to i32
%59 = call i32 @get_dirname(i8* %16, i32 %58, i8* %16)
store i32 %59, i32* %8
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = load i32, i32* %8
store i32 %63, i32* %4
store i32 1, i32* %12
br label %79
64: ; preds = %57
br label %36
65: ; preds = %55, %36
%66 = load i8*, i8** %5
%67 = load i64, i64* %6
%68 = call i32 @strncpy(i8* %66, i8* %16, i64 %67)
%69 = load i8*, i8** %5
%70 = load i64, i64* %6
%71 = sub i64 %70, 1
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %65
%77 = load i32, i32* @ENOBUFS
store i32 %77, i32* %4
store i32 1, i32* %12
br label %79
78: ; preds = %65
store i32 0, i32* %4
store i32 1, i32* %12
br label %79
79: ; preds = %78, %76, %62, %47, %33, %22
%80 = load i8*, i8** %9
%81 = load i32, i32* %4
ret i32 %81
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@ENOBUFS = global i32 0
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@ENOENT = global i32 0
define i32 @find_existing_directory_ancestor(i8* %0, i64 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast %struct.stat* %4 to i8*
%9 = icmp eq i8* %0, null
%10 = icmp eq i64 %1, 0
%11 = or i1 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i32, i32* @ENOBUFS
br label %49
14: ; preds = %3
store i8 0, i8* %0
%15 = call i32 @strncpy(i8* %7, i8* %2, i64 %6)
%16 = add nsw i64 %6, -1
%17 = getelementptr inbounds i8, i8* %7, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %24
22: ; preds = %14
%23 = load i32, i32* @ENOBUFS
br label %49
24: ; preds = %20, %38
%25 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %41, label %27
27: ; preds = %24
%28 = call i64 @lstat(i8* %7, %struct.stat* %4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = load i32, i32* @ENOENT
%33 = icmp eq i32 %31, %32
br i1 %33, label %38, label %49
34: ; preds = %27
%35 = load i32, i32* %21
%36 = call i64 @S_ISDIR(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34, %30
%39 = call i32 @get_dirname(i8* %7, i32 %5, i8* %7)
%40 = icmp eq i32 %39, 0
br i1 %40, label %24, label %49
41: ; preds = %34, %24
%42 = call i32 @strncpy(i8* %0, i8* %7, i64 %1)
%43 = add i64 %1, -1
%44 = getelementptr inbounds i8, i8* %0, i64 %43
%45 = load i8, i8* %44
%46 = icmp eq i8 %45, 0
%47 = load i32, i32* @ENOBUFS
%48 = select i1 %46, i32 0, i32 %47
br label %49
49: ; preds = %38, %30, %41, %22, %12
%50 = phi i32 [ %13, %12 ], [ %23, %22 ], [ %48, %41 ], [ %39, %38 ], [ %31, %30 ]
ret i32 %50
}