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

/* Variables and functions */
int EVP_PKEY_CTX_add1_hkdf_info(int /*<<< orphan*/ *, void const *, int);
int /*<<< orphan*/ EVP_PKEY_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_new_id(int /*<<< orphan*/,
                                        int /*<<< orphan*/ *);
int EVP_PKEY_CTX_set1_hkdf_key(int /*<<< orphan*/ *, void const *, int);
int EVP_PKEY_CTX_set1_hkdf_salt(int /*<<< orphan*/ *, void const *, int);
int EVP_PKEY_CTX_set_hkdf_md(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ EVP_PKEY_HKDF;
int EVP_PKEY_derive(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t *);
int EVP_PKEY_derive_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_sha256();
size_t INT_MAX;
int ece_hkdf_sha256(const void *salt, size_t saltLen, const void *ikm,
                    size_t ikmLen, const void *info, size_t infoLen,
                    uint8_t *output, size_t outputLen) {
  int err = 0;
  EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
  if (!ctx) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_derive_init(ctx) != 1) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) != 1) {
    err = -12;
    goto end;
  }
  if (saltLen > INT_MAX ||
      EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, (int)saltLen) != 1) {
    err = -12;
    goto end;
  }
  if (ikmLen > INT_MAX ||
      EVP_PKEY_CTX_set1_hkdf_key(ctx, ikm, (int)ikmLen) != 1) {
    err = -12;
    goto end;
  }
  if (infoLen > INT_MAX ||
      EVP_PKEY_CTX_add1_hkdf_info(ctx, info, (int)infoLen) != 1) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_derive(ctx, output, &outputLen) != 1) {
    err = -12;
    goto end;
  }
end:
  EVP_PKEY_CTX_free(ctx);
  return err;
}


