#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 */
scalar_t__ EVP_PKEY_CTX_add1_hkdf_info(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ const *, int);
int /*<<< orphan*/ EVP_PKEY_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_PKEY_CTX_new_id(int /*<<< orphan*/,
                                        int /*<<< orphan*/ *);
scalar_t__ EVP_PKEY_CTX_set1_hkdf_key(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ const *, int);
scalar_t__ EVP_PKEY_CTX_set1_hkdf_salt(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ const *, int);
scalar_t__ EVP_PKEY_CTX_set_hkdf_md(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ EVP_PKEY_HKDF;
scalar_t__ EVP_PKEY_derive(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                           size_t *);
scalar_t__ EVP_PKEY_derive_init(int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_sha256();
int ece_hkdf_sha256(const uint8_t *salt, size_t saltLen, const uint8_t *ikm,
                    size_t ikmLen, const uint8_t *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) <= 0) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) <= 0) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, (int)saltLen) <= 0) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_CTX_set1_hkdf_key(ctx, ikm, (int)ikmLen) <= 0) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_CTX_add1_hkdf_info(ctx, info, (int)infoLen) <= 0) {
    err = -12;
    goto end;
  }
  if (EVP_PKEY_derive(ctx, output, &outputLen) <= 0) {
    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    0x2e1a(%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    12e2 <ece_hkdf_sha256+0x152>
mov    -0x40(%rbp),%rdi
call   1060 <EVP_PKEY_derive_init@plt>
cmp    $0x0,%rax
jg     120b <ece_hkdf_sha256+0x7b>
movl   $0xfffffff4,-0x34(%rbp)
jmp    12e2 <ece_hkdf_sha256+0x152>
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    $0x0,%rax
jg     123b <ece_hkdf_sha256+0xab>
movl   $0xfffffff4,-0x34(%rbp)
jmp    12e2 <ece_hkdf_sha256+0x152>
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    $0x0,%rax
jg     1264 <ece_hkdf_sha256+0xd4>
movl   $0xfffffff4,-0x34(%rbp)
jmp    12e2 <ece_hkdf_sha256+0x152>
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    $0x0,%rax
jg     128d <ece_hkdf_sha256+0xfd>
movl   $0xfffffff4,-0x34(%rbp)
jmp    12e2 <ece_hkdf_sha256+0x152>
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    $0x0,%rax
jg     12b6 <ece_hkdf_sha256+0x126>
movl   $0xfffffff4,-0x34(%rbp)
jmp    12e2 <ece_hkdf_sha256+0x152>
mov    -0x40(%rbp),%rdi
mov    0x10(%rbp),%rsi
lea    0x18(%rbp),%rdx
call   1090 <EVP_PKEY_derive@plt>
cmp    $0x0,%rax
jg     12dd <ece_hkdf_sha256+0x14d>
movl   $0xfffffff4,-0x34(%rbp)
jmp    12e2 <ece_hkdf_sha256+0x152>
jmp    12e2 <ece_hkdf_sha256+0x152>
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,%r14
mov    %rdi,%r15
mov    0x2e25(%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     1250 <ece_hkdf_sha256+0xc0>
mov    %rbx,%rdi
call   1060 <EVP_PKEY_derive_init@plt>
test   %rax,%rax
jle    1250 <ece_hkdf_sha256+0xc0>
xor    %eax,%eax
call   10a0 <EVP_sha256@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <EVP_PKEY_CTX_set_hkdf_md@plt>
test   %rax,%rax
jle    1250 <ece_hkdf_sha256+0xc0>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <EVP_PKEY_CTX_set1_hkdf_salt@plt>
test   %rax,%rax
jle    1250 <ece_hkdf_sha256+0xc0>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1040 <EVP_PKEY_CTX_set1_hkdf_key@plt>
test   %rax,%rax
jle    1250 <ece_hkdf_sha256+0xc0>
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
call   1030 <EVP_PKEY_CTX_add1_hkdf_info@plt>
test   %rax,%rax
jle    1250 <ece_hkdf_sha256+0xc0>
mov    0x50(%rsp),%rsi
lea    0x58(%rsp),%rdx
mov    %rbx,%rdi
call   1090 <EVP_PKEY_derive@plt>
xor    %ecx,%ecx
test   %rax,%rax
mov    $0xfffffff4,%ebp
cmovg  %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
define i32 @ece_hkdf_sha256(i32* %0, i64 %1, i32* %2, i64 %3, i32* %4, i64 %5, i32* %6, i64 %7) {
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i64
%15 = alloca i32*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32*
store i32* %0, i32** %9
store i64 %1, i64* %10
store i32* %2, i32** %11
store i64 %3, i64* %12
store i32* %4, i32** %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 %66
24: ; preds = %8
%25 = load i32*, i32** %18
%26 = call i64 @EVP_PKEY_derive_init(i32* %25)
%27 = icmp sle i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -12, i32* %17
br label %66
29: ; preds = %24
%30 = load i32*, i32** %18
%31 = call i32 (...) @EVP_sha256()
%32 = call i64 @EVP_PKEY_CTX_set_hkdf_md(i32* %30, i32 %31)
%33 = icmp sle i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -12, i32* %17
br label %66
35: ; preds = %29
%36 = load i32*, i32** %18
%37 = load i32*, i32** %9
%38 = load i64, i64* %10
%39 = trunc i64 %38 to i32
%40 = call i64 @EVP_PKEY_CTX_set1_hkdf_salt(i32* %36, i32* %37, i32 %39)
%41 = icmp sle i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %35
store i32 -12, i32* %17
br label %66
43: ; preds = %35
%44 = load i32*, i32** %18
%45 = load i32*, i32** %11
%46 = load i64, i64* %12
%47 = trunc i64 %46 to i32
%48 = call i64 @EVP_PKEY_CTX_set1_hkdf_key(i32* %44, i32* %45, i32 %47)
%49 = icmp sle i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %43
store i32 -12, i32* %17
br label %66
51: ; preds = %43
%52 = load i32*, i32** %18
%53 = load i32*, i32** %13
%54 = load i64, i64* %14
%55 = trunc i64 %54 to i32
%56 = call i64 @EVP_PKEY_CTX_add1_hkdf_info(i32* %52, i32* %53, i32 %55)
%57 = icmp sle i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %51
store i32 -12, i32* %17
br label %66
59: ; preds = %51
%60 = load i32*, i32** %18
%61 = load i32*, i32** %15
%62 = call i64 @EVP_PKEY_derive(i32* %60, i32* %61, i64* %16)
%63 = icmp sle i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %59
store i32 -12, i32* %17
br label %66
65: ; preds = %59
br label %66
66: ; preds = %65, %64, %58, %50, %42, %34, %28, %23
%67 = load i32*, i32** %18
%68 = call i32 @EVP_PKEY_CTX_free(i32* %67)
%69 = load i32, i32* %17
ret i32 %69
}

LLVM IR (-O2):
@EVP_PKEY_HKDF = global i32 0
define i32 @ece_hkdf_sha256(i32* %0, i64 %1, i32* %2, i64 %3, i32* %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 %36, label %13
13: ; preds = %8
%14 = tail call i64 @EVP_PKEY_derive_init(i32* %11)
%15 = icmp slt i64 %14, 1
br i1 %15, label %36, label %16
16: ; preds = %13
%17 = tail call i32 (...) @EVP_sha256()
%18 = tail call i64 @EVP_PKEY_CTX_set_hkdf_md(i32* %11, i32 %17)
%19 = icmp slt i64 %18, 1
br i1 %19, label %36, label %20
20: ; preds = %16
%21 = trunc i64 %1 to i32
%22 = tail call i64 @EVP_PKEY_CTX_set1_hkdf_salt(i32* %11, i32* %0, i32 %21)
%23 = icmp slt i64 %22, 1
br i1 %23, label %36, label %24
24: ; preds = %20
%25 = trunc i64 %3 to i32
%26 = tail call i64 @EVP_PKEY_CTX_set1_hkdf_key(i32* %11, i32* %2, i32 %25)
%27 = icmp slt i64 %26, 1
br i1 %27, label %36, label %28
28: ; preds = %24
%29 = trunc i64 %5 to i32
%30 = tail call i64 @EVP_PKEY_CTX_add1_hkdf_info(i32* %11, i32* %4, i32 %29)
%31 = icmp slt i64 %30, 1
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = call i64 @EVP_PKEY_derive(i32* %11, i32* %6, i64* %9)
%34 = icmp slt i64 %33, 1
%35 = select i1 %34, i32 -12, i32 0
br label %36
36: ; preds = %32, %28, %24, %20, %16, %13, %8
%37 = phi i32 [ -12, %8 ], [ -12, %13 ], [ -12, %16 ], [ -12, %20 ], [ -12, %24 ], [ -12, %28 ], [ %35, %32 ]
%38 = call i32 @EVP_PKEY_CTX_free(i32* %11)
ret i32 %37
}