// File: /workspace/github_c_makefile_star2_O1/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 0xcd2c
movq %rsi, %r14
xorl %ebp, %ebp
testq %rsi, %rsi
je 0xcd65
movq %rdi, %rbx
movl $0x42000801, %esi       # imm = 0x42000801
callq 0x60a0
testl %eax, %eax
je 0xcd33
movl %eax, %ebp
leaq 0x14368(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x1971, %edx           # imm = 0x1971
jmp 0xcd60
movl $0xffffffef, %ebp       # imm = 0xFFFFFFEF
jmp 0xcd65
movq 0x8(%rbx), %r12
leaq 0x4(%r12), %r15
movq %r15, 0x8(%rbx)
movl (%r14), %esi
movq %rbx, %rdi
callq 0x6870
testl %eax, %eax
je 0xcd76
movl %eax, %ebp
leaq 0x14332(%rip), %rsi     # 0x2108a
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 0xcda3
movq %rbx, %rdi
movl $0x420009, %esi         # imm = 0x420009
callq 0x63d0
testl %eax, %eax
je 0xcda3
movl %eax, %ebp
leaq 0x142f1(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x197c, %edx           # imm = 0x197C
jmp 0xcd60
movl (%r14), %eax
cmpq $0xe, %rax
ja 0xcefc
movq 0x8(%rbx), %r13
leaq 0x12589(%rip), %rcx     # 0x1f344
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 0xce15
movq 0x8(%r14), %rax
movl (%rax), %edx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x66d0
jmp 0xce15
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x6670
jmp 0xce15
movq 0x8(%r14), %rax
movl (%rax), %edx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x63d0
movl %eax, %ebp
testl %ebp, %ebp
je 0xce2f
leaq 0x14268(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x19dc, %edx           # imm = 0x19DC
jmp 0xcd60
movq 0x8(%rbx), %r14
movq %r12, 0x8(%rbx)
movq %r14, %rsi
subq %r15, %rsi
movq %rbx, %rdi
callq 0x6a80
testl %eax, %eax
je 0xce5f
movl %eax, %ebp
leaq 0x14238(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x19e2, %edx           # imm = 0x19E2
jmp 0xcd60
movq %r14, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0xcd65
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x6c20
testl %eax, %eax
je 0xcedc
movl %eax, %ebp
leaq 0x14207(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x19cd, %edx           # imm = 0x19CD
jmp 0xcd60
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x62c0
testl %eax, %eax
je 0xcedc
movl %eax, %ebp
leaq 0x141e1(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x1988, %edx           # imm = 0x1988
jmp 0xcd60
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x6180
testl %eax, %eax
je 0xcedc
movl %eax, %ebp
leaq 0x141bb(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x199a, %edx           # imm = 0x199A
jmp 0xcd60
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 0xce17
leaq 0x14187(%rip), %rsi     # 0x2108a
movq %rbx, %rdi
movl $0x19d8, %edx           # imm = 0x19D8
callq 0x6de0
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
jmp 0xcd65