#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 scalar_t__ uint64_t;

/* Variables and functions */
scalar_t__ LLONG_MAX;
int /*<<< orphan*/ _SC_PHYS_PAGES;
char *getenv(char *);
scalar_t__ strtoll(char *, char **, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
uint64_t ksm_max_kernel_pages() {
  char *var = getenv("KSM_MAX_KERNEL_PAGES");
  char *endptr;
  uint64_t value;
  if (var && *var) {
    value = strtoll(var, &endptr, 0);
    if (value < LLONG_MAX && !*endptr)
      return value;
  }
  return sysconf(_SC_PHYS_PAGES) / 2;
}


x86 assembly (clang, -O0):
<ksm_max_kernel_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a9 <ksm_max_kernel_pages+0x79>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11a9 <ksm_max_kernel_pages+0x79>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtoll@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
jge    11a4 <ksm_max_kernel_pages+0x74>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11a4 <ksm_max_kernel_pages+0x74>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c5 <ksm_max_kernel_pages+0x95>
jmp    11a9 <ksm_max_kernel_pages+0x79>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ksm_max_kernel_pages>:
push   %rax
lea    0xec8(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1169 <ksm_max_kernel_pages+0x39>
cmpb   $0x0,(%rax)
je     1169 <ksm_max_kernel_pages+0x39>
mov    %rsp,%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1040 <strtoll@plt>
mov    0x2e8d(%rip),%rcx
cmp    (%rcx),%rax
jge    1169 <ksm_max_kernel_pages+0x39>
mov    (%rsp),%rcx
cmpb   $0x0,(%rcx)
je     1183 <ksm_max_kernel_pages+0x53>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1050 <sysconf@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movslq %ecx,%rax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [21 x i8] c"KSM_MAX_KERNEL_PAGES\00"
@LLONG_MAX = global i64 0
@_SC_PHYS_PAGES = global i32 0
define i64 @ksm_max_kernel_pages() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %26
8: ; preds = %0
%9 = load i8*, i8** %2
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %8
%14 = load i8*, i8** %2
%15 = call i64 @strtoll(i8* %14, i8** %3, i32 0)
store i64 %15, i64* %4
%16 = load i64, i64* %4
%17 = load i64, i64* @LLONG_MAX
%18 = icmp slt i64 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19
%24 = load i64, i64* %4
store i64 %24, i64* %1
br label %31
25: ; preds = %19, %13
br label %26
26: ; preds = %25, %8, %0
%27 = load i32, i32* @_SC_PHYS_PAGES
%28 = call i32 @sysconf(i32 %27)
%29 = sdiv i32 %28, 2
%30 = sext i32 %29 to i64
store i64 %30, i64* %1
br label %31
31: ; preds = %26, %23
%32 = load i64, i64* %1
ret i64 %32
}

LLVM IR (-O2):
@.str = [21 x i8] c"KSM_MAX_KERNEL_PAGES\00"
@LLONG_MAX = global i64 0
@_SC_PHYS_PAGES = global i32 0
define i64 @ksm_max_kernel_pages() {
%1 = alloca i8*
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%3 = bitcast i8** %1 to i8*
%4 = icmp eq i8* %2, null
br i1 %4, label %16, label %5
5: ; preds = %0
%6 = load i8, i8* %2
%7 = icmp eq i8 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %5
%9 = call i64 @strtoll(i8* %2, i8** %1, i32 0)
%10 = load i64, i64* @LLONG_MAX
%11 = icmp slt i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i8*, i8** %1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %8, %12, %5, %0
%17 = load i32, i32* @_SC_PHYS_PAGES
%18 = tail call i32 @sysconf(i32 %17)
%19 = sdiv i32 %18, 2
%20 = sext i32 %19 to i64
br label %21
21: ; preds = %12, %16
%22 = phi i64 [ %20, %16 ], [ %9, %12 ]
ret i64 %22
}