#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 unsigned char uint8_t;

/* Variables and functions */
int /*<<< orphan*/ hmac_sha1_vector(unsigned char const *, size_t, int,
                                    unsigned char const **, size_t *,
                                    unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, size_t);
size_t strlen(char const *);
void sha1_prf(const uint8_t *key, size_t key_len, const char *label,
              const uint8_t *data, size_t data_len, uint8_t *buf,
              size_t buf_len) {
  uint8_t zero = 0, counter = 0;
  size_t pos, plen;
  uint8_t hash[20];
  size_t label_len = strlen(label);
  const unsigned char *addr[4];
  size_t len[4];
  addr[0] = (uint8_t *)label;
  len[0] = label_len;
  addr[1] = &zero;
  len[1] = 1;
  addr[2] = data;
  len[2] = data_len;
  addr[3] = &counter;
  len[3] = 1;
  pos = 0;
  while (pos < buf_len) {
    plen = buf_len - pos;
    if (plen >= 20) {
      hmac_sha1_vector(key, key_len, 4, addr, len, &buf[pos]);
      pos += 20;
    } else {
      hmac_sha1_vector(key, key_len, 4, addr, len, hash);
      memcpy(&buf[pos], hash, plen);
      break;
    }
    counter++;
  }
}


x86 assembly (clang, -O0):
<sha1_prf>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movb   $0x0,-0x31(%rbp)
movb   $0x0,-0x32(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0x31(%rbp),%rax
mov    %rax,-0x88(%rbp)
movq   $0x1,-0xa8(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x32(%rbp),%rax
mov    %rax,-0x78(%rbp)
movq   $0x1,-0x98(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    0x10(%rbp),%rax
jae    126f <sha1_prf+0x13f>
mov    0x10(%rbp),%rax
sub    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x14,-0x48(%rbp)
jb     1224 <sha1_prf+0xf4>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x90(%rbp),%rcx
lea    -0xb0(%rbp),%r8
mov    -0x30(%rbp),%r9
add    -0x40(%rbp),%r9
mov    $0x4,%edx
call   1040 <hmac_sha1_vector@plt>
mov    -0x40(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x40(%rbp)
jmp    1262 <sha1_prf+0x132>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x90(%rbp),%rcx
lea    -0xb0(%rbp),%r8
lea    -0x60(%rbp),%r9
mov    $0x4,%edx
call   1040 <hmac_sha1_vector@plt>
mov    -0x30(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1050 <memcpy@plt>
jmp    126f <sha1_prf+0x13f>
mov    -0x32(%rbp),%al
add    $0x1,%al
mov    %al,-0x32(%rbp)
jmp    11c6 <sha1_prf+0x96>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sha1_prf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %r9,%r14
mov    %r8,%r13
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
movb   $0x0,0xf(%rsp)
movb   $0x0,0xe(%rsp)
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %rbx,0x30(%rsp)
mov    %rax,0x10(%rsp)
lea    0xf(%rsp),%rax
mov    %rax,0x38(%rsp)
movq   $0x1,0x18(%rsp)
mov    %rbp,0x40(%rsp)
mov    %r13,0x20(%rsp)
lea    0xe(%rsp),%rax
mov    %rax,0x48(%rsp)
movq   $0x1,0x28(%rsp)
cmpq   $0x0,0xa0(%rsp)
je     122e <sha1_prf+0xfe>
xor    %r13d,%r13d
lea    0x10(%rsp),%rbp
mov    0xa0(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
cmp    $0x14,%rbx
jb     11fb <sha1_prf+0xcb>
lea    (%r14,%r13,1),%r9
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
lea    0x30(%rsp),%rcx
mov    %rbp,%r8
call   1040 <hmac_sha1_vector@plt>
addb   $0x1,0xe(%rsp)
add    $0x14,%r13
add    $0xffffffffffffffec,%rbx
cmp    0xa0(%rsp),%r13
jb     11c0 <sha1_prf+0x90>
jmp    122e <sha1_prf+0xfe>
lea    0x30(%rsp),%rcx
lea    0x10(%rsp),%r8
lea    0x50(%rsp),%rbp
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
mov    %rbp,%r9
call   1040 <hmac_sha1_vector@plt>
add    %r13,%r14
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @sha1_prf(i8* %0, i64 %1, i8* %2, i8* %3, i64 %4, i8* %5, i64 %6) {
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i8
%16 = alloca i8
%17 = alloca i64
%18 = alloca i64
%19 = alloca [20 x i8]
%20 = alloca i64
%21 = alloca [4 x i8*]
%22 = alloca [4 x i64]
store i8* %0, i8** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i64 %4, i64* %12
store i8* %5, i8** %13
store i64 %6, i64* %14
store i8 0, i8* %15
store i8 0, i8* %16
%23 = load i8*, i8** %10
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %20
%25 = load i8*, i8** %10
%26 = getelementptr inbounds [4 x i8*], [4 x i8*]* %21, i64 0, i64 0
store i8* %25, i8** %26
%27 = load i64, i64* %20
%28 = getelementptr inbounds [4 x i64], [4 x i64]* %22, i64 0, i64 0
store i64 %27, i64* %28
%29 = getelementptr inbounds [4 x i8*], [4 x i8*]* %21, i64 0, i64 1
store i8* %15, i8** %29
%30 = getelementptr inbounds [4 x i64], [4 x i64]* %22, i64 0, i64 1
store i64 1, i64* %30
%31 = load i8*, i8** %11
%32 = getelementptr inbounds [4 x i8*], [4 x i8*]* %21, i64 0, i64 2
store i8* %31, i8** %32
%33 = load i64, i64* %12
%34 = getelementptr inbounds [4 x i64], [4 x i64]* %22, i64 0, i64 2
store i64 %33, i64* %34
%35 = getelementptr inbounds [4 x i8*], [4 x i8*]* %21, i64 0, i64 3
store i8* %16, i8** %35
%36 = getelementptr inbounds [4 x i64], [4 x i64]* %22, i64 0, i64 3
store i64 1, i64* %36
store i64 0, i64* %17
br label %37
37: ; preds = %71, %7
%38 = load i64, i64* %17
%39 = load i64, i64* %14
%40 = icmp ult i64 %38, %39
br i1 %40, label %41, label %74
41: ; preds = %37
%42 = load i64, i64* %14
%43 = load i64, i64* %17
%44 = sub i64 %42, %43
store i64 %44, i64* %18
%45 = load i64, i64* %18
%46 = icmp uge i64 %45, 20
br i1 %46, label %47, label %58
47: ; preds = %41
%48 = load i8*, i8** %8
%49 = load i64, i64* %9
%50 = getelementptr inbounds [4 x i8*], [4 x i8*]* %21, i64 0, i64 0
%51 = getelementptr inbounds [4 x i64], [4 x i64]* %22, i64 0, i64 0
%52 = load i8*, i8** %13
%53 = load i64, i64* %17
%54 = getelementptr inbounds i8, i8* %52, i64 %53
%55 = call i32 @hmac_sha1_vector(i8* %48, i64 %49, i32 4, i8** %50, i64* %51, i8* %54)
%56 = load i64, i64* %17
%57 = add i64 %56, 20
store i64 %57, i64* %17
br label %71
58: ; preds = %41
%59 = load i8*, i8** %8
%60 = load i64, i64* %9
%61 = getelementptr inbounds [4 x i8*], [4 x i8*]* %21, i64 0, i64 0
%62 = getelementptr inbounds [4 x i64], [4 x i64]* %22, i64 0, i64 0
%63 = getelementptr inbounds [20 x i8], [20 x i8]* %19, i64 0, i64 0
%64 = call i32 @hmac_sha1_vector(i8* %59, i64 %60, i32 4, i8** %61, i64* %62, i8* %63)
%65 = load i8*, i8** %13
%66 = load i64, i64* %17
%67 = getelementptr inbounds i8, i8* %65, i64 %66
%68 = getelementptr inbounds [20 x i8], [20 x i8]* %19, i64 0, i64 0
%69 = load i64, i64* %18
%70 = call i32 @memcpy(i8* %67, i8* %68, i64 %69)
br label %74
71: ; preds = %47
%72 = load i8, i8* %16
%73 = add i8 %72, 1
store i8 %73, i8* %16
br label %37
74: ; preds = %58, %37
ret void
}

LLVM IR (-O2):
define void @sha1_prf(i8* %0, i64 %1, i8* %2, i8* %3, i64 %4, i8* %5, i64 %6) {
%8 = alloca i8
%9 = alloca i8
%10 = alloca [20 x i8]
%11 = alloca [4 x i8*]
%12 = alloca [4 x i64]
store i8 0, i8* %8
store i8 0, i8* %9
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %10, i64 0, i64 0
%14 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%15 = bitcast [4 x i8*]* %11 to i8*
%16 = bitcast [4 x i64]* %12 to i8*
%17 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 0
store i8* %2, i8** %17
%18 = getelementptr inbounds [4 x i64], [4 x i64]* %12, i64 0, i64 0
store i64 %14, i64* %18
%19 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 1
store i8* %8, i8** %19
%20 = getelementptr inbounds [4 x i64], [4 x i64]* %12, i64 0, i64 1
store i64 1, i64* %20
%21 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 2
store i8* %3, i8** %21
%22 = getelementptr inbounds [4 x i64], [4 x i64]* %12, i64 0, i64 2
store i64 %4, i64* %22
%23 = getelementptr inbounds [4 x i8*], [4 x i8*]* %11, i64 0, i64 3
store i8* %9, i8** %23
%24 = getelementptr inbounds [4 x i64], [4 x i64]* %12, i64 0, i64 3
store i64 1, i64* %24
%25 = icmp eq i64 %6, 0
br i1 %25, label %41, label %26
26: ; preds = %7, %30
%27 = phi i64 [ %33, %30 ], [ 0, %7 ]
%28 = sub nuw i64 %6, %27
%29 = icmp ugt i64 %28, 19
br i1 %29, label %30, label %37
30: ; preds = %26
%31 = getelementptr inbounds i8, i8* %5, i64 %27
%32 = call i32 @hmac_sha1_vector(i8* %0, i64 %1, i32 4, i8** %17, i64* %18, i8* %31)
%33 = add i64 %27, 20
%34 = load i8, i8* %9
%35 = add i8 %34, 1
store i8 %35, i8* %9
%36 = icmp ult i64 %33, %6
br i1 %36, label %26, label %41
37: ; preds = %26
%38 = call i32 @hmac_sha1_vector(i8* %0, i64 %1, i32 4, i8** %17, i64* %18, i8* %13)
%39 = getelementptr inbounds i8, i8* %5, i64 %27
%40 = call i32 @memcpy(i8* %39, i8* %13, i64 %28)
br label %41
41: ; preds = %30, %7, %37
ret void
}