// File: /workspace/github_c_makefile_star2_O2/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 0xbdb5
movq %rsi, %r14
xorl %ebp, %ebp
testq %rsi, %rsi
je 0xbdec
movq %rdi, %rbx
movl $0x42000801, %esi       # imm = 0x42000801
callq 0x60a0
testl %eax, %eax
je 0xbdba
movl %eax, %ebp
leaq 0xba64(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x1971, %edx           # imm = 0x1971
jmp 0xbde7
pushq $-0x11
popq %rbp
jmp 0xbdec
movq 0x8(%rbx), %r12
leaq 0x4(%r12), %r15
movq %r15, 0x8(%rbx)
movl (%r14), %esi
movq %rbx, %rdi
callq 0x68b0
testl %eax, %eax
je 0xbdfd
movl %eax, %ebp
leaq 0xba30(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x1977, %edx           # imm = 0x1977
callq 0x6e80
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 0xbe2a
movq %rbx, %rdi
movl $0x420009, %esi         # imm = 0x420009
callq 0x63f0
testl %eax, %eax
je 0xbe2a
movl %eax, %ebp
leaq 0xb9ef(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x197c, %edx           # imm = 0x197C
jmp 0xbde7
movl (%r14), %eax
cmpq $0xe, %rax
ja 0xbf83
movq 0x8(%rbx), %r13
leaq 0xb55e(%rip), %rcx      # 0x173a0
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 0x6220
jmp 0xbe9c
movq 0x8(%r14), %rax
movl (%rax), %edx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x6700
jmp 0xbe9c
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x66a0
jmp 0xbe9c
movq 0x8(%r14), %rax
movl (%rax), %edx
movq %rbx, %rdi
movl $0x42000b, %esi         # imm = 0x42000B
callq 0x63f0
movl %eax, %ebp
testl %ebp, %ebp
je 0xbeb6
leaq 0xb966(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x19dc, %edx           # imm = 0x19DC
jmp 0xbde7
movq 0x8(%rbx), %r14
movq %r12, 0x8(%rbx)
movq %r14, %rsi
subq %r15, %rsi
movq %rbx, %rdi
callq 0x6b10
testl %eax, %eax
je 0xbee6
movl %eax, %ebp
leaq 0xb936(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x19e2, %edx           # imm = 0x19E2
jmp 0xbde7
movq %r14, 0x8(%rbx)
xorl %ebp, %ebp
jmp 0xbdec
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x6cc0
testl %eax, %eax
je 0xbf63
movl %eax, %ebp
leaq 0xb905(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x19cd, %edx           # imm = 0x19CD
jmp 0xbde7
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x62e0
testl %eax, %eax
je 0xbf63
movl %eax, %ebp
leaq 0xb8df(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x1988, %edx           # imm = 0x1988
jmp 0xbde7
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0x6180
testl %eax, %eax
je 0xbf63
movl %eax, %ebp
leaq 0xb8b9(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x199a, %edx           # imm = 0x199A
jmp 0xbde7
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 0xbe9e
leaq 0xb885(%rip), %rsi      # 0x1780f
movq %rbx, %rdi
movl $0x19d8, %edx           # imm = 0x19D8
callq 0x6e80
pushq $-0x3
jmp 0xbdb7