#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*/ const uint8_t;

/* Variables and functions */
int /*<<< orphan*/ XOR(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *,
                       int const);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *, int const);
int /*<<< orphan*/ memset(int /*<<< orphan*/ const *, int, int const);
int /*<<< orphan*/ sha256(char *, char *, int const);
void HMAC_SHA256(const uint8_t *key, int klen, const uint8_t *message, int mlen,
                 uint8_t *out) {
  const int bsize = 64;
  const int hsize = 32;
  uint8_t temp[mlen + bsize];
  uint8_t usekey[bsize];
  uint8_t okp[bsize], ikp[bsize];
  memcpy(usekey, key, klen);
  if (klen > bsize) {
    sha256((char *)key, (char *)usekey, klen);
    klen = hsize;
  }
  if (klen < bsize) {
    memset(usekey + klen, 0, bsize - klen);
  }
  memset(okp, 0x5c, bsize);
  memset(ikp, 0x36, bsize);
  XOR(okp, usekey, bsize);
  XOR(ikp, usekey, bsize);
  memcpy(temp, ikp, bsize);
  memcpy(temp + bsize, message, mlen);
  sha256((char *)temp, (char *)out, mlen + bsize);
  memcpy(temp, okp, bsize);
  memcpy(temp + bsize, out, hsize);
  sha256((char *)temp, (char *)out, bsize + hsize);
}