x86 assembly (clang, -O0):
<ece_hkdf_sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x18(%rbp),%rax
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)
movl   $0x0,-0x34(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <EVP_PKEY_CTX_new_id@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11ec <ece_hkdf_sha256+0x5c>
movl   $0xfffffff4,-0x34(%rbp)
jmp    1318 <ece_hkdf_sha256+0x188>
mov    -0x40(%rbp),%rdi
call   1060 <EVP_PKEY_derive_init@plt>
cmp    $0x1,%eax
je     120a <ece_hkdf_sha256+0x7a>
movl   $0xfffffff4,-0x34(%rbp)
jmp    1318 <ece_hkdf_sha256+0x188>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   10a0 <EVP_sha256@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
call   1070 <EVP_PKEY_CTX_set_hkdf_md@plt>
cmp    $0x1,%eax
je     1239 <ece_hkdf_sha256+0xa9>
movl   $0xfffffff4,-0x34(%rbp)
jmp    1318 <ece_hkdf_sha256+0x188>
mov    -0x10(%rbp),%rax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
ja     1269 <ece_hkdf_sha256+0xd9>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
call   1050 <EVP_PKEY_CTX_set1_hkdf_salt@plt>
cmp    $0x1,%eax
je     1275 <ece_hkdf_sha256+0xe5>
movl   $0xfffffff4,-0x34(%rbp)
jmp    1318 <ece_hkdf_sha256+0x188>
mov    -0x20(%rbp),%rax
mov    0x2d68(%rip),%rcx
cmp    (%rcx),%rax
ja     12a5 <ece_hkdf_sha256+0x115>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1040 <EVP_PKEY_CTX_set1_hkdf_key@plt>
cmp    $0x1,%eax
je     12b1 <ece_hkdf_sha256+0x121>
movl   $0xfffffff4,-0x34(%rbp)
jmp    1318 <ece_hkdf_sha256+0x188>
mov    -0x30(%rbp),%rax
mov    0x2d2c(%rip),%rcx
cmp    (%rcx),%rax
ja     12e1 <ece_hkdf_sha256+0x151>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
call   1030 <EVP_PKEY_CTX_add1_hkdf_info@plt>
cmp    $0x1,%eax
je     12ed <ece_hkdf_sha256+0x15d>
movl   $0xfffffff4,-0x34(%rbp)
jmp    1318 <ece_hkdf_sha256+0x188>
mov    -0x40(%rbp),%rdi
mov    0x10(%rbp),%rsi
lea    0x18(%rbp),%rdx
call   1090 <EVP_PKEY_derive@plt>
cmp    $0x1,%eax
je     1313 <ece_hkdf_sha256+0x183>
movl   $0xfffffff4,-0x34(%rbp)
jmp    1318 <ece_hkdf_sha256+0x188>
jmp    1318 <ece_hkdf_sha256+0x188>
mov    -0x40(%rbp),%rdi
call   10b0 <EVP_PKEY_CTX_free@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece_hkdf_sha256>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,0x8(%rsp)
mov    %rcx,%r12
mov    %rdx,%r13
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1080 <EVP_PKEY_CTX_new_id@plt>
mov    %rax,%rbx
mov    $0xfffffff4,%ebp
test   %rax,%rax
je     1275 <ece_hkdf_sha256+0xe5>
mov    %rbx,%rdi
call   1060 <EVP_PKEY_derive_init@plt>
cmp    $0x1,%eax
jne    1275 <ece_hkdf_sha256+0xe5>
xor    %eax,%eax
call   10a0 <EVP_sha256@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <EVP_PKEY_CTX_set_hkdf_md@plt>
cmp    $0x1,%eax
jne    1275 <ece_hkdf_sha256+0xe5>
mov    0x2de5(%rip),%rax
cmp    %r15,(%rax)
jb     1275 <ece_hkdf_sha256+0xe5>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <EVP_PKEY_CTX_set1_hkdf_salt@plt>
cmp    $0x1,%eax
jne    1275 <ece_hkdf_sha256+0xe5>
mov    0x2dc6(%rip),%r14
cmp    %r12,(%r14)
jb     1275 <ece_hkdf_sha256+0xe5>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1040 <EVP_PKEY_CTX_set1_hkdf_key@plt>
cmp    $0x1,%eax
jne    1275 <ece_hkdf_sha256+0xe5>
mov    0x10(%rsp),%rdx
cmp    %rdx,(%r14)
jb     1275 <ece_hkdf_sha256+0xe5>
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
call   1030 <EVP_PKEY_CTX_add1_hkdf_info@plt>
cmp    $0x1,%eax
jne    1275 <ece_hkdf_sha256+0xe5>
mov    0x50(%rsp),%rsi
lea    0x58(%rsp),%rdx
mov    %rbx,%rdi
call   1090 <EVP_PKEY_derive@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
mov    $0xfffffff4,%ebp
cmove  %ecx,%ebp
mov    %rbx,%rdi
call   10b0 <EVP_PKEY_CTX_free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EVP_PKEY_HKDF = global i32 0
@INT_MAX = global i64 0
define i32 @ece_hkdf_sha256(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4, i64 %5, i32* %6, i64 %7) {
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32*
store i8* %0, i8** %9
store i64 %1, i64* %10
store i8* %2, i8** %11
store i64 %3, i64* %12
store i8* %4, i8** %13
store i64 %5, i64* %14
store i32* %6, i32** %15
store i64 %7, i64* %16
store i32 0, i32* %17
%19 = load i32, i32* @EVP_PKEY_HKDF
%20 = call i32* @EVP_PKEY_CTX_new_id(i32 %19, i32* null)
store i32* %20, i32** %18
%21 = load i32*, i32** %18
%22 = icmp ne i32* %21, null
br i1 %22, label %24, label %23
23: ; preds = %8
store i32 -12, i32* %17
br label %78
24: ; preds = %8
%25 = load i32*, i32** %18
%26 = call i32 @EVP_PKEY_derive_init(i32* %25)
%27 = icmp ne i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -12, i32* %17
br label %78
29: ; preds = %24
%30 = load i32*, i32** %18
%31 = call i32 (...) @EVP_sha256()
%32 = call i32 @EVP_PKEY_CTX_set_hkdf_md(i32* %30, i32 %31)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -12, i32* %17
br label %78
35: ; preds = %29
%36 = load i64, i64* %10
%37 = load i64, i64* @INT_MAX
%38 = icmp ugt i64 %36, %37
br i1 %38, label %46, label %39
39: ; preds = %35
%40 = load i32*, i32** %18
%41 = load i8*, i8** %9
%42 = load i64, i64* %10
%43 = trunc i64 %42 to i32
%44 = call i32 @EVP_PKEY_CTX_set1_hkdf_salt(i32* %40, i8* %41, i32 %43)
%45 = icmp ne i32 %44, 1
br i1 %45, label %46, label %47
46: ; preds = %39, %35
store i32 -12, i32* %17
br label %78
47: ; preds = %39
%48 = load i64, i64* %12
%49 = load i64, i64* @INT_MAX
%50 = icmp ugt i64 %48, %49
br i1 %50, label %58, label %51
51: ; preds = %47
%52 = load i32*, i32** %18
%53 = load i8*, i8** %11
%54 = load i64, i64* %12
%55 = trunc i64 %54 to i32
%56 = call i32 @EVP_PKEY_CTX_set1_hkdf_key(i32* %52, i8* %53, i32 %55)
%57 = icmp ne i32 %56, 1
br i1 %57, label %58, label %59
58: ; preds = %51, %47
store i32 -12, i32* %17
br label %78
59: ; preds = %51
%60 = load i64, i64* %14
%61 = load i64, i64* @INT_MAX
%62 = icmp ugt i64 %60, %61
br i1 %62, label %70, label %63
63: ; preds = %59
%64 = load i32*, i32** %18
%65 = load i8*, i8** %13
%66 = load i64, i64* %14
%67 = trunc i64 %66 to i32
%68 = call i32 @EVP_PKEY_CTX_add1_hkdf_info(i32* %64, i8* %65, i32 %67)
%69 = icmp ne i32 %68, 1
br i1 %69, label %70, label %71
70: ; preds = %63, %59
store i32 -12, i32* %17
br label %78
71: ; preds = %63
%72 = load i32*, i32** %18
%73 = load i32*, i32** %15
%74 = call i32 @EVP_PKEY_derive(i32* %72, i32* %73, i64* %16)
%75 = icmp ne i32 %74, 1
br i1 %75, label %76, label %77
76: ; preds = %71
store i32 -12, i32* %17
br label %78
77: ; preds = %71
br label %78
78: ; preds = %77, %76, %70, %58, %46, %34, %28, %23
%79 = load i32*, i32** %18
%80 = call i32 @EVP_PKEY_CTX_free(i32* %79)
%81 = load i32, i32* %17
ret i32 %81
}

LLVM IR (-O2):
@EVP_PKEY_HKDF = global i32 0
@INT_MAX = global i64 0
define i32 @ece_hkdf_sha256(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4, i64 %5, i32* %6, i64 %7) {
%9 = alloca i64
store i64 %7, i64* %9
%10 = load i32, i32* @EVP_PKEY_HKDF
%11 = tail call i32* @EVP_PKEY_CTX_new_id(i32 %10, i32* null)
%12 = icmp eq i32* %11, null
br i1 %12, label %45, label %13
13: ; preds = %8
%14 = tail call i32 @EVP_PKEY_derive_init(i32* %11)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %45
16: ; preds = %13
%17 = tail call i32 (...) @EVP_sha256()
%18 = tail call i32 @EVP_PKEY_CTX_set_hkdf_md(i32* %11, i32 %17)
%19 = icmp ne i32 %18, 1
%20 = load i64, i64* @INT_MAX
%21 = icmp ult i64 %20, %1
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %45, label %23
23: ; preds = %16
%24 = trunc i64 %1 to i32
%25 = tail call i32 @EVP_PKEY_CTX_set1_hkdf_salt(i32* %11, i8* %0, i32 %24)
%26 = icmp ne i32 %25, 1
%27 = load i64, i64* @INT_MAX
%28 = icmp ult i64 %27, %3
%29 = select i1 %26, i1 true, i1 %28
br i1 %29, label %45, label %30
30: ; preds = %23
%31 = trunc i64 %3 to i32
%32 = tail call i32 @EVP_PKEY_CTX_set1_hkdf_key(i32* %11, i8* %2, i32 %31)
%33 = icmp ne i32 %32, 1
%34 = load i64, i64* @INT_MAX
%35 = icmp ult i64 %34, %5
%36 = select i1 %33, i1 true, i1 %35
br i1 %36, label %45, label %37
37: ; preds = %30
%38 = trunc i64 %5 to i32
%39 = tail call i32 @EVP_PKEY_CTX_add1_hkdf_info(i32* %11, i8* %4, i32 %38)
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = call i32 @EVP_PKEY_derive(i32* %11, i32* %6, i64* %9)
%43 = icmp eq i32 %42, 1
%44 = select i1 %43, i32 0, i32 -12
br label %45
45: ; preds = %41, %37, %30, %23, %16, %13, %8
%46 = phi i32 [ -12, %8 ], [ -12, %13 ], [ -12, %16 ], [ -12, %23 ], [ -12, %30 ], [ -12, %37 ], [ %44, %41 ]
%47 = call i32 @EVP_PKEY_CTX_free(i32* %11)
ret i32 %46
}