#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;
  }
  *saltLen = 16;
  *keyIdLen = payload[16 + 4];
  if (payloadLen < 21 + *keyIdLen) {
    return -10;
  }
  *rs = ece_read_uint32_be(&payload[16]);
  if (*rs < 18) {
    return -15;
  }
  size_t payloadStart = 21 + *keyIdLen;
  *ciphertextLen = payloadLen - payloadStart;
  if (!(*ciphertextLen)) {
    return -11;
  }
  *salt = payload;
  if (keyIdLen) {
    *keyId = &payload[21];
  } else {
    *keyId = NULL;
  }
  *ciphertext = &payload[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    1252 <ece_aes128gcm_payload_extract_params+0x142>
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    1194 <ece_aes128gcm_payload_extract_params+0x84>
movl   $0xfffffff6,-0x4(%rbp)
jmp    1252 <ece_aes128gcm_payload_extract_params+0x142>
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    11c5 <ece_aes128gcm_payload_extract_params+0xb5>
movl   $0xfffffff1,-0x4(%rbp)
jmp    1252 <ece_aes128gcm_payload_extract_params+0x142>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
add    $0x15,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rcx
sub    -0x40(%rbp),%rcx
mov    0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11fd <ece_aes128gcm_payload_extract_params+0xed>
movl   $0xfffffff5,-0x4(%rbp)
jmp    1252 <ece_aes128gcm_payload_extract_params+0x142>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x38(%rbp)
je     122a <ece_aes128gcm_payload_extract_params+0x11a>
mov    -0x10(%rbp),%rcx
add    $0xa8,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
jmp    1235 <ece_aes128gcm_payload_extract_params+0x125>
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rcx
mov    -0x40(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rcx
mov    0x18(%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   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xfffffff6,%eax
cmp    $0x15,%rsi
jb     11ac <ece_aes128gcm_payload_extract_params+0x9c>
mov    %r9,%r13
mov    %rsi,%rbx
mov    %rdi,%r12
movq   $0x10,(%rcx)
mov    0xa0(%rdi),%rcx
mov    %rcx,(%r9)
add    $0x15,%rcx
cmp    %rsi,%rcx
ja     11ac <ece_aes128gcm_payload_extract_params+0x9c>
mov    %r8,%r14
mov    %rdx,%r15
mov    0x40(%rsp),%rbp
lea    0x80(%r12),%rdi
call   1030 <ece_read_uint32_be@plt>
mov    %eax,%ecx
mov    %eax,0x0(%rbp)
mov    $0xfffffff1,%eax
cmp    $0x12,%ecx
jl     11ac <ece_aes128gcm_payload_extract_params+0x9c>
mov    0x50(%rsp),%rcx
mov    0x0(%r13),%rax
add    $0x15,%rax
sub    %rax,%rbx
mov    %rbx,(%rcx)
je     11a7 <ece_aes128gcm_payload_extract_params+0x97>
mov    0x48(%rsp),%rcx
mov    %r12,(%r15)
lea    0xa8(%r12),%rdx
mov    %rdx,(%r14)
lea    (%r12,%rax,8),%rax
mov    %rax,(%rcx)
xor    %eax,%eax
jmp    11ac <ece_aes128gcm_payload_extract_params+0x9c>
mov    $0xfffffff5,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
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 %73
24: ; preds = %9
%25 = load i64*, i64** %14
store i64 16, i64* %25
%26 = load i64*, i64** %11
%27 = getelementptr inbounds i64, i64* %26, i64 20
%28 = load i64, i64* %27
%29 = load i64*, i64** %16
store i64 %28, i64* %29
%30 = load i64, i64* %12
%31 = load i64*, i64** %16
%32 = load i64, i64* %31
%33 = add i64 21, %32
%34 = icmp ult i64 %30, %33
br i1 %34, label %35, label %36
35: ; preds = %24
store i32 -10, i32* %10
br label %73
36: ; preds = %24
%37 = load i64*, i64** %11
%38 = getelementptr inbounds i64, i64* %37, i64 16
%39 = call i32 @ece_read_uint32_be(i64* %38)
%40 = load i32*, i32** %17
store i32 %39, i32* %40
%41 = load i32*, i32** %17
%42 = load i32, i32* %41
%43 = icmp slt i32 %42, 18
br i1 %43, label %44, label %45
44: ; preds = %36
store i32 -15, i32* %10
br label %73
45: ; preds = %36
%46 = load i64*, i64** %16
%47 = load i64, i64* %46
%48 = add i64 21, %47
store i64 %48, i64* %20
%49 = load i64, i64* %12
%50 = load i64, i64* %20
%51 = sub i64 %49, %50
%52 = load i64*, i64** %19
store i64 %51, i64* %52
%53 = load i64*, i64** %19
%54 = load i64, i64* %53
%55 = icmp ne i64 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %45
store i32 -11, i32* %10
br label %73
57: ; preds = %45
%58 = load i64*, i64** %11
%59 = load i64**, i64*** %13
store i64* %58, i64** %59
%60 = load i64*, i64** %16
%61 = icmp ne i64* %60, null
br i1 %61, label %62, label %66
62: ; preds = %57
%63 = load i64*, i64** %11
%64 = getelementptr inbounds i64, i64* %63, i64 21
%65 = load i64**, i64*** %15
store i64* %64, i64** %65
br label %68
66: ; preds = %57
%67 = load i64**, i64*** %15
store i64* null, i64** %67
br label %68
68: ; preds = %66, %62
%69 = load i64*, i64** %11
%70 = load i64, i64* %20
%71 = getelementptr inbounds i64, i64* %69, i64 %70
%72 = load i64**, i64*** %18
store i64* %71, i64** %72
store i32 0, i32* %10
br label %73
73: ; preds = %68, %56, %44, %35, %23
%74 = load i32, i32* %10
ret i32 %74
}

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 %28, label %11
11: ; preds = %9
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 %28, label %16
16: ; preds = %11
%17 = getelementptr inbounds i64, i64* %0, i64 16
%18 = tail call i32 @ece_read_uint32_be(i64* %17)
store i32 %18, i32* %6
%19 = icmp slt i32 %18, 18
br i1 %19, label %28, label %20
20: ; preds = %16
%21 = load i64, i64* %5
%22 = add i64 %21, 21
%23 = sub i64 %1, %22
store i64 %23, i64* %8
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
store i64* %0, i64** %2
%26 = getelementptr inbounds i64, i64* %0, i64 21
store i64* %26, i64** %4
%27 = getelementptr inbounds i64, i64* %0, i64 %22
store i64* %27, i64** %7
br label %28
28: ; preds = %25, %20, %16, %11, %9
%29 = phi i32 [ -10, %9 ], [ -10, %11 ], [ -15, %16 ], [ 0, %25 ], [ -11, %20 ]
ret i32 %29
}