x86 assembly (clang, -O0):
<HMAC_SHA256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x40,-0x2c(%rbp)
movl   $0x20,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x40,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x348(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x140(%rbp),%rdi
call   1050 <memcpy@plt>
cmpl   $0x40,-0xc(%rbp)
jle    11cf <HMAC_SHA256+0x8f>
mov    -0x8(%rbp),%rdi
lea    -0x140(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <sha256@plt>
movl   $0x20,-0xc(%rbp)
cmpl   $0x40,-0xc(%rbp)
jge    11fa <HMAC_SHA256+0xba>
movslq -0xc(%rbp),%rax
lea    -0x140(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
mov    $0x40,%edx
sub    -0xc(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
lea    -0x240(%rbp),%rdi
mov    $0x5c,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
lea    -0x340(%rbp),%rdi
mov    $0x36,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
lea    -0x240(%rbp),%rdi
lea    -0x140(%rbp),%rsi
mov    $0x40,%edx
call   1060 <XOR@plt>
lea    -0x340(%rbp),%rdi
lea    -0x140(%rbp),%rsi
mov    $0x40,%edx
call   1060 <XOR@plt>
mov    -0x348(%rbp),%rdi
lea    -0x340(%rbp),%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
mov    -0x348(%rbp),%rdi
add    $0x100,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x348(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x40,%edx
call   1040 <sha256@plt>
mov    -0x348(%rbp),%rdi
lea    -0x240(%rbp),%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
mov    -0x348(%rbp),%rdi
add    $0x100,%rdi
mov    -0x28(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    -0x348(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x60,%edx
call   1040 <sha256@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<HMAC_SHA256>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %r8,%r14
mov    %rdx,-0x30(%rbp)
mov    %esi,%ebx
mov    %rdi,%r13
mov    %rcx,-0x38(%rbp)
lea    0x40(%rcx),%eax
mov    %rsp,%r15
mov    %rax,-0x40(%rbp)
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x140(%rbp),%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
cmp    $0x41,%ebx
jl     11ae <HMAC_SHA256+0x6e>
lea    -0x140(%rbp),%rsi
mov    %r13,%rdi
mov    %ebx,%edx
call   1040 <sha256@plt>
mov    $0x20,%ebx
jmp    11b3 <HMAC_SHA256+0x73>
cmp    $0x40,%ebx
je     11cf <HMAC_SHA256+0x8f>
movslq %ebx,%rax
lea    -0x140(,%rax,4),%rdi
ff
add    %rbp,%rdi
mov    $0x40,%edx
sub    %eax,%edx
xor    %esi,%esi
call   1030 <memset@plt>
lea    -0x340(%rbp),%r13
mov    %r13,%rdi
mov    $0x5c,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
lea    -0x240(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x36,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
lea    -0x140(%rbp),%r12
mov    %r13,%rdi
mov    %r12,%rsi
mov    $0x40,%edx
call   1060 <XOR@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x40,%edx
call   1060 <XOR@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
lea    0x100(%r15),%rbx
mov    %rbx,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <memcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    -0x40(%rbp),%rdx
call   1040 <sha256@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x60,%edx
call   1040 <sha256@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @HMAC_SHA256(i32* %0, i32 %1, i32* %2, i32 %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32* %4, i32** %10
store i32 64, i32* %11
store i32 32, i32* %12
%15 = load i32, i32* %9
%16 = add nsw i32 %15, 64
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %13
%19 = alloca i32, i64 %17
store i64 %17, i64* %14
%20 = alloca i32, i64 64
%21 = alloca i32, i64 64
%22 = alloca i32, i64 64
%23 = load i32*, i32** %6
%24 = load i32, i32* %7
%25 = call i32 @memcpy(i32* %20, i32* %23, i32 %24)
%26 = load i32, i32* %7
%27 = icmp sgt i32 %26, 64
br i1 %27, label %28, label %34
28: ; preds = %5
%29 = load i32*, i32** %6
%30 = bitcast i32* %29 to i8*
%31 = bitcast i32* %20 to i8*
%32 = load i32, i32* %7
%33 = call i32 @sha256(i8* %30, i8* %31, i32 %32)
store i32 32, i32* %7
br label %34
34: ; preds = %28, %5
%35 = load i32, i32* %7
%36 = icmp slt i32 %35, 64
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %20, i64 %39
%41 = load i32, i32* %7
%42 = sub nsw i32 64, %41
%43 = call i32 @memset(i32* %40, i32 0, i32 %42)
br label %44
44: ; preds = %37, %34
%45 = call i32 @memset(i32* %21, i32 92, i32 64)
%46 = call i32 @memset(i32* %22, i32 54, i32 64)
%47 = call i32 @XOR(i32* %21, i32* %20, i32 64)
%48 = call i32 @XOR(i32* %22, i32* %20, i32 64)
%49 = call i32 @memcpy(i32* %19, i32* %22, i32 64)
%50 = getelementptr inbounds i32, i32* %19, i64 64
%51 = load i32*, i32** %8
%52 = load i32, i32* %9
%53 = call i32 @memcpy(i32* %50, i32* %51, i32 %52)
%54 = bitcast i32* %19 to i8*
%55 = load i32*, i32** %10
%56 = bitcast i32* %55 to i8*
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 64
%59 = call i32 @sha256(i8* %54, i8* %56, i32 %58)
%60 = call i32 @memcpy(i32* %19, i32* %21, i32 64)
%61 = getelementptr inbounds i32, i32* %19, i64 64
%62 = load i32*, i32** %10
%63 = call i32 @memcpy(i32* %61, i32* %62, i32 32)
%64 = bitcast i32* %19 to i8*
%65 = load i32*, i32** %10
%66 = bitcast i32* %65 to i8*
%67 = call i32 @sha256(i8* %64, i8* %66, i32 96)
%68 = load i8*, i8** %13
ret void
}

LLVM IR (-O2):
define void @HMAC_SHA256(i32* %0, i32 %1, i32* %2, i32 %3, i32* %4) {
%6 = add nsw i32 %3, 64
%7 = zext i32 %6 to i64
%8 = alloca i32, i64 %7
%9 = alloca [64 x i32]
%10 = alloca [64 x i32]
%11 = alloca [64 x i32]
%12 = getelementptr inbounds [64 x i32], [64 x i32]* %11, i64 0, i64 0
%13 = getelementptr inbounds [64 x i32], [64 x i32]* %10, i64 0, i64 0
%14 = getelementptr inbounds [64 x i32], [64 x i32]* %9, i64 0, i64 0
%15 = call i32 @memcpy(i32* %14, i32* %0, i32 %1)
%16 = icmp sgt i32 %1, 64
br i1 %16, label %17, label %21
17: ; preds = %5
%18 = bitcast i32* %0 to i8*
%19 = bitcast [64 x i32]* %9 to i8*
%20 = call i32 @sha256(i8* %18, i8* %19, i32 %1)
br label %23
21: ; preds = %5
%22 = icmp eq i32 %1, 64
br i1 %22, label %29, label %23
23: ; preds = %17, %21
%24 = phi i32 [ 32, %17 ], [ %1, %21 ]
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [64 x i32], [64 x i32]* %9, i64 0, i64 %25
%27 = sub nsw i32 64, %24
%28 = call i32 @memset(i32* %26, i32 0, i32 %27)
br label %29
29: ; preds = %23, %21
%30 = call i32 @memset(i32* %13, i32 92, i32 64)
%31 = call i32 @memset(i32* %12, i32 54, i32 64)
%32 = call i32 @XOR(i32* %13, i32* %14, i32 64)
%33 = call i32 @XOR(i32* %12, i32* %14, i32 64)
%34 = call i32 @memcpy(i32* %8, i32* %12, i32 64)
%35 = getelementptr inbounds i32, i32* %8, i64 64
%36 = call i32 @memcpy(i32* %35, i32* %2, i32 %3)
%37 = bitcast i32* %8 to i8*
%38 = bitcast i32* %4 to i8*
%39 = call i32 @sha256(i8* %37, i8* %38, i32 %6)
%40 = call i32 @memcpy(i32* %8, i32* %13, i32 64)
%41 = call i32 @memcpy(i32* %35, i32* %4, i32 32)
%42 = call i32 @sha256(i8* %37, i8* %38, i32 96)
ret void
}