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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *mprintf(char *, char const *);
int vfscanf(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
int sysctl_vscanf(const char *name, const char *fmt, va_list ap) {
  char *path = NULL;
  FILE *s = NULL;
  int rv;
  path = mprintf("/proc/sys/%s", name);
  if (!path) {
    rv = -errno;
    goto fail;
  }
  s = fopen(path, "r");
  if (!s) {
    rv = -errno;
    goto fail;
  }
  rv = vfscanf(s, fmt, ap);
fail:
  if (s)
    fclose(s);
  if (path)
    free(path);
  return rv;
}


x86 assembly (clang, -O0):
<sysctl_vscanf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe82(%rip),%rdi
call   1040 <mprintf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a5 <sysctl_vscanf+0x55>
xor    %eax,%eax
mov    0x2e45(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    11ea <sysctl_vscanf+0x9a>
mov    -0x20(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d7 <sysctl_vscanf+0x87>
xor    %eax,%eax
mov    0x2e13(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x2c(%rbp)
jmp    11ea <sysctl_vscanf+0x9a>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <vfscanf@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11fe <sysctl_vscanf+0xae>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     1212 <sysctl_vscanf+0xc2>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysctl_vscanf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rsi
lea    0xe99(%rip),%rdi
call   1040 <mprintf@plt>
test   %rax,%rax
je     11a5 <sysctl_vscanf+0x55>
mov    %rax,%r15
lea    0xe92(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b2 <sysctl_vscanf+0x62>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1070 <vfscanf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11bd <sysctl_vscanf+0x6d>
mov    0x2e34(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    11c5 <sysctl_vscanf+0x75>
mov    0x2e27(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/sys/%s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"r\00"
define i32 @sysctl_vscanf(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8* null, i8** %7
store i32* null, i32** %8
%10 = load i8*, i8** %4
%11 = call i8* @mprintf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp ne i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
store i32 %16, i32* %9
br label %30
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %8
%20 = load i32*, i32** %8
%21 = icmp ne i32* %20, null
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
store i32 %24, i32* %9
br label %30
25: ; preds = %17
%26 = load i32*, i32** %8
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = call i32 @vfscanf(i32* %26, i8* %27, i32 %28)
store i32 %29, i32* %9
br label %30
30: ; preds = %25, %22, %14
%31 = load i32*, i32** %8
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
br label %36
36: ; preds = %33, %30
%37 = load i8*, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
br label %42
42: ; preds = %39, %36
%43 = load i32, i32* %9
ret i32 %43
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/sys/%s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"r\00"
define i32 @sysctl_vscanf(i8* %0, i8* %1, i32 %2) {
%4 = tail call i8* @mprintf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %21
9: ; preds = %3
%10 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @vfscanf(i32* %10, i8* %1, i32 %2)
%14 = tail call i32 @fclose(i32* %10)
br label %18
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
br label %18
18: ; preds = %15, %12
%19 = phi i32 [ %13, %12 ], [ %17, %15 ]
%20 = tail call i32 @free(i8* %4)
br label %21
21: ; preds = %6, %18
%22 = phi i32 [ %19, %18 ], [ %8, %6 ]
ret i32 %22
}