#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 char u_char;
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *mem_sha512sum(char *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *realloc(char *, int);
char *hmac(const u_char *ipad, const u_char *opad, u_char **msg, ssize_t len) {
  char *inner, *outer;
  if ((*msg = realloc(*msg, len + 256)) == NULL)
    return NULL;
  memcpy(*msg + len, ipad, 256);
  if ((inner = mem_sha512sum(*msg, len + 256)) == NULL)
    return (NULL);
  if ((inner = realloc(inner, 128 + 256)) == NULL) {
    free(inner);
    return NULL;
  }
  memcpy(&inner[128], opad, 256);
  outer = mem_sha512sum((u_char *)inner, 128 + 256);
  free(inner);
  return outer;
}


x86 assembly (clang, -O0):
<hmac>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
add    $0x100,%esi
call   1050 <realloc@plt>
mov    -0x20(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    118a <hmac+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    1244 <hmac+0x104>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    $0x100,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
add    $0x100,%esi
call   1060 <mem_sha512sum@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11d6 <hmac+0x96>
movq   $0x0,-0x8(%rbp)
jmp    1244 <hmac+0x104>
mov    -0x30(%rbp),%rdi
mov    $0x180,%esi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1208 <hmac+0xc8>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <hmac+0x104>
mov    -0x30(%rbp),%rdi
add    $0x80,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x100,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    $0x180,%esi
call   1060 <mem_sha512sum@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hmac>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdx),%rdi
lea    0x100(%rbp),%r12d
mov    %r12d,%esi
call   1050 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11e0 <hmac+0xa0>
movslq %ebp,%rcx
add    %rcx,%rax
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x100,%edx
call   1040 <memcpy@plt>
mov    (%rbx),%rdi
mov    %r12d,%esi
call   1060 <mem_sha512sum@plt>
test   %rax,%rax
je     11e0 <hmac+0xa0>
mov    %rax,%rdi
mov    $0x180,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     11d9 <hmac+0x99>
mov    %rax,%rbx
mov    %rax,%rdi
sub    $0xffffffffffffff80,%rdi
mov    %r14,%rsi
mov    $0x100,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x180,%esi
call   1060 <mem_sha512sum@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
jmp    11e2 <hmac+0xa2>
xor    %edi,%edi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @hmac(i8* %0, i8* %1, i8** %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8** %2, i8*** %8
store i32 %3, i32* %9
%12 = load i8**, i8*** %8
%13 = load i8*, i8** %12
%14 = load i32, i32* %9
%15 = add nsw i32 %14, 256
%16 = call i8* @realloc(i8* %13, i32 %15)
%17 = load i8**, i8*** %8
store i8* %16, i8** %17
%18 = icmp eq i8* %16, null
br i1 %18, label %19, label %20
19: ; preds = %4
store i8* null, i8** %5
br label %52
20: ; preds = %4
%21 = load i8**, i8*** %8
%22 = load i8*, i8** %21
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8*, i8** %6
%27 = call i32 @memcpy(i8* %25, i8* %26, i32 256)
%28 = load i8**, i8*** %8
%29 = load i8*, i8** %28
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 256
%32 = call i8* @mem_sha512sum(i8* %29, i32 %31)
store i8* %32, i8** %10
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %20
store i8* null, i8** %5
br label %52
35: ; preds = %20
%36 = load i8*, i8** %10
%37 = call i8* @realloc(i8* %36, i32 384)
store i8* %37, i8** %10
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i8*, i8** %10
%41 = call i32 @free(i8* %40)
store i8* null, i8** %5
br label %52
42: ; preds = %35
%43 = load i8*, i8** %10
%44 = getelementptr inbounds i8, i8* %43, i64 128
%45 = load i8*, i8** %7
%46 = call i32 @memcpy(i8* %44, i8* %45, i32 256)
%47 = load i8*, i8** %10
%48 = call i8* @mem_sha512sum(i8* %47, i32 384)
store i8* %48, i8** %11
%49 = load i8*, i8** %10
%50 = call i32 @free(i8* %49)
%51 = load i8*, i8** %11
store i8* %51, i8** %5
br label %52
52: ; preds = %42, %39, %34, %19
%53 = load i8*, i8** %5
ret i8* %53
}

LLVM IR (-O2):
define i8* @hmac(i8* %0, i8* %1, i8** nocapture %2, i32 %3) {
%5 = load i8*, i8** %2
%6 = add nsw i32 %3, 256
%7 = tail call i8* @realloc(i8* %5, i32 %6)
store i8* %7, i8** %2
%8 = icmp eq i8* %7, null
br i1 %8, label %26, label %9
9: ; preds = %4
%10 = sext i32 %3 to i64
%11 = getelementptr inbounds i8, i8* %7, i64 %10
%12 = tail call i32 @memcpy(i8* %11, i8* %0, i32 256)
%13 = load i8*, i8** %2
%14 = tail call i8* @mem_sha512sum(i8* %13, i32 %6)
%15 = icmp eq i8* %14, null
br i1 %15, label %26, label %16
16: ; preds = %9
%17 = tail call i8* @realloc(i8* %14, i32 384)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @free(i8* null)
br label %26
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %17, i64 128
%23 = tail call i32 @memcpy(i8* %22, i8* %1, i32 256)
%24 = tail call i8* @mem_sha512sum(i8* %17, i32 384)
%25 = tail call i32 @free(i8* %17)
br label %26
26: ; preds = %9, %4, %21, %19
%27 = phi i8* [ null, %19 ], [ %24, %21 ], [ null, %4 ], [ null, %9 ]
ret i8* %27
}