#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 */

/* Variables and functions */
scalar_t__ ENOMEM;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int sysctl(int *, size_t, char *, size_t *, int /*<<< orphan*/ *,
           int /*<<< orphan*/);
int sysctlnametomib(char const *, int *, size_t *);
char *sysctl_dynread(const char *n) {
  char *rv = NULL;
  int mib[16];
  size_t mibsz = 16;
  size_t mrg = 10;
  size_t sz;
  int i;
  if (sysctlnametomib(n, mib, &mibsz) == -1) {
    return NULL;
  }
  for (i = 0; i < 10; i++) {
    if (sysctl(mib, mibsz, NULL, &sz, NULL, 0) == -1)
      break;
    sz += sz * mrg / 100;
    if ((rv = (char *)malloc(sz)) == NULL)
      return NULL;
    if (sysctl(mib, mibsz, rv, &sz, NULL, 0) == -1) {
      free(rv);
      rv = NULL;
      if (errno == ENOMEM) {
        mrg += mrg * 50 / 100;
      } else
        break;
    } else {
      break;
    }
  }
  return rv;
}


x86 assembly (clang, -O0):
<sysctl_dynread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x10,-0x68(%rbp)
movq   $0xa,-0x70(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x68(%rbp),%rdx
call   1040 <sysctlnametomib@plt>
cmp    $0xffffffff,%eax
jne    118e <sysctl_dynread+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    129f <sysctl_dynread+0x15f>
movl   $0x0,-0x7c(%rbp)
cmpl   $0xa,-0x7c(%rbp)
jge    1297 <sysctl_dynread+0x157>
lea    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x78(%rbp),%rcx
xor    %r9d,%r9d
mov    %r8,%rdx
call   1060 <sysctl@plt>
cmp    $0xffffffff,%eax
jne    11c9 <sysctl_dynread+0x89>
jmp    1297 <sysctl_dynread+0x157>
mov    -0x78(%rbp),%rax
imul   -0x70(%rbp),%rax
mov    $0x64,%ecx
xor    %edx,%edx
div    %rcx
add    -0x78(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1208 <sysctl_dynread+0xc8>
movq   $0x0,-0x8(%rbp)
jmp    129f <sysctl_dynread+0x15f>
lea    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x78(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
call   1060 <sysctl@plt>
cmp    $0xffffffff,%eax
jne    127f <sysctl_dynread+0x13f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    0x2d92(%rip),%rax
mov    (%rax),%rax
mov    0x2d90(%rip),%rcx
cmp    (%rcx),%rax
jne    1275 <sysctl_dynread+0x135>
imul   $0x32,-0x70(%rbp),%rax
mov    $0x64,%ecx
xor    %edx,%edx
div    %rcx
add    -0x70(%rbp),%rax
mov    %rax,-0x70(%rbp)
jmp    127a <sysctl_dynread+0x13a>
jmp    1297 <sysctl_dynread+0x157>
jmp    1284 <sysctl_dynread+0x144>
jmp    1297 <sysctl_dynread+0x157>
jmp    1289 <sysctl_dynread+0x149>
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    1195 <sysctl_dynread+0x55>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysctl_dynread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
movq   $0x10,0x10(%rsp)
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1040 <sysctlnametomib@plt>
cmp    $0xffffffff,%eax
je     1246 <sysctl_dynread+0x106>
mov    $0xa,%r13d
mov    $0xa,%ebp
xor    %eax,%eax
mov    %rax,0x8(%rsp)
lea    0x20(%rsp),%r15
lea    0x18(%rsp),%r12
movabs $0x28f5c28f5c28f5c3,%r14
c2 f5 28
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
mov    %r12,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <sysctl@plt>
cmp    $0xffffffff,%eax
je     1254 <sysctl_dynread+0x114>
mov    0x18(%rsp),%rdi
mov    %rdi,%rax
imul   %r13,%rax
shr    $0x2,%rax
mul    %r14
shr    $0x2,%rdx
add    %rdx,%rdi
mov    %rdi,0x18(%rsp)
call   1050 <malloc@plt>
test   %rax,%rax
je     1254 <sysctl_dynread+0x114>
mov    %rax,%rbx
mov    0x10(%rsp),%rsi
mov    %r15,%rdi
mov    %rax,%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <sysctl@plt>
cmp    $0xffffffff,%eax
jne    124f <sysctl_dynread+0x10f>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
jne    1254 <sysctl_dynread+0x114>
imul   $0x32,%r13,%rax
shr    $0x2,%rax
mul    %r14
shr    $0x2,%rdx
add    %rdx,%r13
add    $0xffffffff,%ebp
jne    11a0 <sysctl_dynread+0x60>
jmp    1254 <sysctl_dynread+0x114>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    1254 <sysctl_dynread+0x114>
mov    %rbx,0x8(%rsp)
mov    0x8(%rsp),%rax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOMEM = global i64 0
define i8* @sysctl_dynread(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [16 x i32]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %4
store i64 16, i64* %6
store i64 10, i64* %7
%10 = load i8*, i8** %3
%11 = getelementptr inbounds [16 x i32], [16 x i32]* %5, i64 0, i64 0
%12 = call i32 @sysctlnametomib(i8* %10, i32* %11, i64* %6)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %64
15: ; preds = %1
store i32 0, i32* %9
br label %16
16: ; preds = %59, %15
%17 = load i32, i32* %9
%18 = icmp slt i32 %17, 10
br i1 %18, label %19, label %62
19: ; preds = %16
%20 = getelementptr inbounds [16 x i32], [16 x i32]* %5, i64 0, i64 0
%21 = load i64, i64* %6
%22 = call i32 @sysctl(i32* %20, i64 %21, i8* null, i64* %8, i32* null, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %19
br label %62
25: ; preds = %19
%26 = load i64, i64* %8
%27 = load i64, i64* %7
%28 = mul i64 %26, %27
%29 = udiv i64 %28, 100
%30 = load i64, i64* %8
%31 = add i64 %30, %29
store i64 %31, i64* %8
%32 = load i64, i64* %8
%33 = call i64 @malloc(i64 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %4
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %37
36: ; preds = %25
store i8* null, i8** %2
br label %64
37: ; preds = %25
%38 = getelementptr inbounds [16 x i32], [16 x i32]* %5, i64 0, i64 0
%39 = load i64, i64* %6
%40 = load i8*, i8** %4
%41 = call i32 @sysctl(i32* %38, i64 %39, i8* %40, i64* %8, i32* null, i32 0)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %57
43: ; preds = %37
%44 = load i8*, i8** %4
%45 = call i32 @free(i8* %44)
store i8* null, i8** %4
%46 = load i64, i64* @errno
%47 = load i64, i64* @ENOMEM
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %55
49: ; preds = %43
%50 = load i64, i64* %7
%51 = mul i64 %50, 50
%52 = udiv i64 %51, 100
%53 = load i64, i64* %7
%54 = add i64 %53, %52
store i64 %54, i64* %7
br label %56
55: ; preds = %43
br label %62
56: ; preds = %49
br label %58
57: ; preds = %37
br label %62
58: ; preds = %56
br label %59
59: ; preds = %58
%60 = load i32, i32* %9
%61 = add nsw i32 %60, 1
store i32 %61, i32* %9
br label %16
62: ; preds = %57, %55, %24, %16
%63 = load i8*, i8** %4
store i8* %63, i8** %2
br label %64
64: ; preds = %62, %36, %14
%65 = load i8*, i8** %2
ret i8* %65
}

LLVM IR (-O2):
@errno = global i64 0
@ENOMEM = global i64 0
define i8* @sysctl_dynread(i8* %0) {
%2 = alloca [16 x i32]
%3 = alloca i64
%4 = alloca i64
%5 = bitcast [16 x i32]* %2 to i8*
%6 = bitcast i64* %3 to i8*
store i64 16, i64* %3
%7 = bitcast i64* %4 to i8*
%8 = getelementptr inbounds [16 x i32], [16 x i32]* %2, i64 0, i64 0
%9 = call i32 @sysctlnametomib(i8* %0, i32* %8, i64* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %42, label %11
11: ; preds = %1, %34
%12 = phi i32 [ %38, %34 ], [ 0, %1 ]
%13 = phi i64 [ %37, %34 ], [ 10, %1 ]
%14 = load i64, i64* %3
%15 = call i32 @sysctl(i32* %8, i64 %14, i8* null, i64* %4, i32* null, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %42, label %17
17: ; preds = %11
%18 = load i64, i64* %4
%19 = mul i64 %18, %13
%20 = udiv i64 %19, 100
%21 = add i64 %20, %18
store i64 %21, i64* %4
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %42, label %25
25: ; preds = %17
%26 = load i64, i64* %3
%27 = call i32 @sysctl(i32* %8, i64 %26, i8* %23, i64* %4, i32* null, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = call i32 @free(i8* %23)
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENOMEM
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %42
34: ; preds = %29
%35 = mul i64 %13, 50
%36 = udiv i64 %35, 100
%37 = add i64 %36, %13
%38 = add nuw nsw i32 %12, 1
%39 = icmp eq i32 %38, 10
br i1 %39, label %42, label %11
40: ; preds = %25
%41 = inttoptr i64 %22 to i8*
br label %42
42: ; preds = %34, %11, %29, %17, %40, %1
%43 = phi i8* [ null, %1 ], [ %41, %40 ], [ null, %17 ], [ null, %29 ], [ null, %11 ], [ null, %34 ]
ret i8* %43
}