#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);
unsigned int strlen(char *);
int strToInt(char str[]) {
  int ret = 0;
  for (unsigned int i = 0; i < strlen(str); i++) {
    if (isdigit(str[i]))
      ret = ret * 10 + str[i] - '0';
    else
      break;
  }
  return ret;
}


x86 assembly (clang, -O0):
<strToInt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jae    11a4 <strToInt+0x84>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     118c <strToInt+0x6c>
imul   $0xa,-0xc(%rbp),%eax
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%edx
movsbl (%rcx,%rdx,1),%ecx
add    %ecx,%eax
sub    $0x30,%eax
mov    %eax,-0xc(%rbp)
jmp    1191 <strToInt+0x71>
jmp    11a4 <strToInt+0x84>
jmp    1196 <strToInt+0x76>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    113a <strToInt+0x1a>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strToInt>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %eax,%eax
je     1175 <strToInt+0x55>
xor    %ebx,%ebx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     1178 <strToInt+0x58>
lea    (%r15,%r15,4),%eax
movsbl (%r14,%rbx,1),%ecx
lea    (%rcx,%rax,2),%r15d
add    $0xffffffd0,%r15d
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     1140 <strToInt+0x20>
jmp    1178 <strToInt+0x58>
xor    %r15d,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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