#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__ isdigit(char);
scalar_t__ strtol(char *, char **, int);
int stoi(char string[]) {
  int length = -1;
  int index;
  while (string[++length] != '\0')
    ;
  for (index = 0; index < length - 1; index++)
    if ((isdigit(string[index])) != 0)
      continue;
    else
      return -1;
  return (int)strtol(string, (char **)NULL, 10);
}


x86 assembly (clang, -O0):
<stoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1155 <stoi+0x35>
jmp    1133 <stoi+0x13>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11a7 <stoi+0x87>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     118d <stoi+0x6d>
jmp    1199 <stoi+0x79>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <stoi+0x9c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115c <stoi+0x3c>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stoi>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
cmpb   $0x0,(%r14,%rbx,1)
lea    0x1(%rbx),%rbx
jne    1130 <stoi+0x10>
lea    -0x2(%rbx),%eax
test   %eax,%eax
jle    1168 <stoi+0x48>
add    $0xfffffffe,%ebx
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
movsbl (%r14,%r15,1),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1179 <stoi+0x59>
add    $0x1,%r15
cmp    %r15,%rbx
jne    1150 <stoi+0x30>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
jmp    117e <stoi+0x5e>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @stoi(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 -1, i32* %4
br label %6
6: ; preds = %15, %1
%7 = load i8*, i8** %3
%8 = load i32, i32* %4
%9 = add nsw i32 %8, 1
store i32 %9, i32* %4
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %7, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %6
br label %6
16: ; preds = %6
store i32 0, i32* %5
br label %17
17: ; preds = %32, %16
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = sub nsw i32 %19, 1
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %35
22: ; preds = %17
%23 = load i8*, i8** %3
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call i64 @isdigit(i8 signext %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %22
br label %32
31: ; preds = %22
store i32 -1, i32* %2
br label %39
32: ; preds = %30
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %17
35: ; preds = %17
%36 = load i8*, i8** %3
%37 = call i64 @strtol(i8* %36, i8** null, i32 10)
%38 = trunc i64 %37 to i32
store i32 %38, i32* %2
br label %39
39: ; preds = %35, %31
%40 = load i32, i32* %2
ret i32 %40
}

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