#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 */
long hchartoi(char, int);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
long htoi(char s[]) {
  char *p = &s[strlen(s) - 1];
  long deci = 0, dig = 0;
  int pos = 0;
  while (p >= s) {
    if ((dig = hchartoi(*p, pos)) < 0) {
      printf("Error\n");
      return -1;
    }
    deci += dig;
    --p;
    ++pos;
  }
  return deci;
}


x86 assembly (clang, -O0):
<htoi>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     11e3 <htoi+0xb3>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    -0x2c(%rbp),%esi
movsbl %al,%edi
call   1050 <hchartoi@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11bd <htoi+0x8d>
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11eb <htoi+0xbb>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1177 <htoi+0x47>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htoi>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
xor    %r15d,%r15d
test   %eax,%eax
jle    1182 <htoi+0x52>
mov    %eax,%ebx
add    $0xffffffff,%ebx
add    %r14,%rbx
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl (%rbx),%edi
mov    %r15d,%esi
call   1050 <hchartoi@plt>
test   %rax,%rax
js     1187 <htoi+0x57>
add    %rax,%r12
add    $0xffffffffffffffff,%rbx
add    $0x1,%r15d
cmp    %r14,%rbx
jae    1160 <htoi+0x30>
jmp    119a <htoi+0x6a>
xor    %r12d,%r12d
jmp    119a <htoi+0x6a>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffffffffffff,%r12
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"Error\0A\00"
define i64 @htoi(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
store i8* %13, i8** %4
store i64 0, i64* %5
store i64 0, i64* %6
store i32 0, i32* %7
br label %14
14: ; preds = %26, %1
%15 = load i8*, i8** %4
%16 = load i8*, i8** %3
%17 = icmp uge i8* %15, %16
br i1 %17, label %18, label %34
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = load i32, i32* %7
%22 = call i64 @hchartoi(i8 signext %20, i32 %21)
store i64 %22, i64* %6
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %2
br label %36
26: ; preds = %18
%27 = load i64, i64* %6
%28 = load i64, i64* %5
%29 = add nsw i64 %28, %27
store i64 %29, i64* %5
%30 = load i8*, i8** %4
%31 = getelementptr inbounds i8, i8* %30, i32 -1
store i8* %31, i8** %4
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %14
34: ; preds = %14
%35 = load i64, i64* %5
store i64 %35, i64* %2
br label %36
36: ; preds = %34, %24
%37 = load i64, i64* %2
ret i64 %37
}

LLVM IR (-O2):
@str = [6 x i8] c"Error\00"
define i64 @htoi(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 1
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = add nsw i32 %2, -1
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
br label %8
8: ; preds = %4, %17
%9 = phi i32 [ %20, %17 ], [ 0, %4 ]
%10 = phi i64 [ %18, %17 ], [ 0, %4 ]
%11 = phi i8* [ %19, %17 ], [ %7, %4 ]
%12 = load i8, i8* %11
%13 = tail call i64 @hchartoi(i8 signext %12, i32 %9)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %22
17: ; preds = %8
%18 = add nsw i64 %13, %10
%19 = getelementptr inbounds i8, i8* %11, i64 -1
%20 = add nuw nsw i32 %9, 1
%21 = icmp ult i8* %19, %0
br i1 %21, label %22, label %8
22: ; preds = %17, %1, %15
%23 = phi i64 [ -1, %15 ], [ 0, %1 ], [ %18, %17 ]
ret i64 %23
}