#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 /*<<< orphan*/ INT_MAX;
int /*<<< orphan*/ INT_MIN;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ log_debug(char *, char const *, char const *);
scalar_t__ strtonum_OBSD(char const *, int /*<<< orphan*/, int /*<<< orphan*/,
                         char const **, int);
int strtoint(const char *nptr, int base) {
  assert(base == 10 || base == 16);
  int num;
  const char *errstr;
  num = (int)strtonum_OBSD(nptr, INT_MIN, INT_MAX, &errstr, base);
  if (errstr != NULL) {
    log_debug("can't convert to int: nptr is %s: %s\n", errstr, nptr);
    return -1;
  }
  return num;
}


x86 assembly (clang, -O0):
<strtoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x1,%al
cmpl   $0xa,-0x14(%rbp)
mov    %al,-0x21(%rbp)
je     1158 <strtoint+0x28>
cmpl   $0x10,-0x14(%rbp)
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
mov    -0x14(%rbp),%r8d
lea    -0x20(%rbp),%rcx
call   1050 <strtonum_OBSD@plt>
mov    %eax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b6 <strtoint+0x86>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe5b(%rip),%rdi
call   1030 <log_debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <strtoint+0x8c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strtoint>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
cmp    $0xa,%esi
sete   %al
cmp    $0x10,%esi
sete   %cl
or     %al,%cl
movzbl %cl,%edi
call   1040 <assert@plt>
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
mov    0x2e8a(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %ebp,%r8d
call   1050 <strtonum_OBSD@plt>
mov    (%rsp),%rsi
test   %rsi,%rsi
je     118b <strtoint+0x5b>
lea    0xe82(%rip),%rdi
mov    %rbx,%rdx
call   1030 <log_debug@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@INT_MIN = global i32 0
@INT_MAX = global i32 0
@.str = [38 x i8] c"can't convert to int: nptr is %s: %s\0A\00"
define i32 @strtoint(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 10
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, 16
br label %13
13: ; preds = %10, %2
%14 = phi i1 [ true, %2 ], [ %12, %10 ]
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %4
%18 = load i32, i32* @INT_MIN
%19 = load i32, i32* @INT_MAX
%20 = load i32, i32* %5
%21 = call i64 @strtonum_OBSD(i8* %17, i32 %18, i32 %19, i8** %7, i32 %20)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %6
%23 = load i8*, i8** %7
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %13
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = call i32 @log_debug(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
store i32 -1, i32* %3
br label %31
29: ; preds = %13
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@INT_MIN = global i32 0
@INT_MAX = global i32 0
@.str = [38 x i8] c"can't convert to int: nptr is %s: %s\0A\00"
define i32 @strtoint(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = icmp eq i32 %1, 10
%5 = icmp eq i32 %1, 16
%6 = or i1 %4, %5
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = bitcast i8** %3 to i8*
%10 = load i32, i32* @INT_MIN
%11 = load i32, i32* @INT_MAX
%12 = call i64 @strtonum_OBSD(i8* %0, i32 %10, i32 %11, i8** %3, i32 %1)
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @log_debug(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %0)
br label %19
17: ; preds = %2
%18 = trunc i64 %12 to i32
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ -1, %15 ], [ %18, %17 ]
ret i32 %20
}