// File: rellla[P]E3DC-Rscp/RscpMain.cpp
// Function: authLoop(e3dc_config_t*)
int authLoop(e3dc_config_t *config)
{
    int auth_retry = MAX_AUTH_RETRY;

    RscpProtocol protocol;
    bool bStopExecution = false;
    while ((iAuthenticated != 1) && ((auth_retry + 1) > 0)
	   && !bStopExecution) {
	auth_retry--;
	//--------------------------------------------------------------------------------------------------------------
	// RSCP Transmit Frame Block Data
	//--------------------------------------------------------------------------------------------------------------
	SRscpFrameBuffer frameBuffer;
	memset(&frameBuffer, 0, sizeof(frameBuffer));

	// create an RSCP frame with requests to some example data
	createAuthRequest(&frameBuffer, config);

	// check that frame data was created
	if (frameBuffer.dataLength > 0) {
	    // resize temporary encryption buffer to a multiple of AES_BLOCK_SIZE
	    std::vector < uint8_t > encryptionBuffer;
	    encryptionBuffer.resize(ROUNDUP
				    (frameBuffer.dataLength,
				     AES_BLOCK_SIZE));
	    // zero padding for data above the desired length
	    memset(&encryptionBuffer[0] +
		   frameBuffer.dataLength, 0,
		   encryptionBuffer.size() - frameBuffer.dataLength);
	    // copy desired data length
	    memcpy(&encryptionBuffer[0], frameBuffer.data,
		   frameBuffer.dataLength);
	    // set continues encryption IV
	    aesEncrypter.SetIV(ucEncryptionIV, AES_BLOCK_SIZE);
	    // start encryption from encryptionBuffer to encryptionBuffer, blocks = encryptionBuffer.size() / AES_BLOCK_SIZE
	    aesEncrypter.Encrypt(&encryptionBuffer[0],
				 &encryptionBuffer[0],
				 encryptionBuffer.size() / AES_BLOCK_SIZE);
	    // save new IV for next encryption block
	    memcpy(ucEncryptionIV,
		   &encryptionBuffer[0] +
		   encryptionBuffer.size() - AES_BLOCK_SIZE,
		   AES_BLOCK_SIZE);

	    // send data on socket
	    int iResult = SocketSendData(iSocket,
					 &encryptionBuffer[0],
					 encryptionBuffer.size());
	    if (iResult < 0) {
		printf("Socket send error %i. errno %i\n", iResult, errno);
		bStopExecution = true;
		continue;
	    } else {
		// go into receive loop and wait for response
		receiveLoop(bStopExecution);
	    }
	}

	// auth loop sleep / cycle time before next request
	if (!iAuthenticated) {
	    printf("Authentication failed, retry...\n");
	    sleep(1);
	}
	// free frame buffer memory
	protocol.destroyFrameData(&frameBuffer);
    }

    if (!iAuthenticated)
	printf("Authentication failed due to timeout\n");

    return 0;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movl $0x3, 0xec(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x7840
movb $0x0, 0xdf(%rsp)
xorl %eax, %eax
cmpl $0x1, 0x1ff30(%rip)     # 0x24698
movb %al, 0x8f(%rsp)
je 0x4799
movl 0xec(%rsp), %ecx
addl $0x1, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, 0x8f(%rsp)
jle 0x4799
movb 0xdf(%rsp), %al
xorb $-0x1, %al
movb %al, 0x8f(%rsp)
movb 0x8f(%rsp), %al
testb $0x1, %al
jne 0x47a9
jmp 0x4aab
movl 0xec(%rsp), %eax
decl %eax
movl %eax, 0xec(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movq 0xf0(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x2420
jmp 0x47db
cmpl $0x0, 0xc8(%rsp)
jbe 0x4a68
leaq 0x98(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x5fe0
movq 0x80(%rsp), %rdi
movl 0xc8(%rsp), %eax
addl $0x1f, %eax
andl $-0x20, %eax
movl %eax, %esi
callq 0x5ff0
jmp 0x481c
xorl %eax, %eax
movl %eax, 0x6c(%rsp)
movl %eax, %esi
movq %rsi, 0x78(%rsp)
leaq 0x98(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x6090
movq 0x70(%rsp), %rdi
movl 0xc8(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
callq 0x60b0
movq 0x60(%rsp), %rdi
movl 0x6c(%rsp), %esi
movq %rax, %rdx
movl 0xc8(%rsp), %eax
subq %rax, %rdx
callq 0x20e0
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x6090
movq %rax, %rdi
movq 0xc0(%rsp), %rsi
movl 0xc8(%rsp), %eax
movl %eax, %edx
callq 0x2130
leaq 0x1f9e2(%rip), %rdi     # 0x24280
leaq 0x1fdfb(%rip), %rsi     # 0x246a0
movl $0x20, %edx
callq 0xac30
jmp 0x48b1
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x40(%rsp)
leaq 0x98(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x6090
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdi
movq %rax, 0x50(%rsp)
callq 0x6090
movq 0x48(%rsp), %rdi
movq %rax, 0x58(%rsp)
callq 0x60b0
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq %rax, %rcx
shrq $0x5, %rcx
leaq 0x1f979(%rip), %rdi     # 0x24280
movl $0x1, %r8d
callq 0x14500
jmp 0x4914
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x10(%rsp)
leaq 0x98(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x6090
movq 0x20(%rsp), %rdi
movq %rax, 0x18(%rsp)
callq 0x60b0
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movq 0x18(%rsp), %rax
movups -0x20(%rax,%rcx), %xmm0
movups -0x10(%rax,%rcx), %xmm1
movups %xmm1, 0x1fd4f(%rip)    # 0x246b0
movups %xmm0, 0x1fd38(%rip)    # 0x246a0
movl 0x1f822(%rip), %eax     # 0x24190
movl %eax, 0x2c(%rsp)
callq 0x6090
movq 0x20(%rsp), %rdi
movq %rax, 0x30(%rsp)
callq 0x60b0
movl 0x2c(%rsp), %edi
movq 0x30(%rsp), %rsi
movl %eax, %edx
callq 0x1f6b0
movl %eax, 0x3c(%rsp)
jmp 0x499c
movl 0x3c(%rsp), %eax
movl %eax, 0x94(%rsp)
cmpl $0x0, 0x94(%rsp)
jge 0x4a2b
movl 0x94(%rsp), %eax
movl %eax, 0xc(%rsp)
callq 0x2030
movl 0xc(%rsp), %esi
movl (%rax), %edx
leaq 0x1bad8(%rip), %rdi     # 0x204a6
xorl %eax, %eax
callq 0x2040
jmp 0x49d7
movb $0x1, 0xdf(%rsp)
movl $0x2, 0x90(%rsp)
jmp 0x4a47
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x4ae8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x60d0
jmp 0x4ae8
leaq 0xdf(%rsp), %rdi
callq 0x4b10
jmp 0x4a3a
jmp 0x4a3c
movl $0x0, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x60d0
movl 0x90(%rsp), %eax
testl %eax, %eax
je 0x4a66
jmp 0x4a61
jmp 0x475f
jmp 0x4a68
cmpl $0x0, 0x1fc29(%rip)     # 0x24698
jne 0x4a8f
leaq 0x1ba4e(%rip), %rdi     # 0x204c6
xorl %eax, %eax
callq 0x2040
jmp 0x4a81
movl $0x1, %edi
callq 0x2110
jmp 0x4a8d
jmp 0x4a8f
leaq 0xe0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x9580
jmp 0x4aa6
jmp 0x475f
cmpl $0x0, 0x1fbe6(%rip)     # 0x24698
jne 0x4ac6
leaq 0x1ba2c(%rip), %rdi     # 0x204e7
xorl %eax, %eax
callq 0x2040
jmp 0x4ac4
jmp 0x4ac6
movl $0x1, 0x90(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x7860
xorl %eax, %eax
addq $0xf8, %rsp
retq
leaq 0xe0(%rsp), %rdi
callq 0x7860
movq 0xb8(%rsp), %rdi
callq 0x22a0
nopw %cs:(%rax,%rax)