#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 */
int charToInt(char const);
int fastPower10(int);
int stringToInteger(const char *str) {
  int len, value, p;
  len = p = value = 0;
  while (str[len] != '\0') {
    len++;
  }
  for (int i = len - 1; i >= 0; i--) {
    value += charToInt(str[i]) * fastPower10(p);
    p++;
  }
  return value;
}


x86 assembly (clang, -O0):
<stringToInteger>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1164 <stringToInteger+0x44>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1141 <stringToInteger+0x21>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     11b8 <stringToInteger+0x98>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <charToInt@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <fastPower10@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
imul   %ecx,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    116d <stringToInteger+0x4d>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stringToInteger>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebp,%ebp
mov    $0xffffffff,%eax
mov    %rdi,%rcx
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
add    $0x1,%eax
cmpb   $0x0,(%rcx)
lea    0x1(%rcx),%rcx
jne    1140 <stringToInteger+0x20>
test   %eax,%eax
je     119b <stringToInteger+0x7b>
mov    %eax,%r13d
add    $0xffffffffffffffff,%r13
test   %eax,%eax
mov    $0x1,%ecx
cmovle %eax,%ecx
sub    %ecx,%ebp
xor    %ebx,%ebx
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    %r13d,%eax
movsbl (%r14,%rax,1),%edi
call   1030 <charToInt@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1040 <fastPower10@plt>
imul   %r15d,%eax
add    %eax,%r12d
add    $0x1,%ebx
add    $0xffffffffffffffff,%r13
cmp    %ebx,%ebp
jne    1170 <stringToInteger+0x50>
jmp    119e <stringToInteger+0x7e>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stringToInteger(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %3
br label %7
7: ; preds = %15, %1
%8 = load i8*, i8** %2
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, 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 %18
15: ; preds = %7
%16 = load i32, i32* %3
%17 = add nsw i32 %16, 1
store i32 %17, i32* %3
br label %7
18: ; preds = %7
%19 = load i32, i32* %3
%20 = sub nsw i32 %19, 1
store i32 %20, i32* %6
br label %21
21: ; preds = %38, %18
%22 = load i32, i32* %6
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %21
%25 = load i8*, i8** %2
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = call i32 @charToInt(i8 signext %29)
%31 = load i32, i32* %5
%32 = call i32 @fastPower10(i32 %31)
%33 = mul nsw i32 %30, %32
%34 = load i32, i32* %4
%35 = add nsw i32 %34, %33
store i32 %35, i32* %4
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %38
38: ; preds = %24
%39 = load i32, i32* %6
%40 = add nsw i32 %39, -1
store i32 %40, i32* %6
br label %21
41: ; preds = %21
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @stringToInteger(i8* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i64 [ %7, %2 ], [ 0, %1 ]
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
%7 = add nuw i64 %3, 1
br i1 %6, label %8, label %2
8: ; preds = %2
%9 = trunc i64 %3 to i32
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %8
%12 = and i64 %3, 4294967295
%13 = add i32 %9, 1
%14 = call i32 @llvm.smin.i32(i32 %9, i32 1)
%15 = sub i32 %13, %14
br label %18
16: ; preds = %18, %8
%17 = phi i32 [ 0, %8 ], [ %29, %18 ]
ret i32 %17
18: ; preds = %11, %18
%19 = phi i64 [ %12, %11 ], [ %22, %18 ]
%20 = phi i32 [ 0, %11 ], [ %30, %18 ]
%21 = phi i32 [ 0, %11 ], [ %29, %18 ]
%22 = add nsw i64 %19, -1
%23 = and i64 %22, 4294967295
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = tail call i32 @charToInt(i8 signext %25)
%27 = tail call i32 @fastPower10(i32 %20)
%28 = mul nsw i32 %27, %26
%29 = add nsw i32 %28, %21
%30 = add nuw nsw i32 %20, 1
%31 = icmp eq i32 %30, %15
br i1 %31, label %16, label %18
}