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

/* Variables and functions */
int HmacSha256Hash(int /*<<< orphan*/ *, int, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int ReadBinaryFile(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ printf(char *);
int AppendHMACSignature(uint8_t *data, uint32_t length, char *filename,
                        uint32_t offset) {
  uint8_t hmackey[32];
  uint32_t len;
  uint32_t status;
  uint8_t *digest = data + length;
  len = ReadBinaryFile(filename, hmackey, 32);
  if (len != 32) {
    printf("Error reading hmac key file\n");
    return 0;
  }
  status = HmacSha256Hash(&data[offset], length - offset, digest, hmackey);
  if (status) {
    printf("HMAC-SHA256 hash error\n");
    return 0;
  }
  return 32;
}


x86 assembly (clang, -O0):
<AppendHMACSignature>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0xc0(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0xb0(%rbp),%rsi
mov    $0x20,%edx
call   1040 <ReadBinaryFile@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x20,-0xb4(%rbp)
je     119f <AppendHMACSignature+0x6f>
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <AppendHMACSignature+0xc9>
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x24(%rbp),%esi
mov    -0xc0(%rbp),%rdx
lea    -0xb0(%rbp),%rcx
call   1050 <HmacSha256Hash@plt>
mov    %eax,-0xb8(%rbp)
cmpl   $0x0,-0xb8(%rbp)
je     11f2 <AppendHMACSignature+0xc2>
lea    0xe3c(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f9 <AppendHMACSignature+0xc9>
movl   $0x20,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AppendHMACSignature>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %ecx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    %rsp,%rsi
mov    $0x20,%ebx
mov    %rdx,%rdi
mov    $0x20,%edx
call   1040 <ReadBinaryFile@plt>
cmp    $0x20,%eax
jne    1193 <AppendHMACSignature+0x63>
movslq %r15d,%rsi
lea    0x0(,%rsi,4),%rdx
add    %rbp,%rdx
movslq %r14d,%rax
lea    0x0(,%rax,4),%rdi
add    %rbp,%rdi
sub    %eax,%esi
mov    %rsp,%rcx
call   1050 <HmacSha256Hash@plt>
test   %eax,%eax
je     11a1 <AppendHMACSignature+0x71>
lea    0xe6f(%rip),%rdi
jmp    119a <AppendHMACSignature+0x6a>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Error reading hmac key file\0A\00"
@.str.1 = [24 x i8] c"HMAC-SHA256 hash error\0A\00"
define i32 @AppendHMACSignature(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [32 x i32]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i32* %0, i32** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i32*, i32** %6
%15 = load i32, i32* %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %14, i64 %16
store i32* %17, i32** %13
%18 = load i8*, i8** %8
%19 = getelementptr inbounds [32 x i32], [32 x i32]* %10, i64 0, i64 0
%20 = call i32 @ReadBinaryFile(i8* %18, i32* %19, i32 32)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 32
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %41
25: ; preds = %4
%26 = load i32*, i32** %6
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %7
%31 = load i32, i32* %9
%32 = sub nsw i32 %30, %31
%33 = load i32*, i32** %13
%34 = getelementptr inbounds [32 x i32], [32 x i32]* %10, i64 0, i64 0
%35 = call i32 @HmacSha256Hash(i32* %29, i32 %32, i32* %33, i32* %34)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %25
%39 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %41
40: ; preds = %25
store i32 32, i32* %5
br label %41
41: ; preds = %40, %38, %23
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@str = [23 x i8] c"HMAC-SHA256 hash error\00"
@str.2 = [28 x i8] c"Error reading hmac key file\00"
define i32 @AppendHMACSignature(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [32 x i32]
%6 = bitcast [32 x i32]* %5 to i8*
%7 = getelementptr inbounds [32 x i32], [32 x i32]* %5, i64 0, i64 0
%8 = call i32 @ReadBinaryFile(i8* %2, i32* %7, i32 32)
%9 = icmp eq i32 %8, 32
br i1 %9, label %10, label %18
10: ; preds = %4
%11 = sext i32 %1 to i64
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = sext i32 %3 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = sub nsw i32 %1, %3
%16 = call i32 @HmacSha256Hash(i32* %14, i32 %15, i32* %12, i32* %7)
%17 = icmp eq i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %10, %4
%19 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0), %10 ]
%20 = call i32 @puts(i8* dereferenceable(1) %19)
br label %21
21: ; preds = %18, %10
%22 = phi i32 [ 32, %10 ], [ 0, %18 ]
ret i32 %22
}