#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 */
typedef int ssize_t;

/* Variables and functions */
int pow(int, int);
int /*<<< orphan*/ printf(char *, int, int, long);
long htolong(const unsigned char *src, ssize_t len) {
  int tmp;
  long sum = 0;
  for (; len > 0; --len) {
    tmp = (int)*src++;
    sum += tmp * pow(16, 2 * len - 2);
    printf("len = %d, tmp = %d, sum = %ld\n", len, tmp, sum);
  }
  return sum;
}


x86 assembly (clang, -O0):
<htolong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11a5 <htolong+0x85>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movzbl (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
shl    $0x1,%esi
sub    $0x2,%esi
mov    $0x10,%edi
call   1040 <pow@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
imul   %ecx,%eax
cltq
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
lea    0xe6e(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    1137 <htolong+0x17>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htolong>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1190 <htolong+0x70>
mov    %esi,%r15d
mov    %rdi,%r14
lea    (%r15,%r15,1),%ebp
add    $0xfffffffe,%ebp
xor    %ebx,%ebx
lea    0xebb(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r14),%r13d
add    $0x1,%r14
mov    $0x10,%edi
mov    %ebp,%esi
call   1040 <pow@plt>
cltq
imul   %r13,%rax
add    %rax,%rbx
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1030 <printf@plt>
lea    -0x1(%r15),%eax
add    $0xfffffffe,%ebp
cmp    $0x1,%r15d
mov    %eax,%r15d
ja     1150 <htolong+0x30>
jmp    1192 <htolong+0x72>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"len = %d, tmp = %d, sum = %ld\0A\00"
define i64 @htolong(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
store i64 0, i64* %6
br label %7
7: ; preds = %28, %2
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %7
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %3
%13 = load i8, i8* %11
%14 = zext i8 %13 to i32
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = mul nsw i32 2, %16
%18 = sub nsw i32 %17, 2
%19 = call i32 @pow(i32 16, i32 %18)
%20 = mul nsw i32 %15, %19
%21 = sext i32 %20 to i64
%22 = load i64, i64* %6
%23 = add nsw i64 %22, %21
store i64 %23, i64* %6
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = load i64, i64* %6
%27 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %25, i64 %26)
br label %28
28: ; preds = %10
%29 = load i32, i32* %4
%30 = add nsw i32 %29, -1
store i32 %30, i32* %4
br label %7
31: ; preds = %7
%32 = load i64, i64* %6
ret i64 %32
}

LLVM IR (-O2):
@.str = [31 x i8] c"len = %d, tmp = %d, sum = %ld\0A\00"
define i64 @htolong(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %20
4: ; preds = %2, %4
%5 = phi i64 [ %16, %4 ], [ 0, %2 ]
%6 = phi i8* [ %8, %4 ], [ %0, %2 ]
%7 = phi i32 [ %18, %4 ], [ %1, %2 ]
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = load i8, i8* %6
%10 = zext i8 %9 to i32
%11 = shl nuw nsw i32 %7, 1
%12 = add nsw i32 %11, -2
%13 = tail call i32 @pow(i32 16, i32 %12)
%14 = mul nsw i32 %13, %10
%15 = sext i32 %14 to i64
%16 = add nsw i64 %5, %15
%17 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10, i64 %16)
%18 = add nsw i32 %7, -1
%19 = icmp ugt i32 %7, 1
br i1 %19, label %4, label %20
20: ; preds = %4, %2
%21 = phi i64 [ 0, %2 ], [ %16, %4 ]
ret i64 %21
}