#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);
int stoi(char *s) {
  int num = 0;
  while (*s != 0 && num >= 0) {
    num = num * 10;
    if (isdigit(*s))
      num = num + (*s - '0');
    else
      num = -1;
    s++;
  }
  return num;
}


x86 assembly (clang, -O0):
<stoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xd(%rbp)
je     1142 <stoi+0x32>
cmpl   $0x0,-0xc(%rbp)
setge  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1152 <stoi+0x42>
jmp    119e <stoi+0x8e>
imul   $0xa,-0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     1186 <stoi+0x76>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%rcx
movsbl (%rcx),%ecx
sub    $0x30,%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    118d <stoi+0x7d>
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1123 <stoi+0x13>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stoi>:
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
test   %al,%al
je     115f <stoi+0x4f>
mov    %rdi,%rbx
add    $0x1,%rbx
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl %al,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1164 <stoi+0x54>
lea    (%r14,%r14,4),%eax
movsbl -0x1(%rbx),%ecx
lea    (%rcx,%rax,2),%r14d
add    $0xffffffd0,%r14d
movzbl (%rbx),%eax
test   %al,%al
je     116a <stoi+0x5a>
add    $0x1,%rbx
test   %r14d,%r14d
jns    1130 <stoi+0x20>
jmp    116a <stoi+0x5a>
xor    %r14d,%r14d
jmp    116a <stoi+0x5a>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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