#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 /*<<< orphan*/ EVP_MD;

/* Variables and functions */
int EVP_MAX_MD_SIZE;
int /*<<< orphan*/ *EVP_sha1();
int /*<<< orphan*/ *HMAC(int /*<<< orphan*/ const *, void const *, int,
                         unsigned char const *, int, unsigned char *,
                         unsigned int *);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char);
int caslib_signature(char *out, size_t olen, const void *key, size_t klen,
                     const unsigned char *msg, size_t mlen) {
  const EVP_MD *f_digest = EVP_sha1();
  char hmac[EVP_MAX_MD_SIZE];
  unsigned int hlen, i, offset;
  if (HMAC(f_digest, key, (int)klen, msg, (int)mlen, (unsigned char *)hmac,
           &hlen) == NULL)
    return (-1);
  assert(41 - 1 == 2 * hlen);
  for (i = 0; i < hlen; i += 1) {
    offset = i * 2;
    if (offset < olen)
      snprintf(out + offset, olen - offset, "%02x", (hmac[i] & 0xff));
  }
  return (0);
}


x86 assembly (clang, -O0):
<caslib_signature>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
xor    %eax,%eax
call   1030 <EVP_sha1@plt>
mov    %rax,-0x40(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%r9
sub    %rcx,%r9
mov    %r9,-0x68(%rbp)
mov    %r9,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%rax
mov    %eax,%r8d
lea    -0x54(%rbp),%rax
sub    $0x10,%rsp
mov    %rax,(%rsp)
call   1060 <HMAC@plt>
add    $0x10,%rsp
cmp    $0x0,%rax
jne    11e2 <caslib_signature+0xa2>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x60(%rbp)
jmp    1279 <caslib_signature+0x139>
mov    -0x54(%rbp),%ecx
shl    $0x1,%ecx
mov    $0x28,%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movl   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%eax
cmp    -0x54(%rbp),%eax
jae    126b <caslib_signature+0x12b>
mov    -0x58(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    1258 <caslib_signature+0x118>
mov    -0x68(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x5c(%rbp),%ecx
add    %rcx,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x5c(%rbp),%ecx
sub    %rcx,%rsi
mov    -0x58(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
and    $0xff,%eax
lea    0xdb0(%rip),%rdx
movsbl %al,%ecx
call   1040 <snprintf@plt>
jmp    125d <caslib_signature+0x11d>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    1203 <caslib_signature+0xc3>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x60(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<caslib_signature>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,-0x40(%rbp)
mov    %r8,-0x38(%rbp)
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <EVP_sha1@plt>
mov    0x2e75(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%r15
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
sub    $0x8,%rsp
lea    -0x2c(%rbp),%r10
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%r8
mov    %r15,%r9
push   %r10
call   1060 <HMAC@plt>
add    $0x10,%rsp
test   %rax,%rax
je     1219 <caslib_signature+0xd9>
mov    $0x7fffffff,%eax
and    -0x2c(%rbp),%eax
xor    %edi,%edi
cmp    $0x14,%eax
sete   %dil
call   1050 <assert@plt>
mov    -0x2c(%rbp),%edx
test   %edx,%edx
mov    $0x0,%eax
je     1222 <caslib_signature+0xe2>
xor    %ebx,%ebx
xor    %r12d,%r12d
jmp    11ef <caslib_signature+0xaf>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r12
mov    %edx,%ecx
add    $0x2,%rbx
cmp    %rcx,%r12
jae    1220 <caslib_signature+0xe0>
mov    %ebx,%ecx
and    $0xfffffffe,%ecx
cmp    %r13,%rcx
jae    11e0 <caslib_signature+0xa0>
lea    (%r14,%rcx,1),%rdi
mov    %r13,%rsi
sub    %rcx,%rsi
movsbl (%r15,%r12,1),%ecx
lea    0xdf1(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x2c(%rbp),%edx
jmp    11e0 <caslib_signature+0xa0>
mov    $0xffffffff,%eax
jmp    1222 <caslib_signature+0xe2>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_MAX_MD_SIZE = global i32 0
@.str = [5 x i8] c"%02x\00"
define i32 @caslib_signature(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32*
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i8* %0, i8** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i64 %3, i64* %11
store i8* %4, i8** %12
store i64 %5, i64* %13
%21 = call i32* (...) @EVP_sha1()
store i32* %21, i32** %14
%22 = load i32, i32* @EVP_MAX_MD_SIZE
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %15
%25 = alloca i8, i64 %23
store i64 %23, i64* %16
%26 = load i32*, i32** %14
%27 = load i8*, i8** %10
%28 = load i64, i64* %11
%29 = trunc i64 %28 to i32
%30 = load i8*, i8** %12
%31 = load i64, i64* %13
%32 = trunc i64 %31 to i32
%33 = call i32* @HMAC(i32* %26, i8* %27, i32 %29, i8* %30, i32 %32, i8* %25, i32* %17)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %36
35: ; preds = %6
store i32 -1, i32* %7
store i32 1, i32* %20
br label %75
36: ; preds = %6
%37 = load i32, i32* %17
%38 = mul i32 2, %37
%39 = icmp eq i32 40, %38
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
store i32 0, i32* %18
br label %42
42: ; preds = %71, %36
%43 = load i32, i32* %18
%44 = load i32, i32* %17
%45 = icmp ult i32 %43, %44
br i1 %45, label %46, label %74
46: ; preds = %42
%47 = load i32, i32* %18
%48 = mul i32 %47, 2
store i32 %48, i32* %19
%49 = load i32, i32* %19
%50 = zext i32 %49 to i64
%51 = load i64, i64* %9
%52 = icmp ult i64 %50, %51
br i1 %52, label %53, label %70
53: ; preds = %46
%54 = load i8*, i8** %8
%55 = load i32, i32* %19
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i64, i64* %9
%59 = load i32, i32* %19
%60 = zext i32 %59 to i64
%61 = sub i64 %58, %60
%62 = load i32, i32* %18
%63 = zext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %25, i64 %63
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = and i32 %66, 255
%68 = trunc i32 %67 to i8
%69 = call i32 @snprintf(i8* %57, i64 %61, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %68)
br label %70
70: ; preds = %53, %46
br label %71
71: ; preds = %70
%72 = load i32, i32* %18
%73 = add i32 %72, 1
store i32 %73, i32* %18
br label %42
74: ; preds = %42
store i32 0, i32* %7
store i32 1, i32* %20
br label %75
75: ; preds = %74, %35
%76 = load i8*, i8** %15
%77 = load i32, i32* %7
ret i32 %77
}

LLVM IR (-O2):
@EVP_MAX_MD_SIZE = global i32 0
@.str = [5 x i8] c"%02x\00"
define i32 @caslib_signature(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4, i64 %5) {
%7 = alloca i32
%8 = tail call i32* (...) @EVP_sha1()
%9 = load i32, i32* @EVP_MAX_MD_SIZE
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = bitcast i32* %7 to i8*
%13 = trunc i64 %3 to i32
%14 = trunc i64 %5 to i32
%15 = call i32* @HMAC(i32* %8, i8* %2, i32 %13, i8* %4, i32 %14, i8* %11, i32* %7)
%16 = icmp eq i32* %15, null
br i1 %16, label %43, label %17
17: ; preds = %6
%18 = load i32, i32* %7
%19 = and i32 %18, 2147483647
%20 = icmp eq i32 %19, 20
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, 0
br i1 %24, label %43, label %25
25: ; preds = %17, %38
%26 = phi i32 [ %39, %38 ], [ %23, %17 ]
%27 = phi i64 [ %40, %38 ], [ 0, %17 ]
%28 = shl nuw i64 %27, 1
%29 = and i64 %28, 4294967294
%30 = icmp ult i64 %29, %1
br i1 %30, label %31, label %38
31: ; preds = %25
%32 = getelementptr inbounds i8, i8* %0, i64 %29
%33 = sub i64 %1, %29
%34 = getelementptr inbounds i8, i8* %11, i64 %27
%35 = load i8, i8* %34
%36 = call i32 @snprintf(i8* %32, i64 %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %35)
%37 = load i32, i32* %7
br label %38
38: ; preds = %25, %31
%39 = phi i32 [ %26, %25 ], [ %37, %31 ]
%40 = add nuw nsw i64 %27, 1
%41 = zext i32 %39 to i64
%42 = icmp ult i64 %40, %41
br i1 %42, label %25, label %43
43: ; preds = %38, %17, %6
%44 = phi i32 [ -1, %6 ], [ 0, %17 ], [ 0, %38 ]
ret i32 %44
}