// File: /workspace/github_cpp_makefile_star2_O3/rellla[P]E3DC-Rscp/RscpMain.cpp
// Function: main
int main(int argc, char *argv[])
{
    int iConnected = 0;
    int conn_retry = MAX_CONN_RETRY;
    int opt;
    int requests = 0;

    // get conf parameters
    FILE *fp = fopen(CONF_FILE, "r");
    char var[128], value[128], line[256];
    e3dc_config_t e3dc_config;
    if(fp) {
	while (fgets(line, sizeof(line), fp)) {
	    memset(var, 0, sizeof(var));
	    memset(value, 0, sizeof(value));
	    if(sscanf(line, "%[^ \t=]%*[\t ]=%*[\t ]%[^\n]", var, value) == 2) {
		if(strcmp(var, "server_ip") == 0)
		    strcpy(e3dc_config.server_ip, value);
		else if(strcmp(var, "server_port") == 0)
		    e3dc_config.server_port = atoi(value);
		else if(strcmp(var, "e3dc_user") == 0)
		    strcpy(e3dc_config.e3dc_user, value);
		else if(strcmp(var, "e3dc_password") == 0)
		    strcpy(e3dc_config.e3dc_password, value);
		else if(strcmp(var, "aes_password") == 0)
		    strcpy(e3dc_config.aes_password, value);
	    }
	}
	fclose(fp);
    }

    // get commandline parameters
    while (1) {
	static struct option long_options[] = {
	    {"help",		no_argument,		0, 'h'},
	    {"battery",		no_argument,		0, 'b'},
	    {"ems",		no_argument,		0, 'e'},
	    {"time",		no_argument,		0, 't'},
	    {"settime",		no_argument,		0, 's'},
	    {"weather",		required_argument,	0, 'w' },
	};
	int option_index = 0;
	opt = getopt_long(argc, argv, "hbetsw:", long_options, &option_index);

	if(opt == -1)
	    break;

	switch (opt){
	case 'h': {
	    showhelp(argv[0]);
	    return 0;
	    break;
	    }
	case 'b': {
	    requests |= TAG_BATTERY;
	    break;
	    }
	case 'e': {
	    requests |= TAG_EMS;
	    break;
	    }
	case 't': {
	    requests |= TAG_GET_IDLE_PERIODS;
	    break;
	    }
	case 's': {
	    requests |= TAG_SET_IDLE_PERIODS;
	    break;
	    }
	case 'w': {
	    requests |= TAG_WEATHER_ENABLE;
	    if (atoi(optarg) == 1)
		requests |= TAG_WEATHER_ENABLE_F;
	    else if (atoi(optarg) == 0)
		requests &= ~TAG_WEATHER_ENABLE_F;
	    else
		requests &= ~TAG_WEATHER_ENABLE;
	    break;
	    }
	default:
	    printf("%s: option '-%c' is invalid: ignored\n", argv[0], optopt);
	    break;
	}
    }

    if(requests & TAG_BATTERY)
	printf("Get battery details\n");
    if(requests & TAG_EMS)
	printf("Get ems details\n");
    if(requests & TAG_GET_IDLE_PERIODS)
	printf("Get idle periods details\n");
    if(requests & TAG_SET_IDLE_PERIODS)
	printf("Set idle periods details\n");
    if(requests & TAG_WEATHER_ENABLE)
	printf("Set weather enable option\n");

    // connect to server
    while (!iConnected && (conn_retry + 1) > 0) {
	printf("Connecting to server %s:%i\n", e3dc_config.server_ip, e3dc_config.server_port);
	iSocket = SocketConnect(e3dc_config.server_ip, e3dc_config.server_port);
	if (iSocket < 0) {
	    printf("Connection failed\n");
	    sleep(1);
	    conn_retry--;
	    continue;
	}
	iConnected = 1;
	printf("Connection success\n");

    }
    if (!iConnected) {
	printf("Connection failed due to timeout\n");
	return -1;
    }
    // create AES key and set AES parameters
    {
	// initialize AES encryptor and decryptor IV
	memset(ucDecryptionIV, 0xff, AES_BLOCK_SIZE);
	memset(ucEncryptionIV, 0xff, AES_BLOCK_SIZE);

	// limit password length to AES_KEY_SIZE
	int iPasswordLength = strlen(e3dc_config.aes_password);
	if (iPasswordLength > AES_KEY_SIZE)
	    iPasswordLength = AES_KEY_SIZE;

	// copy up to 32 bytes of AES key password
	uint8_t ucAesKey[AES_KEY_SIZE];
	memset(ucAesKey, 0xff, AES_KEY_SIZE);
	memcpy(ucAesKey, e3dc_config.aes_password, iPasswordLength);

	// set encryptor and decryptor parameters
	aesDecrypter.SetParameters(AES_KEY_SIZE * 8, AES_BLOCK_SIZE * 8);
	aesEncrypter.SetParameters(AES_KEY_SIZE * 8, AES_BLOCK_SIZE * 8);
	aesDecrypter.StartDecryption(ucAesKey);
	aesEncrypter.StartEncryption(ucAesKey);
    }

    authLoop(&e3dc_config);
    if (iAuthenticated) {
	printf("Authentication success\n");
	// enter the main transmit / receive loop
	mainLoop(requests);
    } else {
	printf("Authentication failed\n");
	// close socket connection
	SocketClose(iSocket);
	iSocket = -1;
	return -1;
    }

    // close socket connection
    SocketClose(iSocket);
    iSocket = -1;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3f8, %rsp            # imm = 0x3F8
movq %rsi, %r14
movl %edi, %ebp
leaq 0xc44b(%rip), %rdi      # 0x104cf
leaq 0xc630(%rip), %rsi      # 0x106bb
callq 0x2170
testq %rax, %rax
je 0x4288
movq %rax, %rbx
leaq 0x2f0(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rax, %rdx
callq 0x21f0
testq %rax, %rax
je 0x4280
leaq 0x2f0(%rsp), %r12
leaq 0x50(%rsp), %r13
leaq 0xd0(%rsp), %r15
pxor %xmm0, %xmm0
movdqa %xmm0, 0xc0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
movdqa %xmm0, 0xa0(%rsp)
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0xd0(%rsp)
movdqa %xmm0, 0xe0(%rsp)
movdqa %xmm0, 0xf0(%rsp)
movdqa %xmm0, 0x100(%rsp)
movdqa %xmm0, 0x110(%rsp)
movdqa %xmm0, 0x120(%rsp)
movdqa %xmm0, 0x130(%rsp)
movdqa %xmm0, 0x140(%rsp)
movq %r12, %rdi
leaq 0xc37a(%rip), %rsi      # 0x104de
movq %r13, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2130
cmpl $0x2, %eax
jne 0x4267
movq 0x50(%rsp), %rax
movabsq $0x695f726576726573, %rcx # imm = 0x695F726576726573
xorq %rcx, %rax
movzwl 0x58(%rsp), %ecx
xorq $0x70, %rcx
orq %rax, %rcx
je 0x4232
movq 0x50(%rsp), %rax
movabsq $0x705f726576726573, %rcx # imm = 0x705F726576726573
xorq %rcx, %rax
movl 0x58(%rsp), %ecx
xorq $0x74726f, %rcx         # imm = 0x74726F
orq %rax, %rcx
je 0x423c
movq 0x50(%rsp), %rax
movabsq $0x6573755f63643365, %rcx # imm = 0x6573755F63643365
xorq %rcx, %rax
movzwl 0x58(%rsp), %ecx
xorq $0x72, %rcx
orq %rax, %rcx
je 0x424d
movq 0x50(%rsp), %rax
movabsq $0x7361705f63643365, %rcx # imm = 0x7361705F63643365
xorq %rcx, %rax
movq 0x56(%rsp), %rcx
movabsq $0x64726f77737361, %rdx # imm = 0x64726F77737361
xorq %rdx, %rcx
orq %rax, %rcx
je 0x4257
movq 0x50(%rsp), %rax
movabsq $0x737361705f736561, %rcx # imm = 0x737361705F736561
xorq %rcx, %rax
movq 0x55(%rsp), %rcx
xorq %rdx, %rcx
orq %rax, %rcx
jne 0x4267
leaq 0x270(%rsp), %rdi
jmp 0x425f
leaq 0x158(%rsp), %rdi
jmp 0x425f
movq %r15, %rdi
callq 0x21e0
movl %eax, 0x16c(%rsp)
jmp 0x4267
leaq 0x170(%rsp), %rdi
jmp 0x425f
leaq 0x1f0(%rsp), %rdi
movq %r15, %rsi
callq 0x2120
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x21f0
testq %rax, %rax
jne 0x40cf
movq %rbx, %rdi
callq 0x2140
leaq 0xc2c0(%rip), %r15      # 0x1054f
leaq 0xeeda(%rip), %r12      # 0x13170
leaq 0x10(%rsp), %r13
xorl %ebx, %ebx
movl $0x0, 0x10(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x2230
cmpl $0x72, %eax
jle 0x42e9
cmpl $0x73, %eax
je 0x4302
cmpl $0x74, %eax
je 0x4307
cmpl $0x77, %eax
jne 0x4329
movq 0xecfd(%rip), %rax      # 0x12fd0
movq (%rax), %rdi
callq 0x21e0
testl %eax, %eax
je 0x430c
cmpl $0x1, %eax
jne 0x4314
orl $0x18, %ebx
jmp 0x429d
cmpl $0x64, %eax
jg 0x42f8
cmpl $0x62, %eax
jne 0x4319
orl $0x1, %ebx
jmp 0x429d
cmpl $0x65, %eax
jne 0x4320
orl $0x2, %ebx
jmp 0x429d
orl $0x20, %ebx
jmp 0x429d
orl $0x4, %ebx
jmp 0x429d
andl $-0x19, %ebx
orl $0x8, %ebx
jmp 0x429d
andl $-0x9, %ebx
jmp 0x429d
cmpl $-0x1, %eax
jne 0x4329
jmp 0x4348
cmpl $0x68, %eax
je 0x440c
movq (%r14), %rsi
movq 0xecad(%rip), %rax      # 0x12fe0
movl (%rax), %edx
leaq 0xc21b(%rip), %rdi      # 0x10557
xorl %eax, %eax
callq 0x2040
jmp 0x429d
testb $0x1, %bl
je 0x4359
leaq 0xc4b3(%rip), %rdi      # 0x10807
callq 0x21c0
testb $0x2, %bl
je 0x436a
leaq 0xc4b6(%rip), %rdi      # 0x1081b
callq 0x21c0
testb $0x4, %bl
je 0x437b
leaq 0xc4b5(%rip), %rdi      # 0x1082b
callq 0x21c0
testb $0x20, %bl
je 0x438c
leaq 0xc4bd(%rip), %rdi      # 0x10844
callq 0x21c0
testb $0x8, %bl
je 0x439d
leaq 0xc4c5(%rip), %rdi      # 0x1085d
callq 0x21c0
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
leaq 0xc1d4(%rip), %r14      # 0x1057d
leaq 0x158(%rsp), %r15
leaq 0xc520(%rip), %r12      # 0x108d8
movl 0x16c(%rsp), %edx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movl 0x16c(%rsp), %esi
movq %r15, %rdi
callq 0xf7ec
movl %eax, 0xed7f(%rip)      # 0x13160
testl %eax, %eax
jns 0x4419
movq %r12, %rdi
callq 0x21c0
movl $0x1, %edi
callq 0x20d0
incl %ebp
jne 0x43b8
leaq 0xc475(%rip), %rdi      # 0x10877
callq 0x21c0
jmp 0x4676
movq (%r14), %rdi
callq 0x3ffd
jmp 0x469c
leaq 0xc4a5(%rip), %rdi      # 0x108c5
callq 0x21c0
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0xf25f(%rip)     # 0x13690
movdqa %xmm0, 0xf267(%rip)     # 0x136a0
movdqa %xmm0, 0xf22f(%rip)     # 0x13670
movdqa %xmm0, 0xf237(%rip)     # 0x13680
leaq 0x270(%rsp), %r15
movq %r15, %rdi
callq 0x2080
cmpl $0x20, %eax
movl $0x20, %ecx
cmovll %eax, %ecx
leaq 0x10(%rsp), %r14
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x10(%r14)
movdqa %xmm0, (%r14)
movslq %ecx, %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x20f0
leaq 0xefcf(%rip), %r15      # 0x1345c
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x100, %edx            # imm = 0x100
callq 0x5d6a
leaq 0xedaa(%rip), %r12      # 0x13250
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x100, %edx            # imm = 0x100
callq 0x5d6a
movq %r15, %rdi
movq %r14, %rsi
callq 0xa4f0
movq %r12, %rdi
movq %r14, %rsi
callq 0x5de8
leaq 0x158(%rsp), %rdi
callq 0x3a61
cmpb $0x0, 0xf186(%rip)      # 0x13668
je 0x4655
leaq 0xc3bf(%rip), %rdi      # 0x108ae
callq 0x21c0
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x49e0
leaq 0xf(%rsp), %rax
movb $0x0, (%rax)
leaq 0x30(%rsp), %r12
leaq 0xed3b(%rip), %rbp      # 0x13250
leaq 0xf154(%rip), %r15      # 0x13670
pxor %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
movq %r12, %rdi
movl %ebx, %esi
callq 0x2629
movl 0x38(%rsp), %esi
testl %esi, %esi
je 0x4634
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
addl $0x1f, %esi
andl $-0x20, %esi
leaq 0x10(%rsp), %rdi
callq 0x4896
movl 0x38(%rsp), %edi
addq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
subq %rdi, %rdx
xorl %esi, %esi
callq 0x20b0
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x38(%rsp), %edx
callq 0x20f0
movq %rbp, %rdi
movq %r15, %rsi
movl $0x20, %edx
callq 0x5dd0
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
movq %rbp, %rdi
movq %rdx, %rsi
movl $0x1, %r8d
callq 0xa414
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
movdqu -0x20(%rax), %xmm0
movups -0x10(%rax), %xmm1
movl %eax, %edx
subl %esi, %edx
movdqa %xmm0, 0xf095(%rip)     # 0x13670
movaps %xmm1, 0xf09e(%rip)     # 0x13680
movl 0xeb78(%rip), %edi      # 0x13160
callq 0xf94c
movl %eax, %r13d
testl %eax, %eax
js 0x4600
leaq 0xf(%rsp), %rdi
callq 0x3c6b
jmp 0x461d
callq 0x2030
movl (%rax), %edx
leaq 0xbe8b(%rip), %rdi      # 0x10499
movl %r13d, %esi
xorl %eax, %eax
callq 0x2040
movb $0x1, 0xf(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4634
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
movq %r14, %rdi
movq %r12, %rsi
callq 0x58e0
cmpb $0x0, 0xf(%rsp)
jne 0x467d
movl $0x1, %edi
callq 0x20d0
jmp 0x451c
leaq 0xc23c(%rip), %rdi      # 0x10898
callq 0x21c0
movl 0xeaf9(%rip), %edi      # 0x13160
callq 0xf935
movl $0xffffffff, 0xeaea(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x469e
leaq 0x48(%rsp), %rdi
callq 0x49ec
movl 0xead3(%rip), %edi      # 0x13160
callq 0xf935
movl $0xffffffff, 0xeac4(%rip) # imm = 0xFFFFFFFF
xorl %eax, %eax
addq $0x3f8, %rsp            # imm = 0x3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x46b2
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x46d1
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
jmp 0x46d1
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x49ec
movq %rbx, %rdi
callq 0x2240
nop