#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_WRONLY;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ replacestr(char *, char, char);
int strlcat(char *, char *, int);
int /*<<< orphan*/ strlcpy(char *, char *, int);
size_t strlen(char *);
scalar_t__ write(int, char *, size_t);
int setsysctl(char *variable, char *value) {
  char path[PATH_MAX];
  int fd;
  ssize_t n;
  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_WRONLY);
  if (fd < 0)
    return -1;
  n = write(fd, value, strlen(value));
  if ((size_t)n != strlen(value)) {
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<setsysctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%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,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
mov    $0x2f,%edx
call   1090 <replacestr@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%edx
lea    0xe2c(%rip),%rsi
call   1070 <strlcpy@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1040 <strlcat@plt>
mov    -0x48(%rbp),%rcx
cltq
cmp    %rcx,%rax
jb     1221 <setsysctl+0xb1>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
mov    $0x2e,%edx
call   1090 <replacestr@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12e2 <setsysctl+0x172>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
mov    $0x2e,%edx
call   1090 <replacestr@plt>
mov    -0x50(%rbp),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1266 <setsysctl+0xf6>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12e2 <setsysctl+0x172>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x58(%rbp),%rax
cmp    %rcx,%rax
je     12cc <setsysctl+0x15c>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12e2 <setsysctl+0x172>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setsysctl>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e65(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0x2e,%esi
mov    $0x2f,%edx
call   1090 <replacestr@plt>
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdi
mov    %r15d,%edx
call   1070 <strlcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1040 <strlcat@plt>
movslq %eax,%r13
mov    %r12,%rdi
mov    $0x2f,%esi
mov    $0x2e,%edx
call   1090 <replacestr@plt>
mov    $0xffffffff,%r12d
cmp    %r15,%r13
jae    1239 <setsysctl+0xc9>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1239 <setsysctl+0xc9>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
xor    %r12d,%r12d
cmp    %rax,%r15
setne  %r12b
mov    %ebx,%edi
call   1060 <close@plt>
neg    %r12d
mov    %r12d,%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_WRONLY = global i32 0
define i32 @setsysctl(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i8*, i8** %4
%16 = call i32 @replacestr(i8* %15, i8 signext 46, i8 signext 47)
%17 = trunc i64 %12 to i32
%18 = call i32 @strlcpy(i8* %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %4
%20 = trunc i64 %12 to i32
%21 = call i32 @strlcat(i8* %14, i8* %19, i32 %20)
%22 = sext i32 %21 to i64
%23 = icmp uge i64 %22, %12
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = call i32 @replacestr(i8* %25, i8 signext 47, i8 signext 46)
store i32 -1, i32* %3
store i32 1, i32* %10
br label %51
27: ; preds = %2
%28 = load i8*, i8** %4
%29 = call i32 @replacestr(i8* %28, i8 signext 47, i8 signext 46)
%30 = load i32, i32* @O_WRONLY
%31 = call i32 @open(i8* %14, i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
store i32 -1, i32* %3
store i32 1, i32* %10
br label %51
35: ; preds = %27
%36 = load i32, i32* %8
%37 = load i8*, i8** %5
%38 = load i8*, i8** %5
%39 = call i64 @strlen(i8* %38)
%40 = call i64 @write(i32 %36, i8* %37, i64 %39)
store i64 %40, i64* %9
%41 = load i64, i64* %9
%42 = load i8*, i8** %5
%43 = call i64 @strlen(i8* %42)
%44 = icmp ne i64 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %3
store i32 1, i32* %10
br label %51
48: ; preds = %35
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
store i32 0, i32* %3
store i32 1, i32* %10
br label %51
51: ; preds = %48, %45, %34, %24
%52 = load i8*, i8** %6
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/proc/sys/\00"
@O_WRONLY = global i32 0
define i32 @setsysctl(i8* %0, i8* nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 @replacestr(i8* %0, i8 signext 46, i8 signext 47)
%7 = call i32 @strlcpy(i8* %5, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%8 = call i32 @strlcat(i8* %5, i8* %0, i32 %3)
%9 = sext i32 %8 to i64
%10 = icmp ult i64 %9, %4
%11 = call i32 @replacestr(i8* %0, i8 signext 47, i8 signext 46)
br i1 %10, label %12, label %23
12: ; preds = %2
%13 = load i32, i32* @O_WRONLY
%14 = call i32 @open(i8* %5, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = call i64 @strlen(i8* dereferenceable(1) %1)
%18 = call i64 @write(i32 %14, i8* %1, i64 %17)
%19 = call i64 @strlen(i8* dereferenceable(1) %1)
%20 = icmp ne i64 %18, %19
%21 = call i32 @close(i32 %14)
%22 = sext i1 %20 to i32
br label %23
23: ; preds = %16, %2, %12
%24 = phi i32 [ -1, %12 ], [ -1, %2 ], [ %22, %16 ]
ret i32 %24
}