#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 */
long EINVAL;
int sysctl_scanf(char const *, char *, unsigned int *);
long sysctl_strtoul(const char *name) {
  unsigned val;
  int n;
  n = sysctl_scanf(name, "%lu", &val);
  if (n < 0)
    return n;
  if (n != 1)
    return -EINVAL;
  return val;
}


x86 assembly (clang, -O0):
<sysctl_strtoul>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xed9(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <sysctl_scanf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    114a <sysctl_strtoul+0x3a>
movslq -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1170 <sysctl_strtoul+0x60>
cmpl   $0x1,-0x18(%rbp)
je     1169 <sysctl_strtoul+0x59>
xor    %eax,%eax
mov    0x2e83(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    1170 <sysctl_strtoul+0x60>
mov    -0x14(%rbp),%eax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysctl_strtoul>:
push   %rax
lea    0xee8(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1030 <sysctl_scanf@plt>
test   %eax,%eax
js     1131 <sysctl_strtoul+0x21>
cmp    $0x1,%eax
jne    1135 <sysctl_strtoul+0x25>
mov    0x4(%rsp),%eax
pop    %rcx
ret
cltq
pop    %rcx
ret
mov    0x2ea4(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%rax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%lu\00"
@EINVAL = global i64 0
define i64 @sysctl_strtoul(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @sysctl_scanf(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %4)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
store i64 %12, i64* %2
br label %22
13: ; preds = %1
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i64, i64* @EINVAL
%18 = sub nsw i64 0, %17
store i64 %18, i64* %2
br label %22
19: ; preds = %13
%20 = load i32, i32* %4
%21 = zext i32 %20 to i64
store i64 %21, i64* %2
br label %22
22: ; preds = %19, %16, %10
%23 = load i64, i64* %2
ret i64 %23
}

LLVM IR (-O2):
@.str = [4 x i8] c"%lu\00"
@EINVAL = global i64 0
define i64 @sysctl_strtoul(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @sysctl_scanf(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = sext i32 %4 to i64
br label %16
8: ; preds = %1
%9 = icmp eq i32 %4, 1
br i1 %9, label %13, label %10
10: ; preds = %8
%11 = load i64, i64* @EINVAL
%12 = sub nsw i64 0, %11
br label %16
13: ; preds = %8
%14 = load i32, i32* %2
%15 = zext i32 %14 to i64
br label %16
16: ; preds = %13, %10, %6
%17 = phi i64 [ %7, %6 ], [ %12, %10 ], [ %15, %13 ]
ret i64 %17
}