#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 length(char *);
char pwrTen(int);
unsigned int str2dec(char *str) {
  int l = length(str) - 1;
  int total = 0;
  while (*str) {
    total = total + ((*str - '0') * pwrTen(l));
    l--;
    str++;
  }
  return total;
}


x86 assembly (clang, -O0):
<str2dec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <length@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpb   $0x0,(%rax)
je     1197 <str2dec+0x77>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <pwrTen@plt>
mov    -0x18(%rbp),%ecx
mov    %al,%dl
mov    -0x14(%rbp),%eax
movsbl %dl,%edx
imul   %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1142 <str2dec+0x22>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2dec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <length@plt>
mov    %eax,%ebp
mov    (%r15),%al
test   %al,%al
je     1177 <str2dec+0x57>
add    $0xffffffff,%ebp
add    $0x1,%r15
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl %al,%ebx
add    $0xffffffd0,%ebx
mov    %ebp,%edi
call   1040 <pwrTen@plt>
movsbl %al,%eax
imul   %ebx,%eax
add    %eax,%r14d
movzbl (%r15),%eax
add    $0xffffffff,%ebp
add    $0x1,%r15
test   %al,%al
jne    1150 <str2dec+0x30>
jmp    117a <str2dec+0x5a>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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