#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 */
int ENOMEM;
int errno;
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int sysctlbyname(char const *, void *, size_t *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/);
void *plcrash_sysctl_malloc(const char *name, size_t *length) {
  void *result = NULL;
  size_t result_len = 0;
  int ret;
  do {
    if ((ret = sysctlbyname(name, NULL, &result_len, NULL, 0)) == -1)
      break;
    if (result != NULL)
      free(result);
    result = malloc(result_len);
    ret = sysctlbyname(name, result, &result_len, NULL, 0);
  } while (ret == -1 && errno == ENOMEM);
  if (ret == -1) {
    int saved_errno = errno;
    if (result != NULL)
      free(result);
    errno = saved_errno;
    return NULL;
  }
  if (length != NULL)
    *length = result_len;
  return result;
}


x86 assembly (clang, -O0):
<plcrash_sysctl_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%rdx
xor    %r8d,%r8d
mov    %rcx,%rsi
call   1050 <sysctlbyname@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    1178 <plcrash_sysctl_malloc+0x48>
jmp    11e6 <plcrash_sysctl_malloc+0xb6>
cmpq   $0x0,-0x20(%rbp)
je     118c <plcrash_sysctl_malloc+0x5c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1050 <sysctlbyname@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x2c(%rbp)
mov    %al,-0x31(%rbp)
jne    11db <plcrash_sysctl_malloc+0xab>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1150 <plcrash_sysctl_malloc+0x20>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1229 <plcrash_sysctl_malloc+0xf9>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1210 <plcrash_sysctl_malloc+0xe0>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%ecx
mov    0x2dbe(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1247 <plcrash_sysctl_malloc+0x117>
cmpq   $0x0,-0x18(%rbp)
je     123f <plcrash_sysctl_malloc+0x10f>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plcrash_sysctl_malloc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r15
movq   $0x0,0x8(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%r12
mov    0x2e7b(%rip),%r13
mov    0x2e7c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <sysctlbyname@plt>
cmp    $0xffffffff,%eax
je     11dc <plcrash_sysctl_malloc+0xac>
test   %rbx,%rbx
je     1194 <plcrash_sysctl_malloc+0x64>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <sysctlbyname@plt>
mov    0x0(%r13),%r14d
cmp    $0xffffffff,%eax
jne    11c3 <plcrash_sysctl_malloc+0x93>
cmp    0x0(%rbp),%r14d
je     1170 <plcrash_sysctl_malloc+0x40>
cmp    $0xffffffff,%eax
je     11e0 <plcrash_sysctl_malloc+0xb0>
mov    0x10(%rsp),%rcx
test   %rcx,%rcx
je     11f3 <plcrash_sysctl_malloc+0xc3>
mov    0x8(%rsp),%rax
mov    %rax,(%rcx)
jmp    11f3 <plcrash_sysctl_malloc+0xc3>
mov    0x0(%r13),%r14d
test   %rbx,%rbx
je     11ed <plcrash_sysctl_malloc+0xbd>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,0x0(%r13)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@ENOMEM = global i32 0
define i8* @plcrash_sysctl_malloc(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8* null, i8** %6
store i64 0, i64* %7
br label %10
10: ; preds = %34, %2
%11 = load i8*, i8** %4
%12 = call i32 @sysctlbyname(i8* %11, i8* null, i64* %7, i32* null, i32 0)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %10
br label %36
15: ; preds = %10
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = call i32 @free(i8* %19)
br label %21
21: ; preds = %18, %15
%22 = load i64, i64* %7
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %4
%25 = load i8*, i8** %6
%26 = call i32 @sysctlbyname(i8* %24, i8* %25, i64* %7, i32* null, i32 0)
store i32 %26, i32* %8
br label %27
27: ; preds = %21
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = load i32, i32* @ENOMEM
%33 = icmp eq i32 %31, %32
br label %34
34: ; preds = %30, %27
%35 = phi i1 [ false, %27 ], [ %33, %30 ]
br i1 %35, label %10, label %36
36: ; preds = %34, %14
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %48
39: ; preds = %36
%40 = load i32, i32* @errno
store i32 %40, i32* %9
%41 = load i8*, i8** %6
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i8*, i8** %6
%45 = call i32 @free(i8* %44)
br label %46
46: ; preds = %43, %39
%47 = load i32, i32* %9
store i32 %47, i32* @errno
store i8* null, i8** %3
br label %56
48: ; preds = %36
%49 = load i64*, i64** %5
%50 = icmp ne i64* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i64, i64* %7
%53 = load i64*, i64** %5
store i64 %52, i64* %53
br label %54
54: ; preds = %51, %48
%55 = load i8*, i8** %6
store i8* %55, i8** %3
br label %56
56: ; preds = %54, %46
%57 = load i8*, i8** %3
ret i8* %57
}

LLVM IR (-O2):
@errno = global i32 0
@ENOMEM = global i32 0
define i8* @plcrash_sysctl_malloc(i8* %0, i64* writeonly %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 0, i64* %3
br label %5
5: ; preds = %13, %2
%6 = phi i8* [ null, %2 ], [ %15, %13 ]
%7 = call i32 @sysctlbyname(i8* %0, i8* null, i64* %3, i32* null, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %23, label %9
9: ; preds = %5
%10 = icmp eq i8* %6, null
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = call i32 @free(i8* %6)
br label %13
13: ; preds = %11, %9
%14 = load i64, i64* %3
%15 = call i8* @malloc(i64 %14)
%16 = call i32 @sysctlbyname(i8* %0, i8* %15, i64* %3, i32* null, i32 0)
%17 = icmp eq i32 %16, -1
%18 = load i32, i32* @errno
%19 = load i32, i32* @ENOMEM
%20 = icmp eq i32 %18, %19
%21 = select i1 %17, i1 %20, i1 false
br i1 %21, label %5, label %22
22: ; preds = %13
br i1 %17, label %25, label %32
23: ; preds = %5
%24 = load i32, i32* @errno
br label %25
25: ; preds = %23, %22
%26 = phi i32 [ %18, %22 ], [ %24, %23 ]
%27 = phi i8* [ %15, %22 ], [ %6, %23 ]
%28 = icmp eq i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = call i32 @free(i8* %27)
br label %31
31: ; preds = %29, %25
store i32 %26, i32* @errno
br label %36
32: ; preds = %22
%33 = icmp eq i64* %1, null
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = load i64, i64* %3
store i64 %35, i64* %1
br label %36
36: ; preds = %32, %34, %31
%37 = phi i8* [ null, %31 ], [ %15, %34 ], [ %15, %32 ]
ret i8* %37
}