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

/* Variables and functions */
int ece_read_uint32_be(size_t const *);
int ece_aes128gcm_payload_extract_params(
    const uint8_t *payload, size_t payloadLen, const uint8_t **salt,
    size_t *saltLen, const uint8_t **keyId, size_t *keyIdLen, uint32_t *rs,
    const uint8_t **ciphertext, size_t *ciphertextLen) {
  if (payloadLen < 21) {
    return -10;
  }
  *salt = payload;
  *saltLen = 16;
  *keyIdLen = payload[16 + 4];
  if (payloadLen < 21 + *keyIdLen) {
    return -10;
  }
  *keyId = &payload[21];
  *rs = ece_read_uint32_be(&payload[16]);
  if (*rs < 18) {
    return -15;
  }
  size_t payloadStart = 21 + *keyIdLen;
  *ciphertext = &payload[payloadStart];
  *ciphertextLen = payloadLen - payloadStart;
  return 0;
}


x86 assembly (clang, -O0):
<ece_aes128gcm_payload_extract_params>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
cmpq   $0x15,-0x18(%rbp)
jae    1153 <ece_aes128gcm_payload_extract_params+0x43>
movl   $0xfffffff6,-0x4(%rbp)
jmp    121d <ece_aes128gcm_payload_extract_params+0x10d>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
movq   $0x10,(%rax)
mov    -0x10(%rbp),%rax
mov    0xa0(%rax),%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rcx),%rcx
add    $0x15,%rcx
cmp    %rcx,%rax
jae    119f <ece_aes128gcm_payload_extract_params+0x8f>
movl   $0xfffffff6,-0x4(%rbp)
jmp    121d <ece_aes128gcm_payload_extract_params+0x10d>
mov    -0x10(%rbp),%rcx
add    $0xa8,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
add    $0x80,%rdi
call   1030 <ece_read_uint32_be@plt>
mov    %eax,%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    0x10(%rbp),%rax
cmpl   $0x12,(%rax)
jge    11e2 <ece_aes128gcm_payload_extract_params+0xd2>
movl   $0xfffffff1,-0x4(%rbp)
jmp    121d <ece_aes128gcm_payload_extract_params+0x10d>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
add    $0x15,%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x40(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rcx
mov    0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
sub    -0x40(%rbp),%rcx
mov    0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ece_aes128gcm_payload_extract_params>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0xfffffff6,%eax
cmp    $0x15,%rsi
jb     119b <ece_aes128gcm_payload_extract_params+0x8b>
mov    %r9,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rdi,(%rdx)
movq   $0x10,(%rcx)
mov    0xa0(%rdi),%rcx
mov    %rcx,(%r9)
add    $0x15,%rcx
cmp    %rsi,%rcx
ja     119b <ece_aes128gcm_payload_extract_params+0x8b>
mov    0x30(%rsp),%r12
lea    0xa8(%rbx),%rax
mov    %rax,(%r8)
lea    0x80(%rbx),%rdi
call   1030 <ece_read_uint32_be@plt>
mov    %eax,%ecx
mov    %eax,(%r12)
mov    $0xfffffff1,%eax
cmp    $0x12,%ecx
jl     119b <ece_aes128gcm_payload_extract_params+0x8b>
mov    0x40(%rsp),%rax
mov    0x38(%rsp),%rcx
mov    (%r14),%rdx
lea    (%rbx,%rdx,8),%rsi
add    $0xa8,%rsi
add    $0x15,%rdx
mov    %rsi,(%rcx)
sub    %rdx,%r15
mov    %r15,(%rax)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @ece_aes128gcm_payload_extract_params(i64* %0, i64 %1, i64** nocapture writeonly %2, i64* nocapture writeonly %3, i64** nocapture writeonly %4, i64* nocapture %5, i32* nocapture writeonly %6, i64** nocapture writeonly %7, i64* nocapture writeonly %8) {
%10 = icmp ult i64 %1, 21
br i1 %10, label %26, label %11
11: ; preds = %9
store i64* %0, i64** %2
store i64 16, i64* %3
%12 = getelementptr inbounds i64, i64* %0, i64 20
%13 = load i64, i64* %12
store i64 %13, i64* %5
%14 = add i64 %13, 21
%15 = icmp ugt i64 %14, %1
br i1 %15, label %26, label %16
16: ; preds = %11
%17 = getelementptr inbounds i64, i64* %0, i64 21
store i64* %17, i64** %4
%18 = getelementptr inbounds i64, i64* %0, i64 16
%19 = tail call i32 @ece_read_uint32_be(i64* %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 18
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = load i64, i64* %5
%23 = add i64 %22, 21
%24 = getelementptr inbounds i64, i64* %0, i64 %23
store i64* %24, i64** %7
%25 = sub i64 %1, %23
store i64 %25, i64* %8
br label %26
26: ; preds = %16, %11, %9, %21
%27 = phi i32 [ 0, %21 ], [ -10, %9 ], [ -10, %11 ], [ -15, %16 ]
ret i32 %27
}