#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 */

/* Variables and functions */
int /*<<< orphan*/ PATH_MAX;
char *dirname(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ is_only_root_writable(char *);
scalar_t__ strcmp(char *, char *);
char *strndup(char const *, int /*<<< orphan*/);
int check_configuration_permissions(const char *file_name) {
  char *dir = strndup(file_name, PATH_MAX);
  char *buffer = dir;
  do {
    if (!is_only_root_writable(dir)) {
      free(buffer);
      buffer = NULL;
      return -1;
    }
    dir = dirname(dir);
  } while (strcmp(dir, "/") != 0);
  free(buffer);
  buffer = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<check_configuration_permissions>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
call   1060 <strndup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <is_only_root_writable@plt>
cmp    $0x0,%eax
jne    11a9 <check_configuration_permissions+0x59>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <check_configuration_permissions+0x98>
mov    -0x18(%rbp),%rdi
call   1050 <dirname@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    117a <check_configuration_permissions+0x2a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_configuration_permissions>:
push   %r15
push   %r14
push   %rbx
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
call   1060 <strndup@plt>
mov    %rax,%r14
lea    0xe93(%rip),%r15
mov    %rax,%rbx
mov    %rbx,%rdi
call   1040 <is_only_root_writable@plt>
test   %eax,%eax
je     119b <check_configuration_permissions+0x4b>
mov    %rbx,%rdi
call   1050 <dirname@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1170 <check_configuration_permissions+0x20>
xor    %ebx,%ebx
jmp    11a0 <check_configuration_permissions+0x50>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @check_configuration_permissions(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @PATH_MAX
%8 = call i8* @strndup(i8* %6, i32 %7)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
store i8* %9, i8** %5
br label %10
10: ; preds = %20, %1
%11 = load i8*, i8** %4
%12 = call i32 @is_only_root_writable(i8* %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = call i32 @free(i8* %15)
store i8* null, i8** %5
store i32 -1, i32* %2
br label %27
17: ; preds = %10
%18 = load i8*, i8** %4
%19 = call i8* @dirname(i8* %18)
store i8* %19, i8** %4
br label %20
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp ne i64 %22, 0
br i1 %23, label %10, label %24
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
store i8* null, i8** %5
store i32 0, i32* %2
br label %27
27: ; preds = %24, %14
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [2 x i8] c"/\00"
define i32 @check_configuration_permissions(i8* nocapture %0) {
%2 = load i32, i32* @PATH_MAX
%3 = tail call i8* @strndup(i8* %0, i32 %2)
br label %4
4: ; preds = %8, %1
%5 = phi i8* [ %3, %1 ], [ %9, %8 ]
%6 = tail call i32 @is_only_root_writable(i8* %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = tail call i8* @dirname(i8* %5)
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %4
12: ; preds = %8, %4
%13 = phi i32 [ -1, %4 ], [ 0, %8 ]
%14 = tail call i32 @free(i8* %3)
ret i32 %13
}