// File: /workspace/github_cpp_makefile_star2_O2/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 $0x3e8, %rsp            # imm = 0x3E8
movq %rsi, %r14
movl %edi, %ebp
leaq 0xbcc4(%rip), %rdi      # 0xf4cf
leaq 0xbeda(%rip), %rsi      # 0xf6ec
callq 0x21b0
testq %rax, %rax
je 0x39ba
movq %rax, %rbx
leaq 0x2e0(%rsp), %r15
leaq 0xc0(%rsp), %r13
leaq 0x40(%rsp), %r12
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x2230
testq %rax, %rax
je 0x39b2
pxor %xmm0, %xmm0
movdqa %xmm0, 0x130(%rsp)
movdqa %xmm0, 0x120(%rsp)
movdqa %xmm0, 0x110(%rsp)
movdqa %xmm0, 0x100(%rsp)
movdqa %xmm0, 0xf0(%rsp)
movdqa %xmm0, 0xe0(%rsp)
movdqa %xmm0, 0xd0(%rsp)
movdqa %xmm0, 0xc0(%rsp)
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x50(%rsp)
movdqa %xmm0, 0x60(%rsp)
movdqa %xmm0, 0x70(%rsp)
movdqa %xmm0, 0x80(%rsp)
movdqa %xmm0, 0x90(%rsp)
movdqa %xmm0, 0xa0(%rsp)
movdqa %xmm0, 0xb0(%rsp)
movq %r15, %rdi
leaq 0xbbfb(%rip), %rsi      # 0xf4de
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2160
cmpl $0x2, %eax
jne 0x3838
movq %r13, %rdi
leaq 0xbbf5(%rip), %rsi      # 0xf4f8
pushq $0xa
popq %rdx
callq 0x21a0
testl %eax, %eax
je 0x3975
movq %r13, %rdi
leaq 0xbbe9(%rip), %rsi      # 0xf502
pushq $0xc
popq %rdx
callq 0x21a0
testl %eax, %eax
je 0x397f
movq %r13, %rdi
leaq 0xbbdf(%rip), %rsi      # 0xf50e
pushq $0xa
popq %rdx
callq 0x21a0
testl %eax, %eax
je 0x3993
movq %r13, %rdi
leaq 0xbbd3(%rip), %rsi      # 0xf518
pushq $0xe
popq %rdx
callq 0x21a0
testl %eax, %eax
je 0x399d
movq %r13, %rdi
leaq 0xbbcb(%rip), %rsi      # 0xf526
pushq $0xd
popq %rdx
callq 0x21a0
testl %eax, %eax
jne 0x3838
leaq 0x260(%rsp), %rdi
jmp 0x39a5
leaq 0x148(%rsp), %rdi
jmp 0x39a5
movq %r12, %rdi
callq 0x2220
movl %eax, 0x15c(%rsp)
jmp 0x3838
leaq 0x160(%rsp), %rdi
jmp 0x39a5
leaq 0x1e0(%rsp), %rdi
movq %r12, %rsi
callq 0x2150
jmp 0x3838
movq %rbx, %rdi
callq 0x2170
leaq 0xbb8e(%rip), %r15      # 0xf54f
leaq 0xf7c8(%rip), %r12      # 0x13190
leaq 0x10(%rsp), %r13
xorl %ebx, %ebx
andl $0x0, 0x10(%rsp)
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x2270
cmpl $0x62, %eax
je 0x3a55
cmpl $0x65, %eax
je 0x3a2b
cmpl $0x73, %eax
je 0x3a26
cmpl $0x74, %eax
je 0x3a4d
cmpl $0x77, %eax
je 0x3a30
cmpl $-0x1, %eax
je 0x3a7f
cmpl $0x68, %eax
je 0x3a70
movq (%r14), %rsi
movq 0xf5cc(%rip), %rax      # 0x12fe0
movl (%rax), %edx
leaq 0xbb3a(%rip), %rdi      # 0xf557
xorl %eax, %eax
callq 0x2040
jmp 0x39cf
orl $0x20, %ebx
jmp 0x39cf
orl $0x2, %ebx
jmp 0x39cf
movq 0xf599(%rip), %rax      # 0x12fd0
movq (%rax), %rdi
callq 0x2220
testl %eax, %eax
je 0x3a5d
cmpl $0x1, %eax
jne 0x3a68
orl $0x18, %ebx
jmp 0x39cf
orl $0x4, %ebx
jmp 0x39cf
orl $0x1, %ebx
jmp 0x39cf
andl $-0x19, %ebx
orl $0x8, %ebx
jmp 0x39cf
andl $-0x9, %ebx
jmp 0x39cf
movq (%r14), %rdi
callq 0x3784
xorl %eax, %eax
jmp 0x3dad
testb $0x1, %bl
je 0x3a90
leaq 0xbdad(%rip), %rdi      # 0xf838
callq 0x2210
testb $0x2, %bl
je 0x3aa1
leaq 0xbdb0(%rip), %rdi      # 0xf84c
callq 0x2210
testb $0x4, %bl
je 0x3ab2
leaq 0xbdaf(%rip), %rdi      # 0xf85c
callq 0x2210
testb $0x20, %bl
je 0x3ac3
leaq 0xbdb7(%rip), %rdi      # 0xf875
callq 0x2210
testb $0x8, %bl
je 0x3ad4
leaq 0xbdbf(%rip), %rdi      # 0xf88e
callq 0x2210
pushq $0x3
popq %r12
leaq 0xba9e(%rip), %r14      # 0xf57d
leaq 0x148(%rsp), %r15
pushq $0x1
popq %rbp
leaq 0xbe05(%rip), %r13      # 0xf8f6
movb $0x1, %al
testb $0x1, %al
je 0x3b4d
testl %r12d, %r12d
js 0x3b4d
movl 0x15c(%rsp), %edx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movl 0x15c(%rsp), %esi
movq %r15, %rdi
callq 0xea20
movl %eax, 0xf65b(%rip)      # 0x13180
testl %eax, %eax
js 0x3b35
movq %r13, %rdi
callq 0x2210
xorl %eax, %eax
jmp 0x3af3
leaq 0xbdcd(%rip), %rdi      # 0xf909
callq 0x2210
movl %ebp, %edi
callq 0x2100
decl %r12d
jmp 0x3af1
testb $0x1, %al
je 0x3b62
leaq 0xbd50(%rip), %rdi      # 0xf8a8
callq 0x2210
jmp 0x3daa
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0xfb52(%rip)     # 0x136c0
movdqa %xmm0, 0xfb3a(%rip)     # 0x136b0
movdqa %xmm0, 0xfb12(%rip)     # 0x13690
movdqa %xmm0, 0xfb1a(%rip)     # 0x136a0
leaq 0x260(%rsp), %r15
movq %r15, %rdi
callq 0x20a0
cmpl $0x20, %eax
pushq $0x20
popq %rcx
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 0x2120
leaq 0xf8b4(%rip), %r15      # 0x1347c
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x100, %edx            # imm = 0x100
callq 0x545a
leaq 0xf68f(%rip), %r12      # 0x13270
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x100, %edx            # imm = 0x100
callq 0x545a
movq %r15, %rdi
movq %r14, %rsi
callq 0x9c04
movq %r12, %rdi
movq %r14, %rsi
callq 0x54d0
leaq 0x148(%rsp), %rdi
callq 0x322e
cmpb $0x1, 0xfa6b(%rip)      # 0x13688
jne 0x3d8c
leaq 0xbcb5(%rip), %rdi      # 0xf8df
callq 0x2210
leaq 0x8(%rsp), %rdi
callq 0x4380
leaq 0x7(%rsp), %rax
movb $0x0, (%rax)
xorl %r14d, %r14d
leaq 0x30(%rsp), %r12
pushq $0x1
popq %rbp
leaq 0x10(%rsp), %r13
leaq 0xf618(%rip), %r15      # 0x13270
testb $0x1, %r14b
jne 0x3dbf
pxor %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
movq %r12, %rdi
movl %ebx, %esi
callq 0x24f1
movl 0x38(%rsp), %esi
testl %esi, %esi
je 0x3d65
andq $0x0, 0x20(%rsp)
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
addl $0x1f, %esi
andl $-0x20, %esi
movq %r13, %rdi
callq 0x3f7e
movl 0x38(%rsp), %edi
addq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
subq %rdi, %rdx
xorl %esi, %esi
callq 0x20e0
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x38(%rsp), %edx
callq 0x2120
movq %r15, %rdi
leaq 0xf9bb(%rip), %rsi      # 0x13690
pushq $0x20
popq %rdx
callq 0x54ba
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
movq %r15, %rdi
movq %rdx, %rsi
movl %ebp, %r8d
callq 0x9b3c
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
movdqu -0x20(%rax), %xmm0
movups -0x10(%rax), %xmm1
movl %eax, %edx
subl %esi, %edx
movdqa %xmm0, 0xf975(%rip)     # 0x13690
movaps %xmm1, 0xf97e(%rip)     # 0x136a0
movl 0xf458(%rip), %edi      # 0x13180
callq 0xeb64
movl %eax, %r14d
testl %eax, %eax
js 0x3d40
leaq 0x7(%rsp), %rdi
callq 0x3407
jmp 0x3d5d
callq 0x2030
movl (%rax), %edx
leaq 0xb74b(%rip), %rdi      # 0xf499
movl %r14d, %esi
xorl %eax, %eax
callq 0x2040
movb $0x1, 0x7(%rsp)
movq %r13, %rdi
callq 0x4214
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x502e
movb 0x7(%rsp), %r14b
testb %r14b, %r14b
jne 0x3c58
movl %ebp, %edi
callq 0x2100
jmp 0x3c58
leaq 0xbb36(%rip), %rdi      # 0xf8c9
callq 0x2210
movl 0xf3e2(%rip), %edi      # 0x13180
callq 0xeb4d
orl $-0x1, 0xf3d6(%rip)     # 0x13180
pushq $-0x1
popq %rax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8(%rsp), %rdi
callq 0x438c
movl 0xf3b1(%rip), %edi      # 0x13180
callq 0xeb4d
orl $-0x1, 0xf3a5(%rip)     # 0x13180
jmp 0x3a78
jmp 0x3de2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4214
jmp 0x3df4
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x438c
movq %rbx, %rdi
callq 0x2280