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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
char *realloc(char *, size_t);
int /*<<< orphan*/ replacestr(char *, char, char);
int strlcat(char *, char *, int);
int /*<<< orphan*/ strlcpy(char *, char *, int);
char *strrchr(char *, char);
int getsysctl(char *variable, char **value) {
  char path[PATH_MAX];
  char *p;
  char *buf, *tmp, c;
  int fd;
  ssize_t n;
  size_t sz, i;
  replacestr(variable, '.', '/');
  strlcpy(path, "/proc/sys/", sizeof(path));
  if (strlcat(path, variable, sizeof(path)) >= sizeof(path)) {
    replacestr(variable, '/', '.');
    return -1;
  }
  replacestr(variable, '/', '.');
  fd = open(path, O_RDONLY);
  if (fd < 0)
    return -1;
  i = 0;
  sz = 1;
  buf = NULL;
  while (1) {
    n = read(fd, &c, 1);
    if (n < 0) {
      close(fd);
      free(buf);
      return -1;
    }
    if (n == 0)
      break;
    if (i == sz - 1) {
      sz *= 2;
      tmp = realloc(buf, sz);
      if (!tmp) {
        close(fd);
        free(buf);
        return -1;
      }
      buf = tmp;
    }
    buf[i++] = c;
  }
  buf[i] = '\0';
  p = strrchr(buf, '\n');
  if (p)
    *p = '\0';
  *value = buf;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<getsysctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
mov    $0x2f,%edx
call   10b0 <replacestr@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    %eax,%edx
lea    0xe09(%rip),%rsi
call   1080 <strlcpy@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1040 <strlcat@plt>
mov    -0x70(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     1244 <getsysctl+0xb4>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
mov    $0x2e,%edx
call   10b0 <replacestr@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13d2 <getsysctl+0x242>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
mov    $0x2e,%edx
call   10b0 <replacestr@plt>
mov    -0x78(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    1289 <getsysctl+0xf9>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13d2 <getsysctl+0x242>
movq   $0x0,-0x60(%rbp)
movq   $0x1,-0x58(%rbp)
movq   $0x0,-0x38(%rbp)
jmp    12a6 <getsysctl+0x116>
mov    -0x48(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jge    12ea <getsysctl+0x15a>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13d2 <getsysctl+0x242>
cmpq   $0x0,-0x50(%rbp)
jne    12fa <getsysctl+0x16a>
jmp    1381 <getsysctl+0x1f1>
mov    -0x60(%rbp),%rax
mov    -0x58(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jne    1363 <getsysctl+0x1d3>
mov    -0x58(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1090 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    135b <getsysctl+0x1cb>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13d2 <getsysctl+0x242>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x41(%rbp),%dl
mov    -0x38(%rbp),%rax
mov    -0x60(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x60(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    12a1 <getsysctl+0x111>
mov    -0x38(%rbp),%rax
mov    -0x60(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     13b1 <getsysctl+0x221>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsysctl>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %rdi,%r15
mov    0x2e42(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0x2e,%esi
mov    $0x2f,%edx
call   10b0 <replacestr@plt>
lea    0xe28(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%edx
call   1080 <strlcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <strlcat@plt>
movslq %eax,%r12
mov    %r15,%rdi
mov    $0x2f,%esi
mov    $0x2e,%edx
call   10b0 <replacestr@plt>
mov    $0xffffffff,%r15d
cmp    %r14,%r12
jae    1300 <getsysctl+0x170>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1300 <getsysctl+0x170>
mov    %eax,%r12d
lea    -0x29(%rbp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
js     12b9 <getsysctl+0x129>
mov    %r13,-0x38(%rbp)
mov    $0x1,%ebx
xor    %r13d,%r13d
xor    %r15d,%r15d
jmp    1288 <getsysctl+0xf8>
nopw   0x0(%rax,%rax,1)
mov    %r15,%r14
movzbl -0x29(%rbp),%eax
mov    %al,(%r14,%r13,1)
add    $0x1,%r13
mov    %r12d,%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %r14,%r15
test   %rax,%rax
js     12bc <getsysctl+0x12c>
test   %rax,%rax
je     12d4 <getsysctl+0x144>
lea    -0x1(%rbx),%rax
cmp    %rax,%r13
jne    1260 <getsysctl+0xd0>
add    %rbx,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1263 <getsysctl+0xd3>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r15,%rdi
jmp    12c7 <getsysctl+0x137>
xor    %r14d,%r14d
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r15d
jmp    1300 <getsysctl+0x170>
movb   $0x0,(%r15,%r13,1)
mov    %r15,%rdi
mov    $0xa,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     12ee <getsysctl+0x15e>
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %r15,(%rax)
mov    %r12d,%edi
call   1060 <close@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
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 @getsysctl(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8
%12 = alloca i32
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%17 = load i32, i32* @PATH_MAX
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %6
%20 = alloca i8, i64 %18
store i64 %18, i64* %7
%21 = load i8*, i8** %4
%22 = call i32 @replacestr(i8* %21, i8 signext 46, i8 signext 47)
%23 = trunc i64 %18 to i32
%24 = call i32 @strlcpy(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i8*, i8** %4
%26 = trunc i64 %18 to i32
%27 = call i32 @strlcat(i8* %20, i8* %25, i32 %26)
%28 = sext i32 %27 to i64
%29 = icmp uge i64 %28, %18
br i1 %29, label %30, label %33
30: ; preds = %2
%31 = load i8*, i8** %4
%32 = call i32 @replacestr(i8* %31, i8 signext 47, i8 signext 46)
store i32 -1, i32* %3
store i32 1, i32* %16
br label %98
33: ; preds = %2
%34 = load i8*, i8** %4
%35 = call i32 @replacestr(i8* %34, i8 signext 47, i8 signext 46)
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %20, i32 %36)
store i32 %37, i32* %12
%38 = load i32, i32* %12
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %33
store i32 -1, i32* %3
store i32 1, i32* %16
br label %98
41: ; preds = %33
store i64 0, i64* %15
store i64 1, i64* %14
store i8* null, i8** %9
br label %42
42: ; preds = %77, %41
br label %43
43: ; preds = %42
%44 = load i32, i32* %12
%45 = call i64 @read(i32 %44, i8* %11, i32 1)
store i64 %45, i64* %13
%46 = load i64, i64* %13
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load i32, i32* %12
%50 = call i32 @close(i32 %49)
%51 = load i8*, i8** %9
%52 = call i32 @free(i8* %51)
store i32 -1, i32* %3
store i32 1, i32* %16
br label %98
53: ; preds = %43
%54 = load i64, i64* %13
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %53
br label %83
57: ; preds = %53
%58 = load i64, i64* %15
%59 = load i64, i64* %14
%60 = sub i64 %59, 1
%61 = icmp eq i64 %58, %60
br i1 %61, label %62, label %77
62: ; preds = %57
%63 = load i64, i64* %14
%64 = mul i64 %63, 2
store i64 %64, i64* %14
%65 = load i8*, i8** %9
%66 = load i64, i64* %14
%67 = call i8* @realloc(i8* %65, i64 %66)
store i8* %67, i8** %10
%68 = load i8*, i8** %10
%69 = icmp ne i8* %68, null
br i1 %69, label %75, label %70
70: ; preds = %62
%71 = load i32, i32* %12
%72 = call i32 @close(i32 %71)
%73 = load i8*, i8** %9
%74 = call i32 @free(i8* %73)
store i32 -1, i32* %3
store i32 1, i32* %16
br label %98
75: ; preds = %62
%76 = load i8*, i8** %10
store i8* %76, i8** %9
br label %77
77: ; preds = %75, %57
%78 = load i8, i8* %11
%79 = load i8*, i8** %9
%80 = load i64, i64* %15
%81 = add i64 %80, 1
store i64 %81, i64* %15
%82 = getelementptr inbounds i8, i8* %79, i64 %80
store i8 %78, i8* %82
br label %42
83: ; preds = %56
%84 = load i8*, i8** %9
%85 = load i64, i64* %15
%86 = getelementptr inbounds i8, i8* %84, i64 %85
store i8 0, i8* %86
%87 = load i8*, i8** %9
%88 = call i8* @strrchr(i8* %87, i8 signext 10)
store i8* %88, i8** %8
%89 = load i8*, i8** %8
%90 = icmp ne i8* %89, null
br i1 %90, label %91, label %93
91: ; preds = %83
%92 = load i8*, i8** %8
store i8 0, i8* %92
br label %93
93: ; preds = %91, %83
%94 = load i8*, i8** %9
%95 = load i8**, i8*** %5
store i8* %94, i8** %95
%96 = load i32, i32* %12
%97 = call i32 @close(i32 %96)
store i32 0, i32* %3
store i32 1, i32* %16
br label %98
98: ; preds = %93, %70, %48, %40, %30
%99 = load i8*, i8** %6
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/proc/sys/\00"
@O_RDONLY = global i32 0
define i32 @getsysctl(i8* %0, i8** nocapture writeonly %1) {
%3 = alloca i8
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32 @replacestr(i8* %0, i8 signext 46, i8 signext 47)
%8 = call i32 @strlcpy(i8* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
%9 = call i32 @strlcat(i8* %6, i8* %0, i32 %4)
%10 = sext i32 %9 to i64
%11 = icmp ult i64 %10, %5
%12 = call i32 @replacestr(i8* %0, i8 signext 47, i8 signext 46)
br i1 %11, label %13, label %55
13: ; preds = %2
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %6, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %55, label %17
17: ; preds = %13
%18 = call i64 @read(i32 %15, i8* %3, i32 1)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %40, %17
%21 = phi i8* [ null, %17 ], [ %41, %40 ]
%22 = call i32 @close(i32 %15)
%23 = call i32 @free(i8* %21)
br label %55
24: ; preds = %17, %40
%25 = phi i64 [ %46, %40 ], [ %18, %17 ]
%26 = phi i64 [ %44, %40 ], [ 0, %17 ]
%27 = phi i64 [ %42, %40 ], [ 1, %17 ]
%28 = phi i8* [ %41, %40 ], [ null, %17 ]
%29 = icmp eq i64 %25, 0
br i1 %29, label %48, label %30
30: ; preds = %24
%31 = add i64 %27, -1
%32 = icmp eq i64 %26, %31
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = shl i64 %27, 1
%35 = call i8* @realloc(i8* %28, i64 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @close(i32 %15)
%39 = call i32 @free(i8* %28)
br label %55
40: ; preds = %33, %30
%41 = phi i8* [ %28, %30 ], [ %35, %33 ]
%42 = phi i64 [ %27, %30 ], [ %34, %33 ]
%43 = load i8, i8* %3
%44 = add i64 %26, 1
%45 = getelementptr inbounds i8, i8* %41, i64 %26
store i8 %43, i8* %45
%46 = call i64 @read(i32 %15, i8* %3, i32 1)
%47 = icmp slt i64 %46, 0
br i1 %47, label %20, label %24
48: ; preds = %24
%49 = getelementptr inbounds i8, i8* %28, i64 %26
store i8 0, i8* %49
%50 = call i8* @strrchr(i8* dereferenceable(1) %28, i8 signext 10)
%51 = icmp eq i8* %50, null
br i1 %51, label %53, label %52
52: ; preds = %48
store i8 0, i8* %50
br label %53
53: ; preds = %52, %48
store i8* %28, i8** %1
%54 = call i32 @close(i32 %15)
br label %55
55: ; preds = %2, %13, %53, %37, %20
%56 = phi i32 [ -1, %20 ], [ 0, %53 ], [ -1, %37 ], [ -1, %13 ], [ -1, %2 ]
ret i32 %56
}