#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_ISCHR(int /*<<< orphan*/);
int stat(char const *, struct stat *);
char *detect_talpa_root(void) {
  static const char securityfs[] = "/sys/kernel/security/talpa";
  static const char procfs[] = "/proc/sys/talpa";
  static char *root;
  int ret;
  struct stat statbuf;
  if (root)
    return root;
  ret = stat(securityfs, &statbuf);
  if (!ret && S_ISCHR(statbuf.st_mode)) {
    root = (char *)securityfs;
  } else {
    ret = stat(procfs, &statbuf);
    if (!ret && S_ISCHR(statbuf.st_mode))
      root = (char *)procfs;
  }
  return root;
}


x86 assembly (clang, -O0):
<detect_talpa_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpq   $0x0,0x2f08(%rip)
je     1146 <detect_talpa_root+0x26>
mov    0x2efb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11d5 <detect_talpa_root+0xb5>
lea    0xeb3(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <stat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1188 <detect_talpa_root+0x68>
mov    -0x10(%rbp),%edi
call   1040 <S_ISCHR@plt>
cmp    $0x0,%rax
je     1188 <detect_talpa_root+0x68>
lea    0xe84(%rip),%rax
mov    %rax,0x2eb5(%rip)
jmp    11ca <detect_talpa_root+0xaa>
lea    0xe91(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <stat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11c5 <detect_talpa_root+0xa5>
mov    -0x10(%rbp),%edi
call   1040 <S_ISCHR@plt>
cmp    $0x0,%rax
je     11c5 <detect_talpa_root+0xa5>
lea    0xe62(%rip),%rax
mov    %rax,0x2e73(%rip)
jmp    11ca <detect_talpa_root+0xaa>
mov    0x2e67(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_talpa_root>:
push   %rbx
sub    $0x10,%rsp
mov    0x2f0c(%rip),%rax
test   %rax,%rax
jne    118d <detect_talpa_root+0x6d>
lea    0xec8(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <stat@plt>
test   %eax,%eax
je     1171 <detect_talpa_root+0x51>
lea    0xed0(%rip),%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1030 <stat@plt>
test   %eax,%eax
jne    1186 <detect_talpa_root+0x66>
mov    0x8(%rsp),%edi
call   1040 <S_ISCHR@plt>
test   %rax,%rax
jne    117f <detect_talpa_root+0x5f>
jmp    1186 <detect_talpa_root+0x66>
mov    0x8(%rsp),%edi
call   1040 <S_ISCHR@plt>
test   %rax,%rax
je     1149 <detect_talpa_root+0x29>
mov    %rbx,0x2eb2(%rip)
mov    0x2eab(%rip),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@detect_talpa_root.securityfs = internal constant [27 x i8] c"/sys/kernel/security/talpa\00"
@detect_talpa_root.procfs = internal constant [16 x i8] c"/proc/sys/talpa\00"
@detect_talpa_root.root = internal global i8* null
define i8* @detect_talpa_root() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca %struct.stat
%4 = load i8*, i8** @detect_talpa_root.root
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = load i8*, i8** @detect_talpa_root.root
store i8* %7, i8** %1
br label %31
8: ; preds = %0
%9 = call i32 @stat(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @detect_talpa_root.securityfs, i64 0, i64 0), %struct.stat* %3)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp ne i32 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISCHR(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @detect_talpa_root.securityfs, i64 0, i64 0), i8** @detect_talpa_root.root
br label %29
18: ; preds = %12, %8
%19 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @detect_talpa_root.procfs, i64 0, i64 0), %struct.stat* %3)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp ne i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @S_ISCHR(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @detect_talpa_root.procfs, i64 0, i64 0), i8** @detect_talpa_root.root
br label %28
28: ; preds = %27, %22, %18
br label %29
29: ; preds = %28, %17
%30 = load i8*, i8** @detect_talpa_root.root
store i8* %30, i8** %1
br label %31
31: ; preds = %29, %6
%32 = load i8*, i8** %1
ret i8* %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@detect_talpa_root.securityfs = internal constant [27 x i8] c"/sys/kernel/security/talpa\00"
@detect_talpa_root.procfs = internal constant [16 x i8] c"/proc/sys/talpa\00"
@detect_talpa_root.root = internal unnamed_addr global i8* null
define i8* @detect_talpa_root() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i8*, i8** @detect_talpa_root.root
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %25
5: ; preds = %0
%6 = call i32 @stat(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @detect_talpa_root.securityfs, i64 0, i64 0), %struct.stat* %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISCHR(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8, %5
%14 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @detect_talpa_root.procfs, i64 0, i64 0), %struct.stat* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i64 @S_ISCHR(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16, %8
%22 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @detect_talpa_root.securityfs, i64 0, i64 0), %8 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @detect_talpa_root.procfs, i64 0, i64 0), %16 ]
store i8* %22, i8** @detect_talpa_root.root
br label %23
23: ; preds = %21, %13, %16
%24 = load i8*, i8** @detect_talpa_root.root
br label %25
25: ; preds = %0, %23
%26 = phi i8* [ %24, %23 ], [ %3, %0 ]
ret i8* %26
}