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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int sysctlbyname(const char *oid0, void *oldp, size_t *oldlenp, void *newp,
                 size_t newlen) {
  char oidcopy[1024], *oid;
  char path[1024];
  FILE *fd;
  strncpy(oidcopy, oid0, sizeof(oidcopy));
  oid = oidcopy;
  strcpy(path, "/proc/sys");
  do {
    char *cp, *tp;
    for (cp = oid; *cp != '\0' && *cp != '.'; cp++)
      ;
    if (*cp == '.')
      *cp++ = '\0';
    tp = strchr(path, '\0');
    snprintf(tp, sizeof(path) - (tp - path), "/%s", oid);
    oid = cp;
  } while (*oid != '\0');
  if (oldp != NULL) {
    fd = fopen(path, "r");
    if (fd == NULL)
      return -1;
    if (fscanf(fd, "%u", (int *)oldp) != 1) {
      fclose(fd);
      return -1;
    }
  } else {
    fd = fopen(path, "w");
    if (fd == NULL)
      return -1;
    (void)fprintf(fd, "%u", *(int *)newp);
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<sysctlbyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    -0x430(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x840(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x438(%rbp),%rax
mov    %rax,-0x850(%rbp)
mov    -0x850(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x859(%rbp)
je     1214 <sysctlbyname+0x94>
mov    -0x850(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
setne  %al
mov    %al,-0x859(%rbp)
mov    -0x859(%rbp),%al
test   $0x1,%al
jne    1227 <sysctlbyname+0xa7>
jmp    1243 <sysctlbyname+0xc3>
jmp    122c <sysctlbyname+0xac>
mov    -0x850(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x850(%rbp)
jmp    11e3 <sysctlbyname+0x63>
mov    -0x850(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    126e <sysctlbyname+0xee>
mov    -0x850(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x850(%rbp)
movb   $0x0,(%rax)
lea    -0x840(%rbp),%rdi
xor    %esi,%esi
call   1060 <strchr@plt>
mov    %rax,-0x858(%rbp)
mov    -0x858(%rbp),%rdi
mov    -0x858(%rbp),%rcx
lea    -0x840(%rbp),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x438(%rbp),%rcx
lea    0xd57(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x850(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x438(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11d5 <sysctlbyname+0x55>
cmpq   $0x0,-0x18(%rbp)
je     1355 <sysctlbyname+0x1d5>
lea    -0x840(%rbp),%rdi
lea    0xd1c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x848(%rbp)
cmpq   $0x0,-0x848(%rbp)
jne    1318 <sysctlbyname+0x198>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <sysctlbyname+0x235>
mov    -0x848(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xce6(%rip),%rsi
call   1080 <fscanf@plt>
cmp    $0x1,%eax
je     1350 <sysctlbyname+0x1d0>
mov    -0x848(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <sysctlbyname+0x235>
jmp    13a2 <sysctlbyname+0x222>
lea    -0x840(%rbp),%rdi
lea    0xcb0(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x848(%rbp)
cmpq   $0x0,-0x848(%rbp)
jne    1389 <sysctlbyname+0x209>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b5 <sysctlbyname+0x235>
mov    -0x848(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
lea    0xc73(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x848(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysctlbyname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%rsi
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%edx
call   1030 <strncpy@plt>
lea    0xe4a(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
call   1040 <strcpy@plt>
lea    0xe42(%rip),%r13
mov    %rbx,%rbp
jmp    11d4 <sysctlbyname+0x54>
nopl   (%rax)
add    $0x1,%rbp
movzbl 0x0(%rbp),%eax
test   %al,%al
je     11e8 <sysctlbyname+0x68>
cmp    $0x2e,%al
jne    11d0 <sysctlbyname+0x50>
movb   $0x0,0x0(%rbp)
add    $0x1,%rbp
mov    %r12,%rdi
call   1060 <strlen@plt>
lea    (%rsp,%rax,1),%rdi
mov    $0x400,%esi
sub    %eax,%esi
mov    %r13,%rdx
mov    %rbx,%rcx
call   1070 <snprintf@plt>
cmpb   $0x0,0x0(%rbp)
mov    %rbp,%rbx
jne    11d4 <sysctlbyname+0x54>
test   %r15,%r15
je     1249 <sysctlbyname+0xc9>
lea    0xdf3(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     127e <sysctlbyname+0xfe>
mov    %rax,%rbx
lea    0xdde(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1080 <fscanf@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
je     1272 <sysctlbyname+0xf2>
jmp    1274 <sysctlbyname+0xf4>
lea    0xdc3(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     127e <sysctlbyname+0xfe>
mov    %rax,%rbx
mov    (%r14),%edx
lea    0xda6(%rip),%rsi
mov    %rax,%rdi
call   1090 <fprintf@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1283 <sysctlbyname+0x103>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/proc/sys\00"
@.str.1 = [4 x i8] c"/%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%u\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @sysctlbyname(i8* %0, i8* %1, i64* %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [1024 x i8]
%13 = alloca i8*
%14 = alloca [1024 x i8]
%15 = alloca i32*
%16 = alloca i8*
%17 = alloca i8*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64* %2, i64** %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%19 = load i8*, i8** %7
%20 = call i32 @strncpy(i8* %18, i8* %19, i32 1024)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
store i8* %21, i8** %13
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %64, %5
%25 = load i8*, i8** %13
store i8* %25, i8** %16
br label %26
26: ; preds = %39, %24
%27 = load i8*, i8** %16
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %26
%32 = load i8*, i8** %16
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 46
br label %36
36: ; preds = %31, %26
%37 = phi i1 [ false, %26 ], [ %35, %31 ]
br i1 %37, label %38, label %42
38: ; preds = %36
br label %39
39: ; preds = %38
%40 = load i8*, i8** %16
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %16
br label %26
42: ; preds = %36
%43 = load i8*, i8** %16
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 46
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i8*, i8** %16
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %16
store i8 0, i8* %48
br label %50
50: ; preds = %47, %42
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%52 = call i8* @strchr(i8* %51, i8 signext 0)
store i8* %52, i8** %17
%53 = load i8*, i8** %17
%54 = load i8*, i8** %17
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%56 = ptrtoint i8* %54 to i64
%57 = ptrtoint i8* %55 to i64
%58 = sub i64 %56, %57
%59 = sub i64 1024, %58
%60 = trunc i64 %59 to i32
%61 = load i8*, i8** %13
%62 = call i32 @snprintf(i8* %53, i32 %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %61)
%63 = load i8*, i8** %16
store i8* %63, i8** %13
br label %64
64: ; preds = %50
%65 = load i8*, i8** %13
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp ne i32 %67, 0
br i1 %68, label %24, label %69
69: ; preds = %64
%70 = load i8*, i8** %8
%71 = icmp ne i8* %70, null
br i1 %71, label %72, label %88
72: ; preds = %69
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%74 = call i32* @fopen(i8* %73, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %74, i32** %15
%75 = load i32*, i32** %15
%76 = icmp eq i32* %75, null
br i1 %76, label %77, label %78
77: ; preds = %72
store i32 -1, i32* %6
br label %103
78: ; preds = %72
%79 = load i32*, i32** %15
%80 = load i8*, i8** %8
%81 = bitcast i8* %80 to i32*
%82 = call i32 @fscanf(i32* %79, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %81)
%83 = icmp ne i32 %82, 1
br i1 %83, label %84, label %87
84: ; preds = %78
%85 = load i32*, i32** %15
%86 = call i32 @fclose(i32* %85)
store i32 -1, i32* %6
br label %103
87: ; preds = %78
br label %100
88: ; preds = %69
%89 = getelementptr inbounds [1024 x i8], [1024 x i8]* %14, i64 0, i64 0
%90 = call i32* @fopen(i8* %89, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %90, i32** %15
%91 = load i32*, i32** %15
%92 = icmp eq i32* %91, null
br i1 %92, label %93, label %94
93: ; preds = %88
store i32 -1, i32* %6
br label %103
94: ; preds = %88
%95 = load i32*, i32** %15
%96 = load i8*, i8** %10
%97 = bitcast i8* %96 to i32*
%98 = load i32, i32* %97
%99 = call i32 @fprintf(i32* %95, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %98)
br label %100
100: ; preds = %94, %87
%101 = load i32*, i32** %15
%102 = call i32 @fclose(i32* %101)
store i32 0, i32* %6
br label %103
103: ; preds = %100, %93, %84, %77
%104 = load i32, i32* %6
ret i32 %104
}

LLVM IR (-O2):
@.str = [10 x i8] c"/proc/sys\00"
@.str.1 = [4 x i8] c"/%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%u\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @sysctlbyname(i8* %0, i8* %1, i64* nocapture readnone %2, i8* nocapture %3, i64 %4) {
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%10 = call i32 @strncpy(i8* %8, i8* %0, i32 1024)
%11 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %21, %5
%13 = phi i8* [ %8, %5 ], [ %22, %21 ]
br label %14
14: ; preds = %17, %12
%15 = phi i8* [ %13, %12 ], [ %18, %17 ]
%16 = load i8, i8* %15
switch i8 %16, label %17 [
i8 46, label %19
i8 0, label %21
]
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %15, i64 1
br label %14
19: ; preds = %14
%20 = getelementptr inbounds i8, i8* %15, i64 1
store i8 0, i8* %15
br label %21
21: ; preds = %14, %19
%22 = phi i8* [ %20, %19 ], [ %15, %14 ]
%23 = call i64 @strlen(i8* %9)
%24 = getelementptr [1024 x i8], [1024 x i8]* %7, i64 0, i64 %23
%25 = trunc i64 %23 to i32
%26 = sub i32 1024, %25
%27 = call i32 @snprintf(i8* %24, i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%28 = load i8, i8* %22
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %12
30: ; preds = %21
%31 = icmp eq i8* %1, null
br i1 %31, label %39, label %32
32: ; preds = %30
%33 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %52, label %35
35: ; preds = %32
%36 = bitcast i8* %1 to i32*
%37 = call i32 @fscanf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %36)
%38 = icmp eq i32 %37, 1
br i1 %38, label %46, label %48
39: ; preds = %30
%40 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %52, label %42
42: ; preds = %39
%43 = bitcast i8* %3 to i32*
%44 = load i32, i32* %43
%45 = call i32 @fprintf(i32* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %35, %42
%47 = phi i32* [ %33, %35 ], [ %40, %42 ]
br label %48
48: ; preds = %35, %46
%49 = phi i32* [ %47, %46 ], [ %33, %35 ]
%50 = phi i32 [ 0, %46 ], [ -1, %35 ]
%51 = call i32 @fclose(i32* %49)
br label %52
52: ; preds = %48, %39, %32
%53 = phi i32 [ -1, %32 ], [ -1, %39 ], [ %50, %48 ]
ret i32 %53
}