#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *memmem(char *, int, char *, int);
int /*<<< orphan*/ perror(char *);
int read_file(char *, char *, int);
int smap_smep_enabled() {
  char buffer[4096];
  int length = read_file("/proc/cpuinfo", &buffer[0], 4096);
  if (length == -1) {
    perror("[-] open/read(/proc/cpuinfo)");
    exit(EXIT_FAILURE);
  }
  int rv = 0;
  char *found = memmem(&buffer[0], length, "smep", 4);
  if (found != NULL)
    rv += 1;
  found = memmem(&buffer[0], length, "smap", 4);
  if (found != NULL)
    rv += 2;
  return rv;
}


x86 assembly (clang, -O0):
<smap_smep_enabled>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
lea    -0x1000(%rbp),%rsi
lea    0xea7(%rip),%rdi
mov    $0x1000,%edx
call   1040 <read_file@plt>
mov    %eax,-0x1004(%rbp)
cmpl   $0xffffffff,-0x1004(%rbp)
jne    1190 <smap_smep_enabled+0x50>
lea    0xe91(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movl   $0x0,-0x1008(%rbp)
lea    -0x1000(%rbp),%rdi
mov    -0x1004(%rbp),%esi
lea    0xe7d(%rip),%rdx
mov    $0x4,%ecx
call   1030 <memmem@plt>
mov    %rax,-0x1010(%rbp)
cmpq   $0x0,-0x1010(%rbp)
je     11dc <smap_smep_enabled+0x9c>
mov    -0x1008(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1008(%rbp)
lea    -0x1000(%rbp),%rdi
mov    -0x1004(%rbp),%esi
lea    0xe40(%rip),%rdx
mov    $0x4,%ecx
call   1030 <memmem@plt>
mov    %rax,-0x1010(%rbp)
cmpq   $0x0,-0x1010(%rbp)
je     121e <smap_smep_enabled+0xde>
mov    -0x1008(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1008(%rbp)
mov    -0x1008(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smap_smep_enabled>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
lea    0xeae(%rip),%rdi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1040 <read_file@plt>
cmp    $0xffffffff,%eax
je     11b4 <smap_smep_enabled+0x74>
mov    %eax,%ebp
lea    0xebe(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x4,%ecx
call   1030 <memmem@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
lea    0xea2(%rip),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x4,%ecx
call   1030 <memmem@plt>
lea    0x2(%rbx),%ecx
test   %rax,%rax
cmove  %ebx,%ecx
mov    %ecx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe53(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [29 x i8] c"[-] open/read(/proc/cpuinfo)\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"smep\00"
@.str.3 = [5 x i8] c"smap\00"
define i32 @smap_smep_enabled() {
%1 = alloca [4096 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%6 = call i32 @read_file(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %5, i32 4096)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
store i32 0, i32* %3
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%15 = load i32, i32* %2
%16 = call i8* @memmem(i8* %14, i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %22
22: ; preds = %19, %13
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%24 = load i32, i32* %2
%25 = call i8* @memmem(i8* %23, i32 %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 2
store i32 %30, i32* %3
br label %31
31: ; preds = %28, %22
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@.str.1 = [29 x i8] c"[-] open/read(/proc/cpuinfo)\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"smep\00"
@.str.3 = [5 x i8] c"smap\00"
define i32 @smap_smep_enabled() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = call i32 @read_file(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %2, i32 4096)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = call i8* @memmem(i8* %2, i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 4)
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = call i8* @memmem(i8* %2, i32 %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%14 = icmp eq i8* %13, null
%15 = or i32 %12, 2
%16 = select i1 %14, i32 %12, i32 %15
ret i32 %16
}