#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 char2num(int /*<<< orphan*/, char);
int integer_power(int, int);
int str2integer(char *buf, int len) {
  if (!len) {
    return 0;
  }
  if (len == 1) {
    return char2num(0, buf[0]);
  }
  return char2num(0, buf[0]) * integer_power(10, len - 1) +
         str2integer(buf + 1, len - 1);
}


x86 assembly (clang, -O0):
<str2integer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1155 <str2integer+0x25>
movl   $0x0,-0x4(%rbp)
jmp    11be <str2integer+0x8e>
cmpl   $0x1,-0x14(%rbp)
jne    1175 <str2integer+0x45>
mov    -0x10(%rbp),%rax
xor    %edi,%edi
movsbl (%rax),%esi
call   1040 <char2num@plt>
mov    %eax,-0x4(%rbp)
jmp    11be <str2integer+0x8e>
mov    -0x10(%rbp),%rax
xor    %edi,%edi
movsbl (%rax),%esi
call   1040 <char2num@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
mov    $0xa,%edi
call   1050 <integer_power@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
mov    -0x14(%rbp),%esi
sub    $0x1,%esi
call   1030 <str2integer@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str2integer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
xor    %r15d,%r15d
test   %ebx,%ebx
je     117e <str2integer+0x5e>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x1,%ebx
je     1171 <str2integer+0x51>
movsbl (%r14),%esi
xor    %edi,%edi
call   1030 <char2num@plt>
mov    %eax,%ebp
add    $0xffffffff,%ebx
mov    $0xa,%edi
mov    %ebx,%esi
call   1040 <integer_power@plt>
imul   %ebp,%eax
add    $0x1,%r14
add    %eax,%r15d
test   %ebx,%ebx
jne    1140 <str2integer+0x20>
jmp    117e <str2integer+0x5e>
movsbl (%r14),%esi
xor    %edi,%edi
call   1030 <char2num@plt>
mov    %eax,%ebx
add    %r15d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @str2integer(i8* nocapture %0, i32 %1) {
br label %3
3: ; preds = %10, %2
%4 = phi i32 [ 0, %2 ], [ %17, %10 ]
%5 = phi i8* [ %0, %2 ], [ %16, %10 ]
%6 = phi i32 [ %1, %2 ], [ %13, %10 ]
switch i32 %6, label %10 [
i32 0, label %18
i32 1, label %7
]
7: ; preds = %3
%8 = load i8, i8* %5
%9 = tail call i32 @char2num(i32 0, i8 signext %8)
br label %18
10: ; preds = %3
%11 = load i8, i8* %5
%12 = tail call i32 @char2num(i32 0, i8 signext %11)
%13 = add nsw i32 %6, -1
%14 = tail call i32 @integer_power(i32 10, i32 %13)
%15 = mul nsw i32 %14, %12
%16 = getelementptr inbounds i8, i8* %5, i64 1
%17 = add nsw i32 %15, %4
br label %3
18: ; preds = %3, %7
%19 = phi i32 [ %9, %7 ], [ %6, %3 ]
%20 = add nsw i32 %19, %4
ret i32 %20
}