#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 {
  scalar_t__ st_uid;
  int st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ EACCES;
int /*<<< orphan*/ ENOENT;
int /*<<< orphan*/ S_ISLNK(int);
int S_IWGRP;
int S_IWOTH;
int /*<<< orphan*/ errno;
scalar_t__ lstat(char *, struct stat *);
scalar_t__ stat(char *, struct stat *);
int check_config_secure(char *config_path) {
  struct stat config_stat;
  if (lstat(config_path, &config_stat) != 0) {
    errno = ENOENT;
    return 0;
  }
  if (config_stat.st_uid != 0) {
    errno = EACCES;
    return 0;
  }
  if (!S_ISLNK(config_stat.st_mode) &&
      config_stat.st_mode & (S_IWGRP | S_IWOTH)) {
    errno = EACCES;
    return 0;
  }
  if (!S_ISLNK(config_stat.st_mode)) {
    return 1;
  }
  if (stat(config_path, &config_stat) != 0) {
    errno = ENOENT;
    return 0;
  }
  if (config_stat.st_uid != 0) {
    errno = EACCES;
    return 0;
  }
  if (config_stat.st_mode & (S_IWGRP | S_IWOTH)) {
    errno = EACCES;
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<check_config_secure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <lstat@plt>
cmp    $0x0,%rax
je     1171 <check_config_secure+0x41>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6d(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12a9 <check_config_secure+0x179>
cmpq   $0x0,-0x20(%rbp)
je     119a <check_config_secure+0x6a>
mov    0x2e5d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e44(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12a9 <check_config_secure+0x179>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    11e9 <check_config_secure+0xb9>
mov    -0x18(%rbp),%eax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2dfa(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11e9 <check_config_secure+0xb9>
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
mov    0x2df5(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12a9 <check_config_secure+0x179>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    1206 <check_config_secure+0xd6>
movl   $0x1,-0x4(%rbp)
jmp    12a9 <check_config_secure+0x179>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     123b <check_config_secure+0x10b>
mov    0x2d9c(%rip),%rax
mov    (%rax),%ecx
mov    0x2da3(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12a9 <check_config_secure+0x179>
cmpq   $0x0,-0x20(%rbp)
je     1264 <check_config_secure+0x134>
mov    0x2d93(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7a(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12a9 <check_config_secure+0x179>
mov    -0x18(%rbp),%eax
mov    0x2d7a(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d41(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     12a2 <check_config_secure+0x172>
mov    0x2d55(%rip),%rax
mov    (%rax),%ecx
mov    0x2d3c(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12a9 <check_config_secure+0x179>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_config_secure>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <lstat@plt>
mov    0x2e72(%rip),%r14
test   %rax,%rax
jne    1165 <check_config_secure+0x35>
mov    0x2e86(%rip),%r15
cmpq   $0x0,0x8(%rsp)
je     1180 <check_config_secure+0x50>
mov    %r15,%r14
mov    (%r14),%eax
mov    0x2e61(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%ebp
mov    %ebp,%edi
call   1030 <S_ISLNK@plt>
test   %eax,%eax
je     11d6 <check_config_secure+0xa6>
mov    %ebp,%edi
call   1030 <S_ISLNK@plt>
mov    $0x1,%ebp
test   %eax,%eax
je     1173 <check_config_secure+0x43>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    1165 <check_config_secure+0x35>
cmpq   $0x0,0x8(%rsp)
jne    1162 <check_config_secure+0x32>
mov    0x2e28(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %r15,%r14
test   %ecx,0x10(%rsp)
jne    1165 <check_config_secure+0x35>
jmp    1173 <check_config_secure+0x43>
mov    0x2e0b(%rip),%rax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
test   %ebp,%ecx
jne    1162 <check_config_secure+0x32>
jmp    118f <check_config_secure+0x5f>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@ENOENT = global i32 0
@errno = global i32 0
@EACCES = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define i32 @check_config_secure(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @lstat(i8* %5, %struct.stat* %4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* @ENOENT
store i32 %9, i32* @errno
store i32 0, i32* %2
br label %60
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%12 = load i64, i64* %11
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i32, i32* @EACCES
store i32 %15, i32* @errno
store i32 0, i32* %2
br label %60
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @S_ISLNK(i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %16
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IWGRP
%25 = load i32, i32* @S_IWOTH
%26 = or i32 %24, %25
%27 = and i32 %23, %26
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = load i32, i32* @EACCES
store i32 %30, i32* @errno
store i32 0, i32* %2
br label %60
31: ; preds = %21, %16
%32 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%33 = load i32, i32* %32
%34 = call i32 @S_ISLNK(i32 %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %31
store i32 1, i32* %2
br label %60
37: ; preds = %31
%38 = load i8*, i8** %3
%39 = call i64 @stat(i8* %38, %struct.stat* %4)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = load i32, i32* @ENOENT
store i32 %42, i32* @errno
store i32 0, i32* %2
br label %60
43: ; preds = %37
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%45 = load i64, i64* %44
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = load i32, i32* @EACCES
store i32 %48, i32* @errno
store i32 0, i32* %2
br label %60
49: ; preds = %43
%50 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load i32, i32* @S_IWGRP
%53 = load i32, i32* @S_IWOTH
%54 = or i32 %52, %53
%55 = and i32 %51, %54
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = load i32, i32* @EACCES
store i32 %58, i32* @errno
store i32 0, i32* %2
br label %60
59: ; preds = %49
store i32 1, i32* %2
br label %60
60: ; preds = %59, %57, %47, %41, %36, %29, %14, %8
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@ENOENT = global i32 0
@errno = global i32 0
@EACCES = global i32 0
@S_IWGRP = global i32 0
@S_IWOTH = global i32 0
define i32 @check_config_secure(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %37
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i64, i64* %7
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %37
10: ; preds = %6
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = tail call i32 @S_ISLNK(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = load i32, i32* @S_IWGRP
%17 = load i32, i32* @S_IWOTH
%18 = or i32 %17, %16
%19 = and i32 %18, %12
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %37
21: ; preds = %15, %10
%22 = tail call i32 @S_ISLNK(i32 %12)
%23 = icmp eq i32 %22, 0
br i1 %23, label %40, label %24
24: ; preds = %21
%25 = call i64 @stat(i8* %0, %struct.stat* %2)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i64, i64* %7
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i32, i32* %11
%32 = load i32, i32* @S_IWGRP
%33 = load i32, i32* @S_IWOTH
%34 = or i32 %33, %32
%35 = and i32 %34, %31
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %30, %27, %24, %15, %6, %1
%38 = phi i32* [ @ENOENT, %1 ], [ @EACCES, %6 ], [ @EACCES, %15 ], [ @ENOENT, %24 ], [ @EACCES, %27 ], [ @EACCES, %30 ]
%39 = load i32, i32* %38
store i32 %39, i32* @errno
br label %40
40: ; preds = %37, %30, %21
%41 = phi i32 [ 1, %21 ], [ 1, %30 ], [ 0, %37 ]
ret i32 %41
}