// File: /workspace/github_c_makefile_star2_O0/snow1eo[P]IPsec_IKEv2/ike.c
// Function: ike_init
int ike_init (void)
{
    MID++;

    const uint8_t num_transforms = 4;
    const uint8_t num_proposals = 1;

    transform *transforms = calloc (num_transforms, sizeof (transform));
    transforms[0].more = 3;
    transforms[0].transform_type = ENCR;
    transforms[0].transform_ID = ENCR_AES_CBC;
    transforms[0].attribute_format = AF_TV;
    transforms[0].attribute_type = AT_KEY_LEN;
    transforms[0].attribute_value = calloc (1, 2);
    transforms[0].attribute_value[0] = 0;
    transforms[0].attribute_value[1] = 128;

    transforms[1].more = 3;
    transforms[1].transform_type = PRF;
    transforms[1].transform_ID = PRF_HMAC_SHA1;
    transforms[1].attribute_format = AF_TV;
    transforms[1].attribute_type = AT_KEY_LEN;
    transforms[1].attribute_value = calloc (1, 2);
    transforms[1].attribute_value[1] = 160;

    transforms[2].more = 3;
    transforms[2].transform_type = INTEG;
    transforms[2].transform_ID = AUTH_HMAC_SHA1_96;
    transforms[2].attribute_format = AF_TV;
    transforms[2].attribute_type = AT_KEY_LEN;
    transforms[2].attribute_value = calloc (1, 2);
    transforms[2].attribute_value[1] = 160;

    transforms[3].more = 0;
    transforms[3].transform_type = DH_GR;
    transforms[3].transform_ID = MODP_2048;
    transforms[3].attribute_value = NULL;

    uint32_t sum_length = 0;
    for (int i = 0; i < num_transforms; i++)
    {
        if (transforms[i].transform_ID == ENCR_AES_CBC ||
            transforms[i].transform_ID == ENCR_AES_CTR ||
            transforms[i].transform_ID == ENCR_NULL ||
            transforms[i].transform_type == PRF ||
            transforms[i].transform_type == INTEG)
            transforms[i].transform_length = 12;
        else
            transforms[i].transform_length = 8;

        sum_length += transforms[i].transform_length;
    }

    proposal *proposals = calloc (num_proposals, sizeof (proposal));
    proposals[0].more = 0;
    proposals[0].proposal_num = 1;
    proposals[0].protocol_ID = IKE;
    proposals[0].SPI_size = 0;
    proposals[0].SPI = calloc (1, proposals[0].SPI_size);
    proposals[0].num_transforms = num_transforms;
    proposals[0].transforms = transforms;
    proposals[0].proposal_length = 
        sum_length + sizeof (proposal) + proposals[0].SPI_size -
        sizeof (proposals[0].SPI) - sizeof (proposals[0].transforms);

    SAi1.proposals = proposals;
    SAi1.header.next_payload = PL_KE;
    SAi1.header.critical = 0;
    SAi1.header.payload_length = sizeof (generic_payload_header);
    uint32_t prop_num = 0;
    do SAi1.header.payload_length += SAi1.proposals[prop_num].proposal_length;
    while (SAi1.proposals[prop_num++].more != 0);

    dh_group = 0;
    for (int i = 0; i < num_transforms; i++)
        if (SAi1.proposals[0].transforms[i].transform_type == DH_GR &&
            SAi1.proposals[0].transforms[i].transform_ID > dh_group)
            dh_group = SAi1.proposals[0].transforms[i].transform_ID;
    modp = get_modp (dh_group);

    KEi.header.next_payload = PL_NONCE;
    KEi.header.critical = 0;
    KEi.header.payload_length = sizeof (KEi.header) +
        sizeof (KEi.dh_group_num) + sizeof (KEi.reserved) + modp/8;
    KEi.dh_group_num = dh_group;
    KEi.reserved = 0;
    KEi.key_exchange_data = calloc (1, modp/8);
    MEMCHECK (KEi.key_exchange_data);

    mpz_set (dh_a, dh_nsize_random (A_SIZE));
    dh_set_p (modp);
    mpz_powm_sec (dh_A, dh_g, dh_a, dh_p);
    size_t *count = (size_t*) calloc (1, sizeof (size_t));
    uint32_t size = mpz_sizeinbase (dh_A, 256);
    mpz_export (KEi.key_exchange_data + modp/8 - size, count, 1, 1, 1, 0, dh_A);
    free (count);

    Ni.header.next_payload = NO_NEXT_PL;
    Ni.header.critical = 0;
    Ni_len = 4*(4 + (rand() % 60));
    Ni.header.payload_length = sizeof (Ni.header) + Ni_len;
    Ni.nonce_data = calloc (1, Ni_len);
    for (int i = 0; i < Ni_len; i++)
        Ni.nonce_data[i] = rand ();

    ike_header HDR;
    for (int i = 0; i < sizeof (HDR.init_SPI); i++)
        *((uint8_t*)&HDR.init_SPI + i) = rand ();
    HDR.resp_SPI = 0;
    HDR.next_payload = PL_SA;
    HDR.versions = VERSIONS;
    HDR.exchange_type = IKE_SA_INIT;
    HDR.flags = XXI;
    HDR.message_ID = MID;
    HDR.length = ike_sa_init_len = 
        sizeof (ike_header) + SAi1.header.payload_length +
        KEi.header.payload_length + Ni.header.payload_length;

    ike_sa_init = calloc (1, HDR.length);
    uint32_t offset = 0;
    offset += ike_hdr2char  (&HDR,  ike_sa_init + offset);
    offset += sa_pl2char    (&SAi1, ike_sa_init + offset);
    offset += ke_pl2char    (&KEi,  ike_sa_init + offset);
    offset += nonce_pl2char (&Ni,   ike_sa_init + offset);

    free (buff);
    buff = calloc (1, HDR.length);
    memcpy (buff, ike_sa_init, HDR.length);

    return offset;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl 0xca42(%rip), %eax      # 0x12430
addl $0x1, %eax
movl %eax, 0xca39(%rip)      # 0x12430
movb $0x4, -0x1(%rbp)
movb $0x1, -0x2(%rbp)
movl $0x4, %edi
movl $0x12, %esi
callq 0x3250
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movb $0x3, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x10(%rbp), %rax
movw $0xc, 0x6(%rax)
movq -0x10(%rbp), %rax
movb $-0x80, 0x8(%rax)
movq -0x10(%rbp), %rax
movb $0xe, 0x9(%rax)
movl $0x1, %edi
movl $0x2, %esi
callq 0x3250
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa(%rax)
movq -0x10(%rbp), %rax
movq 0xa(%rax), %rax
movb $0x0, (%rax)
movq -0x10(%rbp), %rax
movq 0xa(%rax), %rax
movb $-0x80, 0x1(%rax)
movq -0x10(%rbp), %rax
movb $0x3, 0x12(%rax)
movq -0x10(%rbp), %rax
movb $0x2, 0x16(%rax)
movq -0x10(%rbp), %rax
movw $0x2, 0x18(%rax)
movq -0x10(%rbp), %rax
movb $-0x80, 0x1a(%rax)
movq -0x10(%rbp), %rax
movb $0xe, 0x1b(%rax)
movl $0x1, %edi
movl $0x2, %esi
callq 0x3250
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x1c(%rax)
movq -0x10(%rbp), %rax
movq 0x1c(%rax), %rax
movb $-0x60, 0x1(%rax)
movq -0x10(%rbp), %rax
movb $0x3, 0x24(%rax)
movq -0x10(%rbp), %rax
movb $0x3, 0x28(%rax)
movq -0x10(%rbp), %rax
movw $0x2, 0x2a(%rax)
movq -0x10(%rbp), %rax
movb $-0x80, 0x2c(%rax)
movq -0x10(%rbp), %rax
movb $0xe, 0x2d(%rax)
movl $0x1, %edi
movl $0x2, %esi
callq 0x3250
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x2e(%rax)
movq -0x10(%rbp), %rax
movq 0x2e(%rax), %rax
movb $-0x60, 0x1(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x36(%rax)
movq -0x10(%rbp), %rax
movb $0x4, 0x3a(%rax)
movq -0x10(%rbp), %rax
movw $0xe, 0x3c(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x40(%rax)
movl $0x0, -0x14(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x4, -0x18(%rbp)
jge 0x5c15
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzwl 0x6(%rax), %eax
cmpl $0xc, %eax
je 0x5bc2
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzwl 0x6(%rax), %eax
cmpl $0xd, %eax
je 0x5bc2
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzwl 0x6(%rax), %eax
cmpl $0xb, %eax
je 0x5bc2
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzbl 0x4(%rax), %eax
cmpl $0x2, %eax
je 0x5bc2
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzbl 0x4(%rax), %eax
cmpl $0x3, %eax
jne 0x5bd9
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movw $0xc, 0x2(%rax)
jmp 0x5bee
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movw $0x8, 0x2(%rax)
movq -0x10(%rbp), %rax
movslq -0x18(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzwl 0x2(%rax), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5b40
movl $0x1, %edi
movl $0x18, %esi
callq 0x3250
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movb $0x0, (%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x5(%rax)
movq -0x20(%rbp), %rax
movb $0x0, 0x6(%rax)
movq -0x20(%rbp), %rax
movzbl 0x6(%rax), %eax
movl %eax, %esi
movl $0x1, %edi
callq 0x3250
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movb $0x4, 0x7(%rax)
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x14(%rbp), %eax
addq $0x18, %rax
movq -0x20(%rbp), %rcx
movzbl 0x6(%rcx), %ecx
addq %rcx, %rax
subq $0x8, %rax
subq $0x8, %rax
movw %ax, %cx
movq -0x20(%rbp), %rax
movw %cx, 0x2(%rax)
movq -0x20(%rbp), %rax
movq %rax, 0xc78e(%rip)      # 0x12438
movb $0x22, 0xc783(%rip)     # 0x12434
movb $0x0, 0xc77d(%rip)      # 0x12435
movw $0x4, 0xc775(%rip)      # 0x12436
movl $0x0, -0x24(%rbp)
movq 0xc769(%rip), %rax      # 0x12438
movl -0x24(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzwl 0x2(%rax), %ecx
movzwl 0xc752(%rip), %eax      # 0x12436
addl %ecx, %eax
movw %ax, 0xc749(%rip)       # 0x12436
movq 0xc744(%rip), %rax      # 0x12438
movl -0x24(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x24(%rbp)
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movzbl (%rax), %eax
cmpl $0x0, %eax
jne 0x5cc8
movw $0x0, 0xc86b(%rip)      # 0x12584
movl $0x0, -0x28(%rbp)
cmpl $0x4, -0x28(%rbp)
jge 0x5d98
movq 0xc70b(%rip), %rax      # 0x12438
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzbl 0x4(%rax), %eax
cmpl $0x4, %eax
jne 0x5d8b
movq 0xc6ec(%rip), %rax      # 0x12438
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movzwl 0x6(%rax), %eax
movzwl 0xc81e(%rip), %ecx      # 0x12584
cmpl %ecx, %eax
jle 0x5d8b
movq 0xc6c7(%rip), %rax      # 0x12438
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
imulq $0x12, %rcx, %rcx
addq %rcx, %rax
movw 0x6(%rax), %ax
movw %ax, 0xc7f9(%rip)       # 0x12584
jmp 0x5d8d
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x5d20
movzwl 0xc7e5(%rip), %edi      # 0x12584
callq 0x3210
movl %eax, 0xc7de(%rip)      # 0x12588
movb $0x28, 0xc6b7(%rip)     # 0x12468
movb $0x0, 0xc6b1(%rip)      # 0x12469
movl 0xc7ca(%rip), %eax      # 0x12588
shrl $0x3, %eax
movl %eax, %eax
addq $0x8, %rax
movw %ax, 0xc69c(%rip)       # 0x1246a
movw 0xc7af(%rip), %ax       # 0x12584
movw %ax, 0xc690(%rip)       # 0x1246c
movw $0x0, 0xc689(%rip)      # 0x1246e
movl 0xc79d(%rip), %eax      # 0x12588
shrl $0x3, %eax
movl %eax, %eax
movl %eax, %esi
movl $0x1, %edi
callq 0x3250
movq %rax, 0xc66d(%rip)      # 0x12470
cmpq $0x0, 0xc665(%rip)      # 0x12470
jne 0x5e36
movq 0xc1e4(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0x725a(%rip), %rsi      # 0xd078
leaq 0x7257(%rip), %rdx      # 0xd07c
movb $0x0, %al
callq 0x3270
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x34c0
movl $0x400, %edi            # imm = 0x400
callq 0x3580
movq %rax, %rsi
leaq 0xc566(%rip), %rdi      # 0x123b0
callq 0x3190
movl 0xc733(%rip), %edi      # 0x12588
callq 0x3260
leaq 0xc58f(%rip), %rdi      # 0x123f0
leaq 0xc568(%rip), %rsi      # 0x123d0
leaq 0xc541(%rip), %rdx      # 0x123b0
leaq 0xc56a(%rip), %rcx      # 0x123e0
callq 0x3380
movl $0x1, %edi
movl $0x8, %esi
callq 0x3250
movq %rax, -0x30(%rbp)
leaq 0xc55b(%rip), %rdi      # 0x123f0
movl $0x100, %esi            # imm = 0x100
callq 0x3530
movl %eax, -0x34(%rbp)
movq 0xc5c7(%rip), %rdi      # 0x12470
movl 0xc6d9(%rip), %eax      # 0x12588
shrl $0x3, %eax
movl %eax, %eax
addq %rax, %rdi
movl -0x34(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
subq %rcx, %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movl $0x1, %r8d
movl $0x1, %ecx
xorl %eax, %eax
movl %eax, %r9d
leaq 0xc511(%rip), %rax      # 0x123f0
movl %r8d, %edx
movq %rax, (%rsp)
callq 0x3500
movq -0x30(%rbp), %rdi
callq 0x3060
movb $0x0, 0xc5bd(%rip)      # 0x124b8
movb $0x0, 0xc5b7(%rip)      # 0x124b9
callq 0x3600
movl $0x3c, %ecx
cltd
idivl %ecx
movl %edx, %eax
addl $0x4, %eax
shll $0x2, %eax
movl %eax, 0xc66f(%rip)      # 0x1258c
movl 0xc669(%rip), %eax      # 0x1258c
addq $0x4, %rax
movw %ax, 0xc58c(%rip)       # 0x124ba
movl 0xc658(%rip), %eax      # 0x1258c
movl %eax, %esi
movl $0x1, %edi
callq 0x3250
movq %rax, 0xc575(%rip)      # 0x124bc
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl 0xc635(%rip), %eax      # 0x1258c
jae 0x5f79
callq 0x3600
movb %al, %dl
movq 0xc555(%rip), %rax      # 0x124bc
movslq -0x38(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x5f4e
movl $0x0, -0x58(%rbp)
movslq -0x58(%rbp), %rax
cmpq $0x8, %rax
jae 0x5fa4
callq 0x3600
movb %al, %cl
movslq -0x58(%rbp), %rax
movb %cl, -0x54(%rbp,%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5f80
movq $0x0, -0x4c(%rbp)
movb $0x21, -0x44(%rbp)
movb $0x20, -0x43(%rbp)
movb $0x22, -0x42(%rbp)
movb $0x8, -0x41(%rbp)
movl 0xc46e(%rip), %eax      # 0x12430
movl %eax, -0x40(%rbp)
movzwl 0xc46a(%rip), %eax      # 0x12436
addq $0x1c, %rax
movzwl 0xc493(%rip), %ecx      # 0x1246a
addq %rcx, %rax
movzwl 0xc4d9(%rip), %ecx      # 0x124ba
addq %rcx, %rax
movl %eax, 0xc5a6(%rip)      # 0x12590
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, %esi
movl $0x1, %edi
callq 0x3250
movq %rax, 0xc595(%rip)      # 0x12598
movl $0x0, -0x5c(%rbp)
movq 0xc587(%rip), %rsi      # 0x12598
movl -0x5c(%rbp), %eax
addq %rax, %rsi
leaq -0x54(%rbp), %rdi
callq 0x3490
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq 0xc56b(%rip), %rsi      # 0x12598
movl -0x5c(%rbp), %eax
addq %rax, %rsi
leaq 0xc3fa(%rip), %rdi      # 0x12434
callq 0x33f0
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq 0xc54c(%rip), %rsi      # 0x12598
movl -0x5c(%rbp), %eax
addq %rax, %rsi
leaq 0xc40f(%rip), %rdi      # 0x12468
callq 0x3110
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq 0xc52d(%rip), %rsi      # 0x12598
movl -0x5c(%rbp), %eax
addq %rax, %rsi
leaq 0xc440(%rip), %rdi      # 0x124b8
callq 0x3070
addl -0x5c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq 0xc516(%rip), %rdi      # 0x125a0
callq 0x3060
movl -0x3c(%rbp), %eax
movl %eax, %esi
movl $0x1, %edi
callq 0x3250
movq %rax, 0xc4fb(%rip)      # 0x125a0
movq 0xc4f4(%rip), %rdi      # 0x125a0
movq 0xc4e5(%rip), %rsi      # 0x12598
movl -0x3c(%rbp), %eax
movl %eax, %edx
callq 0x32c0
movl -0x5c(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)