#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__ EINVAL;
int /*<<< orphan*/ aeron_set_err(scalar_t__, char *, char const *);
scalar_t__ errno;
scalar_t__ strcmp(char *, char const *);
unsigned long strtoul(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int aeron_prefixlen_resolver(const char *prefixlen, unsigned long max) {
  if ('\0' == *prefixlen) {
    return (int)max;
  }
  if ('/' == *prefixlen) {
    prefixlen++;
  }
  if (strcmp("0", prefixlen) == 0) {
    return 0;
  }
  unsigned long value = strtoul(prefixlen, NULL, 0);
  if (0 == value && EINVAL == errno) {
    aeron_set_err(EINVAL, "prefixlen invalid: %s", prefixlen);
    return -1;
  } else if (value > max) {
    aeron_set_err(EINVAL, "prefixlen out of range: %s", prefixlen);
    return -1;
  }
  return (int)value;
}


x86 assembly (clang, -O0):
<aeron_prefixlen_resolver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    115d <aeron_prefixlen_resolver+0x2d>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1242 <aeron_prefixlen_resolver+0x112>
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x2f,%eax
cmp    %ecx,%eax
jne    117d <aeron_prefixlen_resolver+0x4d>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe78(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11a3 <aeron_prefixlen_resolver+0x73>
movl   $0x0,-0x4(%rbp)
jmp    1242 <aeron_prefixlen_resolver+0x112>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtoul@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1202 <aeron_prefixlen_resolver+0xd2>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jne    1202 <aeron_prefixlen_resolver+0xd2>
mov    0x2df5(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   1030 <aeron_set_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <aeron_prefixlen_resolver+0x112>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    1236 <aeron_prefixlen_resolver+0x106>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdf3(%rip),%rsi
call   1030 <aeron_set_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <aeron_prefixlen_resolver+0x112>
jmp    123b <aeron_prefixlen_resolver+0x10b>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeron_prefixlen_resolver>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%al
cmp    $0x2f,%al
je     1146 <aeron_prefixlen_resolver+0x16>
test   %al,%al
je     11bc <aeron_prefixlen_resolver+0x8c>
jmp    114a <aeron_prefixlen_resolver+0x1a>
add    $0x1,%rbx
lea    0xeaf(%rip),%rdi
mov    %rbx,%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11b4 <aeron_prefixlen_resolver+0x84>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtoul@plt>
test   %rax,%rax
jne    118e <aeron_prefixlen_resolver+0x5e>
mov    0x2e62(%rip),%rcx
mov    (%rcx),%rdi
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rdi
jne    118e <aeron_prefixlen_resolver+0x5e>
lea    0xe76(%rip),%rsi
jmp    11a4 <aeron_prefixlen_resolver+0x74>
cmp    %r14,%rax
jbe    11b9 <aeron_prefixlen_resolver+0x89>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rdi
lea    0xe74(%rip),%rsi
mov    %rbx,%rdx
call   1030 <aeron_set_err@plt>
mov    $0xffffffff,%r14d
jmp    11bc <aeron_prefixlen_resolver+0x8c>
xor    %r14d,%r14d
jmp    11bc <aeron_prefixlen_resolver+0x8c>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@EINVAL = global i64 0
@errno = global i64 0
@.str.1 = [22 x i8] c"prefixlen invalid: %s\00"
@.str.2 = [27 x i8] c"prefixlen out of range: %s\00"
define i32 @aeron_prefixlen_resolver(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp eq i32 0, %9
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i64, i64* %5
%13 = trunc i64 %12 to i32
store i32 %13, i32* %3
br label %52
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 47, %17
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %4
br label %22
22: ; preds = %19, %14
%23 = load i8*, i8** %4
%24 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 0, i32* %3
br label %52
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = call i64 @strtoul(i8* %28, i32* null, i32 0)
store i64 %29, i64* %6
%30 = load i64, i64* %6
%31 = icmp eq i64 0, %30
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = load i64, i64* @EINVAL
%34 = load i64, i64* @errno
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = load i64, i64* @EINVAL
%38 = load i8*, i8** %4
%39 = call i32 @aeron_set_err(i64 %37, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 -1, i32* %3
br label %52
40: ; preds = %32, %27
%41 = load i64, i64* %6
%42 = load i64, i64* %5
%43 = icmp ugt i64 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %40
%45 = load i64, i64* @EINVAL
%46 = load i8*, i8** %4
%47 = call i32 @aeron_set_err(i64 %45, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %46)
store i32 -1, i32* %3
br label %52
48: ; preds = %40
br label %49
49: ; preds = %48
%50 = load i64, i64* %6
%51 = trunc i64 %50 to i32
store i32 %51, i32* %3
br label %52
52: ; preds = %49, %44, %36, %26, %11
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [2 x i8] c"0\00"
@EINVAL = global i64 0
@errno = global i64 0
@.str.1 = [22 x i8] c"prefixlen invalid: %s\00"
@.str.2 = [27 x i8] c"prefixlen out of range: %s\00"
define i32 @aeron_prefixlen_resolver(i8* %0, i64 %1) {
%3 = load i8, i8* %0
switch i8 %3, label %8 [
i8 0, label %4
i8 47, label %6
]
4: ; preds = %2
%5 = trunc i64 %1 to i32
br label %28
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 1
br label %8
8: ; preds = %2, %6
%9 = phi i8* [ %7, %6 ], [ %0, %2 ]
%10 = tail call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %28, label %12
12: ; preds = %8
%13 = tail call i64 @strtoul(i8* nocapture %9, i32* null, i32 0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i64, i64* @EINVAL
%17 = load i64, i64* @errno
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @aeron_set_err(i64 %16, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %28
21: ; preds = %15, %12
%22 = icmp ugt i64 %13, %1
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = load i64, i64* @EINVAL
%25 = tail call i32 @aeron_set_err(i64 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %28
26: ; preds = %21
%27 = trunc i64 %13 to i32
br label %28
28: ; preds = %19, %23, %26, %8, %4
%29 = phi i32 [ %5, %4 ], [ 0, %8 ], [ -1, %19 ], [ -1, %23 ], [ %27, %26 ]
ret i32 %29
}