#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;
int /*<<< orphan*/ isdigit(char const);
size_t strlen(char const *);
long strtol(char const *, int /*<<< orphan*/ *, int);
int matoi(const char *str) {
  size_t len = strlen(str);
  unsigned int a = 0;
  if (len > 7)
    return -1;
  for (a = 0; a < len; a++) {
    if (!isdigit(str[a]))
      return -2;
  }
  long lret = strtol(str, NULL, 10);
  if (lret <= INT_MAX && lret >= INT_MIN)
    return (int)lret;
  else
    return -2;
}


x86 assembly (clang, -O0):
<matoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x7,-0x18(%rbp)
jbe    1167 <matoi+0x37>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <matoi+0xd4>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    11b3 <matoi+0x83>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    11a0 <matoi+0x70>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1204 <matoi+0xd4>
jmp    11a5 <matoi+0x75>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116e <matoi+0x3e>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jg     11fd <matoi+0xcd>
mov    -0x28(%rbp),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
jl     11fd <matoi+0xcd>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1204 <matoi+0xd4>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matoi>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rax,%r15
mov    $0xffffffff,%eax
cmp    $0x7,%r15
ja     11b0 <matoi+0x80>
test   %r15,%r15
je     1176 <matoi+0x46>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11ab <matoi+0x7b>
add    $0x1,%ebx
cmp    %rbx,%r15
ja     1160 <matoi+0x30>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %rax,%rcx
mov    0x2e51(%rip),%rax
cmp    (%rax),%rcx
mov    0x2e4f(%rip),%rdx
mov    $0xfffffffe,%esi
mov    %ecx,%eax
cmovl  %esi,%eax
cmp    (%rdx),%rcx
cmovg  %esi,%eax
jmp    11b0 <matoi+0x80>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@INT_MAX = global i64 0
@INT_MIN = global i64 0
define i32 @matoi(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %4
store i32 0, i32* %5
%9 = load i64, i64* %4
%10 = icmp ugt i64 %9, 7
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %45
12: ; preds = %1
store i32 0, i32* %5
br label %13
13: ; preds = %28, %12
%14 = load i32, i32* %5
%15 = zext i32 %14 to i64
%16 = load i64, i64* %4
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = load i32, i32* %5
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = call i32 @isdigit(i8 signext %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %18
store i32 -2, i32* %2
br label %45
27: ; preds = %18
br label %28
28: ; preds = %27
%29 = load i32, i32* %5
%30 = add i32 %29, 1
store i32 %30, i32* %5
br label %13
31: ; preds = %13
%32 = load i8*, i8** %3
%33 = call i64 @strtol(i8* %32, i32* null, i32 10)
store i64 %33, i64* %6
%34 = load i64, i64* %6
%35 = load i64, i64* @INT_MAX
%36 = icmp sle i64 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %31
%38 = load i64, i64* %6
%39 = load i64, i64* @INT_MIN
%40 = icmp sge i64 %38, %39
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i64, i64* %6
%43 = trunc i64 %42 to i32
store i32 %43, i32* %2
br label %45
44: ; preds = %37, %31
store i32 -2, i32* %2
br label %45
45: ; preds = %44, %41, %26, %11
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@INT_MAX = global i64 0
@INT_MIN = global i64 0
define i32 @matoi(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp ugt i64 %2, 7
br i1 %3, label %25, label %4
4: ; preds = %1
%5 = icmp eq i64 %2, 0
br i1 %5, label %16, label %10
6: ; preds = %10
%7 = add nuw nsw i64 %11, 1
%8 = and i64 %7, 4294967295
%9 = icmp ugt i64 %2, %8
br i1 %9, label %10, label %16
10: ; preds = %4, %6
%11 = phi i64 [ %8, %6 ], [ 0, %4 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32 @isdigit(i8 signext %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %25, label %6
16: ; preds = %6, %4
%17 = tail call i64 @strtol(i8* nocapture %0, i32* null, i32 10)
%18 = load i64, i64* @INT_MAX
%19 = icmp sgt i64 %17, %18
%20 = load i64, i64* @INT_MIN
%21 = icmp slt i64 %17, %20
%22 = select i1 %19, i1 true, i1 %21
%23 = trunc i64 %17 to i32
%24 = select i1 %22, i32 -2, i32 %23
br label %25
25: ; preds = %10, %1, %16
%26 = phi i32 [ %24, %16 ], [ -1, %1 ], [ -2, %10 ]
ret i32 %26
}