#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 uint8_t;
typedef int uint64_t;
typedef int uint32_t;
typedef int int32_t;

/* Variables and functions */
int /*<<< orphan*/ EVP_sha1();
int /*<<< orphan*/ HMAC(int /*<<< orphan*/, int const *, size_t const, int *,
                        int, int *, int /*<<< orphan*/ *);
int SHA_DIGEST_LENGTH;
int32_t hotp(const uint8_t *K, const size_t Klen, uint64_t C,
             const int digits) {
  uint8_t counter[8];
  for (int i = 8; i-- > 0;) {
    counter[i] = C;
    C >>= 8;
  }
  uint8_t hash[SHA_DIGEST_LENGTH];
  HMAC(EVP_sha1(), K, Klen, counter, 8, hash, NULL);
  int offset = hash[SHA_DIGEST_LENGTH - 1] & 0x0F;
  uint32_t value = 0;
  for (int i = 0; i < 4; ++i) {
    value <<= 8;
    value |= hash[offset + i];
  }
  value &= 0x7FFFFFFF;
  static uint32_t pow10[10] = {1,         10,        100,     1000,
                               10000,     100000,    1000000, 10000000,
                               100000000, 1000000000};
  return value % pow10[digits];
}


x86 assembly (clang, -O0):
<hotp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x8,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x44(%rbp)
cmp    $0x0,%eax
jle    116a <hotp+0x4a>
mov    -0x14(%rbp),%ecx
movslq -0x44(%rbp),%rax
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x14(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x14(%rbp)
jmp    113d <hotp+0x1d>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1030 <EVP_sha1@plt>
mov    -0x70(%rbp),%r9
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x40(%rbp),%rcx
mov    $0x8,%r8d
xor    %eax,%eax
sub    $0x10,%rsp
movq   $0x0,(%rsp)
call   1040 <HMAC@plt>
add    $0x10,%rsp
mov    -0x70(%rbp),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
and    $0xf,%eax
mov    %eax,-0x5c(%rbp)
movl   $0x0,-0x60(%rbp)
movl   $0x0,-0x64(%rbp)
cmpl   $0x4,-0x64(%rbp)
jge    122e <hotp+0x10e>
mov    -0x70(%rbp),%rax
mov    -0x60(%rbp),%ecx
shl    $0x8,%ecx
mov    %ecx,-0x60(%rbp)
mov    -0x5c(%rbp),%ecx
add    -0x64(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
or     -0x60(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    11f7 <hotp+0xd7>
mov    -0x60(%rbp),%eax
and    $0x7fffffff,%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%eax
movslq -0x18(%rbp),%rsi
lea    0x2df9(%rip),%rcx
cltd
idivl  (%rcx,%rsi,4)
mov    %edx,%eax
mov    -0x50(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hotp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,-0x2c(%rbp)
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rsp,%r13
mov    %edx,-0x34(%rbp)
mov    %edx,%eax
sar    $0x8,%eax
mov    %eax,-0x38(%rbp)
mov    %edx,%eax
sar    $0x10,%eax
mov    %eax,-0x3c(%rbp)
movd   %edx,%xmm0
sar    $0x18,%edx
mov    %edx,-0x40(%rbp)
pshufd $0x0,%xmm0,%xmm0
psrad  $0x1f,%xmm0
movdqa %xmm0,-0x50(%rbp)
mov    0x2e78(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   1030 <EVP_sha1@plt>
sub    $0x8,%rsp
lea    -0x50(%rbp),%rcx
mov    %eax,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    $0x8,%r8d
mov    %rbx,%r9
push   $0x0
call   1040 <HMAC@plt>
add    $0x10,%rsp
movslq (%r14),%rax
mov    -0x4(%rbx,%rax,4),%ecx
and    $0xf,%ecx
mov    (%rbx,%rcx,4),%edx
mov    0x4(%rbx,%rcx,4),%eax
shl    $0x10,%edx
shl    $0x8,%eax
or     %edx,%eax
or     0x8(%rbx,%rcx,4),%eax
shl    $0x8,%eax
or     0xc(%rbx,%rcx,4),%eax
and    $0x7fffffff,%eax
movslq -0x2c(%rbp),%rcx
lea    0xe19(%rip),%rsi
xor    %edx,%edx
idivl  (%rsi,%rcx,4)
mov    %edx,%eax
mov    %r13,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA_DIGEST_LENGTH = global i32 0
@hotp.pow10 = internal global [10 x i32] [i32 1, i32 10, i32 100, i32 1000, i32 10000, i32 100000, i32 1000000, i32 10000000, i32 100000000, i32 1000000000]
define i32 @hotp(i32* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca [8 x i32]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32* %0, i32** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 8, i32* %10
br label %16
16: ; preds = %20, %4
%17 = load i32, i32* %10
%18 = add nsw i32 %17, -1
store i32 %18, i32* %10
%19 = icmp sgt i32 %17, 0
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i32, i32* %7
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %9, i64 0, i64 %23
store i32 %21, i32* %24
%25 = load i32, i32* %7
%26 = ashr i32 %25, 8
store i32 %26, i32* %7
br label %16
27: ; preds = %16
%28 = load i32, i32* @SHA_DIGEST_LENGTH
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %11
%31 = alloca i32, i64 %29
store i64 %29, i64* %12
%32 = call i32 (...) @EVP_sha1()
%33 = load i32*, i32** %5
%34 = load i64, i64* %6
%35 = getelementptr inbounds [8 x i32], [8 x i32]* %9, i64 0, i64 0
%36 = call i32 @HMAC(i32 %32, i32* %33, i64 %34, i32* %35, i32 8, i32* %31, i32* null)
%37 = load i32, i32* @SHA_DIGEST_LENGTH
%38 = sub nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %31, i64 %39
%41 = load i32, i32* %40
%42 = and i32 %41, 15
store i32 %42, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
br label %43
43: ; preds = %57, %27
%44 = load i32, i32* %15
%45 = icmp slt i32 %44, 4
br i1 %45, label %46, label %60
46: ; preds = %43
%47 = load i32, i32* %14
%48 = shl i32 %47, 8
store i32 %48, i32* %14
%49 = load i32, i32* %13
%50 = load i32, i32* %15
%51 = add nsw i32 %49, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %31, i64 %52
%54 = load i32, i32* %53
%55 = load i32, i32* %14
%56 = or i32 %55, %54
store i32 %56, i32* %14
br label %57
57: ; preds = %46
%58 = load i32, i32* %15
%59 = add nsw i32 %58, 1
store i32 %59, i32* %15
br label %43
60: ; preds = %43
%61 = load i32, i32* %14
%62 = and i32 %61, 2147483647
store i32 %62, i32* %14
%63 = load i32, i32* %14
%64 = load i32, i32* %8
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [10 x i32], [10 x i32]* @hotp.pow10, i64 0, i64 %65
%67 = load i32, i32* %66
%68 = srem i32 %63, %67
%69 = load i8*, i8** %11
ret i32 %68
}

LLVM IR (-O2):
@SHA_DIGEST_LENGTH = global i32 0
@hotp.pow10 = internal unnamed_addr constant [10 x i32] [i32 1, i32 10, i32 100, i32 1000, i32 10000, i32 100000, i32 1000000, i32 10000000, i32 100000000, i32 1000000000]
define i32 @hotp(i32* %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca [8 x i32]
%6 = bitcast [8 x i32]* %5 to i8*
%7 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 7
store i32 %2, i32* %7
%8 = ashr i32 %2, 8
%9 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 6
store i32 %8, i32* %9
%10 = ashr i32 %2, 16
%11 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 5
store i32 %10, i32* %11
%12 = ashr i32 %2, 24
%13 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 4
store i32 %12, i32* %13
%14 = ashr i32 %2, 31
%15 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%16 = insertelement <4 x i32> poison, i32 %14, i64 0
%17 = shufflevector <4 x i32> %16, <4 x i32> poison, <4 x i32> zeroinitializer
%18 = bitcast [8 x i32]* %5 to <4 x i32>*
store <4 x i32> %17, <4 x i32>* %18
%19 = load i32, i32* @SHA_DIGEST_LENGTH
%20 = zext i32 %19 to i64
%21 = tail call i8* @llvm.stacksave()
%22 = alloca i32, i64 %20
%23 = tail call i32 (...) @EVP_sha1()
%24 = call i32 @HMAC(i32 %23, i32* %0, i64 %1, i32* %15, i32 8, i32* %22, i32* null)
%25 = load i32, i32* @SHA_DIGEST_LENGTH
%26 = add nsw i32 %25, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %22, i64 %27
%29 = load i32, i32* %28
%30 = and i32 %29, 15
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %22, i64 %31
%33 = load i32, i32* %32
%34 = add nuw nsw i64 %31, 1
%35 = getelementptr inbounds i32, i32* %22, i64 %34
%36 = load i32, i32* %35
%37 = shl i32 %33, 16
%38 = shl i32 %36, 8
%39 = or i32 %37, %38
%40 = add nuw nsw i64 %31, 2
%41 = getelementptr inbounds i32, i32* %22, i64 %40
%42 = load i32, i32* %41
%43 = or i32 %42, %39
%44 = shl i32 %43, 8
%45 = add nuw nsw i64 %31, 3
%46 = getelementptr inbounds i32, i32* %22, i64 %45
%47 = load i32, i32* %46
%48 = or i32 %47, %44
%49 = and i32 %48, 2147483647
%50 = sext i32 %3 to i64
%51 = getelementptr inbounds [10 x i32], [10 x i32]* @hotp.pow10, i64 0, i64 %50
%52 = load i32, i32* %51
%53 = srem i32 %49, %52
ret i32 %53
}