// File: /workspace/github_c_makefile_star2_O3/OpenKMIP[P]libkmip/src/kmip.c
// Function: kmip_encode_attribute_v1
int
kmip_encode_attribute_v1(KMIP *ctx, const Attribute *value)
{
    /* TODO (ph) Add CryptographicParameters support? */
    CHECK_ENCODE_ARGS(ctx, value);

    int result = 0;
    
    result = kmip_encode_int32_be(ctx, TAG_TYPE(KMIP_TAG_ATTRIBUTE, KMIP_TYPE_STRUCTURE));
    CHECK_RESULT(ctx, result);
    
    uint8 *length_index = ctx->index;
    uint8 *value_index = ctx->index += 4;
    
    result = kmip_encode_attribute_name(ctx, value->type);
    CHECK_RESULT(ctx, result);
    
    if(value->index != KMIP_UNSET)
    {
        result = kmip_encode_integer(ctx, KMIP_TAG_ATTRIBUTE_INDEX, value->index);
        CHECK_RESULT(ctx, result);
    }
    
    uint8 *curr_index = 0;
    uint8 *tag_index = ctx->index;
    enum tag t = KMIP_TAG_ATTRIBUTE_VALUE;
    
    switch(value->type)
    {
        case KMIP_ATTR_APPLICATION_SPECIFIC_INFORMATION:
        {
            result = kmip_encode_application_specific_information(ctx, (ApplicationSpecificInformation*)value->value);
            CHECK_RESULT(ctx, result);

            curr_index = ctx->index;
            ctx->index = tag_index;

            result = kmip_encode_int32_be(ctx, TAG_TYPE(KMIP_TAG_ATTRIBUTE_VALUE, KMIP_TYPE_STRUCTURE));

            ctx->index = curr_index;
        }
        break;

        case KMIP_ATTR_UNIQUE_IDENTIFIER:
        result = kmip_encode_text_string(ctx, t, (TextString*)value->value);
        break;
        
        case KMIP_ATTR_NAME:
        /* TODO (ph) This is messy. Clean it up? */
        result = kmip_encode_name(ctx, (Name*)value->value);
        CHECK_RESULT(ctx, result);
        
        curr_index = ctx->index;
        ctx->index = tag_index;
        
        result = kmip_encode_int32_be(ctx, TAG_TYPE(KMIP_TAG_ATTRIBUTE_VALUE, KMIP_TYPE_STRUCTURE));
        
        ctx->index = curr_index;
        break;
        
        case KMIP_ATTR_OBJECT_TYPE:
        result = kmip_encode_enum(ctx, t, *(int32 *)value->value);
        break;
        
        case KMIP_ATTR_CRYPTOGRAPHIC_ALGORITHM:
        result = kmip_encode_enum(ctx, t, *(int32 *)value->value);
        break;
        
        case KMIP_ATTR_CRYPTOGRAPHIC_LENGTH:
        result = kmip_encode_integer(ctx, t, *(int32 *)value->value);
        break;
        
        case KMIP_ATTR_OPERATION_POLICY_NAME:
        result = kmip_encode_text_string(ctx, t, (TextString*)value->value);
        break;
        
        case KMIP_ATTR_CRYPTOGRAPHIC_USAGE_MASK:
        result = kmip_encode_integer(ctx, t, *(int32 *)value->value);
        break;
        
        case KMIP_ATTR_STATE:
        result = kmip_encode_enum(ctx, t, *(int32 *)value->value);
        break;
        
        case KMIP_ATTR_OBJECT_GROUP:
        {
            result = kmip_encode_text_string(ctx, t, (TextString*)value->value);
        }
        break;

        case KMIP_ATTR_ACTIVATION_DATE:
        case KMIP_ATTR_DEACTIVATION_DATE:
        case KMIP_ATTR_PROCESS_START_DATE:
        case KMIP_ATTR_PROTECT_STOP_DATE:
        {
            result = kmip_encode_date_time(ctx, t, *(int64 *)value->value);
        } break;

        case KMIP_ATTR_CRYPTOGRAPHIC_PARAMETERS:
        {
            result = kmip_encode_cryptographic_parameters(ctx, (CryptographicParameters*)value->value);
            CHECK_RESULT(ctx, result);

            curr_index = ctx->index;
            ctx->index = tag_index;

            result = kmip_encode_int32_be(ctx, TAG_TYPE(KMIP_TAG_ATTRIBUTE_VALUE, KMIP_TYPE_STRUCTURE));

            ctx->index = curr_index;
        } break;

        default:
        kmip_push_error_frame(ctx, __func__, __LINE__);
        return(KMIP_ERROR_ATTR_UNSUPPORTED);
        break;
    };
    CHECK_RESULT(ctx, result);
    
    curr_index = ctx->index;
    ctx->index = length_index;
    
    result = kmip_encode_length(ctx, curr_index - value_index);
    CHECK_RESULT(ctx, result);
    
    ctx->index = curr_index;
    
    return(KMIP_OK);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0xcad4
movq %rsi, %r14
xorl %ebp, %ebp
testq %rsi, %rsi
je 0xcb0d
movq %rdi, %rbx
movl $0x42000801, %esi       # imm = 0x42000801
callq 0x60a0
testl %eax, %eax
je 0xcadb
movl %eax, %ebp
leaq 0x13630(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x1971, %edx           # imm = 0x1971
jmp 0xcb08
movl $0xffffffef, %ebp       # imm = 0xFFFFFFEF
jmp 0xcb0d
movq 0x8(%rbx), %r12
leaq 0x4(%r12), %r15
movq %r15, 0x8(%rbx)
movl (%r14), %esi
movq %rbx, %rdi
callq 0x6870
testl %eax, %eax
je 0xcb1e
movl %eax, %ebp
leaq 0x135fa(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x1977, %edx           # imm = 0x1977
callq 0x6de0
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%r14), %edx
cmpl $-0x1, %edx
je 0xcb4b
movq %rbx, %rdi
movl $0x420009, %esi         # imm = 0x420009
callq 0x63d0
testl %eax, %eax
je 0xcb4b
movl %eax, %ebp
leaq 0x135b9(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x197c, %edx           # imm = 0x197C
jmp 0xcb08
movl (%r14), %eax
cmpq $0xe, %rax
ja 0xcca4
movq 0x8(%rbx), %r13
leaq 0x117e1(%rip), %rcx     # 0x1e344
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%r14), %rax
movq (%rax), %rdx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x6210
jmp 0xcbbd
movq 0x8(%r14), %rax
movl (%rax), %edx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x66d0
jmp 0xcbbd
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x6670
jmp 0xcbbd
movq 0x8(%r14), %rax
movl (%rax), %edx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x63d0
movl %eax, %ebp
testl %ebp, %ebp
je 0xcbd7
leaq 0x13530(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x19dc, %edx           # imm = 0x19DC
jmp 0xcb08
movq 0x8(%rbx), %r14
movq %r12, 0x8(%rbx)
movq %r14, %rsi
subq %r15, %rsi
movq %rbx, %rdi
callq 0x6a80
testl %eax, %eax
je 0xcc07
movl %eax, %ebp
leaq 0x13500(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x19e2, %edx           # imm = 0x19E2
jmp 0xcb08
movq %r14, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0xcb0d
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x6c20
testl %eax, %eax
je 0xcc84
movl %eax, %ebp
leaq 0x134cf(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x19cd, %edx           # imm = 0x19CD
jmp 0xcb08
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x62c0
testl %eax, %eax
je 0xcc84
movl %eax, %ebp
leaq 0x134a9(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x1988, %edx           # imm = 0x1988
jmp 0xcb08
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x6180
testl %eax, %eax
je 0xcc84
movl %eax, %ebp
leaq 0x13483(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x199a, %edx           # imm = 0x199A
jmp 0xcb08
movq 0x8(%rbx), %r14
movq %r13, 0x8(%rbx)
movq %rbx, %rdi
movl $0x42000b01, %esi       # imm = 0x42000B01
callq 0x60a0
movl %eax, %ebp
movq %r14, 0x8(%rbx)
jmp 0xcbbf
leaq 0x1344f(%rip), %rsi     # 0x200fa
movq %rbx, %rdi
movl $0x19d8, %edx           # imm = 0x19D8
callq 0x6de0
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
jmp 0xcb0d