#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 INT_MAX;
long INT_MIN;
scalar_t__ errno;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
long strtol(char *, char **, int);
int mandoc_strntoi(const char *p, size_t sz, int base) {
  char buf[32];
  char *ep;
  long v;
  if (sz > 31)
    return (-1);
  memcpy(buf, p, sz);
  buf[(int)sz] = '\0';
  errno = 0;
  v = strtol(buf, &ep, base);
  if (buf[0] == '\0' || *ep != '\0')
    return (-1);
  if (v > INT_MAX)
    v = INT_MAX;
  if (v < INT_MIN)
    v = INT_MIN;
  return ((int)v);
}


x86 assembly (clang, -O0):
<mandoc_strntoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x1f,-0x18(%rbp)
jbe    114a <mandoc_strntoi+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <mandoc_strntoi+0xdc>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
cltq
movb   $0x0,-0x40(%rbp,%rax,1)
mov    0x2e63(%rip),%rax
movq   $0x0,(%rax)
lea    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    -0x48(%rbp),%rsi
call   1030 <strtol@plt>
mov    %rax,-0x50(%rbp)
movsbl -0x40(%rbp),%eax
cmp    $0x0,%eax
je     11a5 <mandoc_strntoi+0x85>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b1 <mandoc_strntoi+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fc <mandoc_strntoi+0xdc>
mov    -0x50(%rbp),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
jle    11d3 <mandoc_strntoi+0xb3>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jge    11f5 <mandoc_strntoi+0xd5>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mandoc_strntoi>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbp
mov    $0xffffffff,%ebx
cmp    $0x1f,%rsi
ja     11a3 <mandoc_strntoi+0x83>
mov    %edx,%r14d
mov    %rdi,%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rbp,%rdx
call   1040 <memcpy@plt>
movb   $0x0,0x10(%rsp,%rbp,1)
mov    0x2e76(%rip),%rax
movq   $0x0,(%rax)
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
mov    %r14d,%edx
call   1030 <strtol@plt>
cmpb   $0x0,0x10(%rsp)
je     11a3 <mandoc_strntoi+0x83>
mov    0x8(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    11a3 <mandoc_strntoi+0x83>
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
cmovg  %rcx,%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%rbx
cmp    %rbx,%rax
cmovge %eax,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@INT_MAX = global i64 0
@INT_MIN = global i64 0
define i32 @mandoc_strntoi(i8* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca [32 x i8]
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%11 = load i64, i64* %6
%12 = icmp ugt i64 %11, 31
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %51
14: ; preds = %3
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = call i32 @memcpy(i8* %15, i8* %16, i64 %17)
%19 = load i64, i64* %6
%20 = trunc i64 %19 to i32
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 %21
store i8 0, i8* %22
store i64 0, i64* @errno
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%24 = load i32, i32* %7
%25 = call i64 @strtol(i8* %23, i8** %9, i32 %24)
store i64 %25, i64* %10
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %14
%31 = load i8*, i8** %9
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30, %14
store i32 -1, i32* %4
br label %51
36: ; preds = %30
%37 = load i64, i64* %10
%38 = load i64, i64* @INT_MAX
%39 = icmp sgt i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i64, i64* @INT_MAX
store i64 %41, i64* %10
br label %42
42: ; preds = %40, %36
%43 = load i64, i64* %10
%44 = load i64, i64* @INT_MIN
%45 = icmp slt i64 %43, %44
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = load i64, i64* @INT_MIN
store i64 %47, i64* %10
br label %48
48: ; preds = %46, %42
%49 = load i64, i64* %10
%50 = trunc i64 %49 to i32
store i32 %50, i32* %4
br label %51
51: ; preds = %48, %35, %13
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@errno = global i64 0
@INT_MAX = global i64 0
@INT_MIN = global i64 0
define i32 @mandoc_strntoi(i8* %0, i64 %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
%8 = icmp ugt i64 %1, 31
br i1 %8, label %27, label %9
9: ; preds = %3
%10 = call i32 @memcpy(i8* %6, i8* %0, i64 %1)
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 %1
store i8 0, i8* %11
store i64 0, i64* @errno
%12 = call i64 @strtol(i8* %6, i8** %5, i32 %2)
%13 = load i8, i8* %6
%14 = icmp eq i8 %13, 0
br i1 %14, label %27, label %15
15: ; preds = %9
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i64, i64* @INT_MAX
%21 = icmp sgt i64 %12, %20
%22 = select i1 %21, i64 %20, i64 %12
%23 = load i64, i64* @INT_MIN
%24 = icmp slt i64 %22, %23
%25 = select i1 %24, i64 %23, i64 %22
%26 = trunc i64 %25 to i32
br label %27
27: ; preds = %9, %15, %3, %19
%28 = phi i32 [ %26, %19 ], [ -1, %3 ], [ -1, %15 ], [ -1, %9 ]
ret i32 %28
}