#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 int /*<<< orphan*/ str;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int sysctl_get_int(const char *file, int *value) {
  char path[PATH_MAX];
  char str[64];
  ssize_t ret;
  int fd;
  strncpy(path, "/proc/sys/", PATH_MAX);
  strncat(path, file, PATH_MAX - sizeof("/proc/sys/") - 1);
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return -1;
  ret = read(fd, str, sizeof(str));
  if (ret > 0) {
    *value = atoi(str);
    ret = 0;
  } else {
    ret = -1;
  }
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<sysctl_get_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x88(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0xe4e(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x88(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e27(%rip),%rax
movslq (%rax),%rax
sub    $0xb,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1040 <strncat@plt>
mov    -0x88(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jge    1210 <sysctl_get_int+0xb0>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x7c(%rbp)
jmp    1267 <sysctl_get_int+0x107>
mov    -0x78(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jle    124b <sysctl_get_int+0xeb>
lea    -0x70(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x74(%rbp)
jmp    1252 <sysctl_get_int+0xf2>
movl   $0xffffffff,-0x74(%rbp)
mov    -0x78(%rbp),%edi
call   1050 <close@plt>
mov    -0x74(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x7c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysctl_get_int>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e74(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe65(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strncpy@plt>
mov    (%r12),%edx
add    $0xfffffff4,%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strncat@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f1 <sysctl_get_int+0x91>
mov    %eax,%ebx
lea    -0x60(%rbp),%rsi
mov    %eax,%edi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11f9 <sysctl_get_int+0x99>
lea    -0x60(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,(%r14)
xor    %r14d,%r14d
jmp    11ff <sysctl_get_int+0x9f>
mov    $0xffffffff,%r14d
jmp    1206 <sysctl_get_int+0xa6>
mov    $0xffffffff,%r14d
mov    %ebx,%edi
call   1050 <close@plt>
mov    %r14d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/proc/sys/\00"
@O_RDONLY = global i32 0
define i32 @sysctl_get_int(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [64 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = load i32, i32* @PATH_MAX
%17 = call i32 @strncpy(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i8*, i8** %4
%19 = load i32, i32* @PATH_MAX
%20 = sext i32 %19 to i64
%21 = sub i64 %20, 11
%22 = sub i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i32 @strncat(i8* %15, i8* %18, i32 %23)
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %15, i32 %25)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %11
br label %45
30: ; preds = %2
%31 = load i32, i32* %10
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 64)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%38 = call i32 @atoi(i8* %37)
%39 = load i32*, i32** %5
store i32 %38, i32* %39
store i32 0, i32* %9
br label %41
40: ; preds = %30
store i32 -1, i32* %9
br label %41
41: ; preds = %40, %36
%42 = load i32, i32* %10
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %9
store i32 %44, i32* %3
store i32 1, i32* %11
br label %45
45: ; preds = %41, %29
%46 = load i8*, i8** %6
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/proc/sys/\00"
@O_RDONLY = global i32 0
define i32 @sysctl_get_int(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [64 x i8]
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%8 = call i32 @strncpy(i8* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%9 = load i32, i32* @PATH_MAX
%10 = add i32 %9, -12
%11 = call i32 @strncat(i8* %6, i8* %0, i32 %10)
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %6, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %2
%16 = call i32 @read(i32 %13, i8* %7, i32 64)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @atoi(i8* %7)
store i32 %19, i32* %1
br label %20
20: ; preds = %15, %18
%21 = phi i32 [ 0, %18 ], [ -1, %15 ]
%22 = call i32 @close(i32 %13)
br label %23
23: ; preds = %2, %20
%24 = phi i32 [ %21, %20 ], [ -1, %2 ]
ret i32 %24
}