#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 */
typedef int /*<<< orphan*/ statename;
typedef int /*<<< orphan*/ dirname;

/* Variables and functions */
scalar_t__ cfs_file_size(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strrchr(char *, char);
char *xxstrdup(char *);
char *alloc_state_root(const char *path) {
  char dirname[1024];
  snprintf(dirname, sizeof(dirname), "%s", path);
  while (1) {
    char statename[1024];
    snprintf(statename, sizeof(statename), "%s/.__alloc", dirname);
    if (cfs_file_size(statename) >= 0) {
      if (dirname[0])
        return xxstrdup(dirname);
      else
        return xxstrdup("/");
    }
    char *s = strrchr(dirname, '/');
    if (!s)
      return 0;
    *s = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<alloc_state_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x400,%esi
lea    0xe9a(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x810(%rbp),%rdi
lea    -0x410(%rbp),%rcx
mov    $0x400,%esi
lea    0xe7e(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x810(%rbp),%rdi
call   1050 <cfs_file_size@plt>
cmp    $0x0,%rax
jl     11d7 <alloc_state_root+0x97>
cmpb   $0x0,-0x410(%rbp)
je     11c2 <alloc_state_root+0x82>
lea    -0x410(%rbp),%rdi
call   1060 <xxstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1219 <alloc_state_root+0xd9>
lea    0xe46(%rip),%rdi
call   1060 <xxstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1219 <alloc_state_root+0xd9>
lea    -0x410(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x818(%rbp)
cmpq   $0x0,-0x818(%rbp)
jne    120a <alloc_state_root+0xca>
movq   $0x0,-0x8(%rbp)
jmp    1219 <alloc_state_root+0xd9>
mov    -0x818(%rbp),%rax
movb   $0x0,(%rax)
jmp    116b <alloc_state_root+0x2b>
mov    -0x8(%rbp),%rax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_state_root>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%rcx
lea    0xea8(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x400,%esi
call   1030 <snprintf@plt>
lea    0xe94(%rip),%rdx
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rcx
call   1030 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <cfs_file_size@plt>
test   %rax,%rax
jns    11e5 <alloc_state_root+0xa5>
mov    %rsp,%r12
xor    %r14d,%r14d
lea    0xe62(%rip),%r15
lea    0x400(%rsp),%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11ff <alloc_state_root+0xbf>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1030 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <cfs_file_size@plt>
test   %rax,%rax
js     11b0 <alloc_state_root+0x70>
cmpb   $0x0,(%rsp)
je     11f0 <alloc_state_root+0xb0>
mov    %rsp,%rdi
jmp    11f7 <alloc_state_root+0xb7>
lea    0xe18(%rip),%rdi
call   1060 <xxstrdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [12 x i8] c"%s/.__alloc\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @alloc_state_root(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %10
10: ; preds = %1, %32
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = call i32 @snprintf(i8* %11, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%15 = call i64 @cfs_file_size(i8* %14)
%16 = icmp sge i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %10
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = load i8, i8* %18
%20 = icmp ne i8 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%23 = call i8* @xxstrdup(i8* %22)
store i8* %23, i8** %2
br label %34
24: ; preds = %17
%25 = call i8* @xxstrdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %25, i8** %2
br label %34
26: ; preds = %10
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = call i8* @strrchr(i8* %27, i8 signext 47)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %26
store i8* null, i8** %2
br label %34
32: ; preds = %26
%33 = load i8*, i8** %6
store i8 0, i8* %33
br label %10
34: ; preds = %31, %24, %21
%35 = load i8*, i8** %2
ret i8* %35
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [12 x i8] c"%s/.__alloc\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @alloc_state_root(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 1024, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%8 = call i64 @cfs_file_size(i8* %6)
%9 = icmp sgt i64 %8, -1
br i1 %9, label %10, label %17
10: ; preds = %20, %1
%11 = load i8, i8* %4
%12 = icmp eq i8 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = call i8* @xxstrdup(i8* %4)
br label %24
15: ; preds = %10
%16 = call i8* @xxstrdup(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %24
17: ; preds = %1, %20
%18 = call i8* @strrchr(i8* %4, i8 signext 47)
%19 = icmp eq i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %17
store i8 0, i8* %18
%21 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%22 = call i64 @cfs_file_size(i8* %6)
%23 = icmp sgt i64 %22, -1
br i1 %23, label %10, label %17
24: ; preds = %17, %13, %15
%25 = phi i8* [ %16, %15 ], [ %14, %13 ], [ null, %17 ]
ret i8* %25
}