// File: /workspace/github_cpp_makefile_star2_O1/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 $0x408, %rsp            # imm = 0x408
movq %rsi, %r14
movl %edi, 0x3c(%rsp)
leaq 0xc2bc(%rip), %rdi      # 0x104cf
leaq 0xc4a1(%rip), %rsi      # 0x106bb
callq 0x2170
testq %rax, %rax
je 0x441a
movq %rax, %rbx
leaq 0x300(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rax, %rdx
callq 0x21f0
testq %rax, %rax
je 0x4412
movabsq $0x64726f77737361, %rbp # imm = 0x64726F77737361
leaq 0x300(%rsp), %r12
leaq 0x60(%rsp), %r13
leaq 0xe0(%rsp), %r15
pxor %xmm0, %xmm0
movdqa %xmm0, 0xd0(%rsp)
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, 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)
movdqa %xmm0, 0x150(%rsp)
movq %r12, %rdi
leaq 0xc1de(%rip), %rsi      # 0x104de
movq %r13, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2130
cmpl $0x2, %eax
jne 0x43f9
movq 0x60(%rsp), %rax
movabsq $0x695f726576726573, %rcx # imm = 0x695F726576726573
xorq %rcx, %rax
movzwl 0x68(%rsp), %ecx
xorq $0x70, %rcx
orq %rax, %rcx
je 0x43c4
movq 0x60(%rsp), %rax
movabsq $0x705f726576726573, %rcx # imm = 0x705F726576726573
xorq %rcx, %rax
movl 0x68(%rsp), %ecx
xorq $0x74726f, %rcx         # imm = 0x74726F
orq %rax, %rcx
je 0x43ce
movq 0x60(%rsp), %rax
movabsq $0x6573755f63643365, %rcx # imm = 0x6573755F63643365
xorq %rcx, %rax
movzwl 0x68(%rsp), %ecx
xorq $0x72, %rcx
orq %rax, %rcx
je 0x43df
movq 0x60(%rsp), %rax
movabsq $0x7361705f63643365, %rcx # imm = 0x7361705F63643365
xorq %rcx, %rax
movq 0x66(%rsp), %rcx
xorq %rbp, %rcx
orq %rax, %rcx
je 0x43e9
movq 0x60(%rsp), %rax
movabsq $0x737361705f736561, %rcx # imm = 0x737361705F736561
xorq %rcx, %rax
movq 0x65(%rsp), %rcx
xorq %rbp, %rcx
orq %rax, %rcx
jne 0x43f9
leaq 0x280(%rsp), %rdi
jmp 0x43f1
leaq 0x168(%rsp), %rdi
jmp 0x43f1
movq %r15, %rdi
callq 0x21e0
movl %eax, 0x17c(%rsp)
jmp 0x43f9
leaq 0x180(%rsp), %rdi
jmp 0x43f1
leaq 0x200(%rsp), %rdi
movq %r15, %rsi
callq 0x2120
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdx
callq 0x21f0
testq %rax, %rax
jne 0x4268
movq %rbx, %rdi
callq 0x2140
xorl %ebx, %ebx
leaq 0xc12c(%rip), %r15      # 0x1054f
leaq 0xfd46(%rip), %r12      # 0x14170
leaq 0x10(%rsp), %r13
movl $0x0, 0x10(%rsp)
movl 0x3c(%rsp), %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
movq %r13, %r8
callq 0x2230
cmpl $0x67, %eax
jle 0x4468
cmpl $0x73, %eax
jg 0x447c
cmpl $0x68, %eax
je 0x44c8
cmpl $0x73, %eax
jne 0x44aa
orl $0x20, %ebx
jmp 0x44ec
cmpl $-0x1, %eax
je 0x44a3
cmpl $0x62, %eax
je 0x44dc
cmpl $0x65, %eax
jne 0x44aa
orl $0x2, %ebx
jmp 0x44ec
cmpl $0x74, %eax
je 0x44d7
cmpl $0x77, %eax
jne 0x44aa
movq 0xfb43(%rip), %rax      # 0x13fd0
movq (%rax), %rdi
callq 0x21e0
testl %eax, %eax
je 0x44e1
cmpl $0x1, %eax
jne 0x44e9
orl $0x18, %ebx
jmp 0x44ec
movl $0x5, %ebp
jmp 0x44ee
movq (%r14), %rsi
movq 0xfb2c(%rip), %rax      # 0x13fe0
movl (%rax), %edx
xorl %ebp, %ebp
leaq 0xc098(%rip), %rdi      # 0x10557
xorl %eax, %eax
callq 0x2040
jmp 0x44ee
movq (%r14), %rdi
callq 0x418a
movl $0x1, %ebp
jmp 0x44ee
orl $0x4, %ebx
jmp 0x44ec
orl $0x1, %ebx
jmp 0x44ec
andl $-0x19, %ebx
orl $0x8, %ebx
jmp 0x44ec
andl $-0x9, %ebx
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x442f
cmpl $0x5, %ebp
jne 0x4825
testb $0x1, %bl
je 0x4510
leaq 0xc2fc(%rip), %rdi      # 0x10807
callq 0x21c0
testb $0x2, %bl
je 0x4521
leaq 0xc2ff(%rip), %rdi      # 0x1081b
callq 0x21c0
testb $0x4, %bl
je 0x4532
leaq 0xc2fe(%rip), %rdi      # 0x1082b
callq 0x21c0
testb $0x20, %bl
je 0x4543
leaq 0xc306(%rip), %rdi      # 0x10844
callq 0x21c0
testb $0x8, %bl
je 0x4554
leaq 0xc30e(%rip), %rdi      # 0x1085d
callq 0x21c0
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
leaq 0xc01d(%rip), %r14      # 0x1057d
leaq 0x168(%rsp), %r15
leaq 0xc369(%rip), %r12      # 0x108d8
movl 0x17c(%rsp), %edx
movq %r14, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movl 0x17c(%rsp), %esi
movq %r15, %rdi
callq 0xfa60
movl %eax, 0xfbc8(%rip)      # 0x14160
testl %eax, %eax
jns 0x45c3
movq %r12, %rdi
callq 0x21c0
movl $0x1, %edi
callq 0x20d0
incl %ebp
jne 0x456f
leaq 0xc2be(%rip), %rdi      # 0x10877
callq 0x21c0
jmp 0x484a
leaq 0xc2fb(%rip), %rdi      # 0x108c5
callq 0x21c0
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x100b5(%rip)    # 0x14690
movdqa %xmm0, 0x100bd(%rip)    # 0x146a0
movdqa %xmm0, 0x10085(%rip)    # 0x14670
movdqa %xmm0, 0x1008d(%rip)    # 0x14680
leaq 0x280(%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 0xfe25(%rip), %r15      # 0x1445c
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x100, %edx            # imm = 0x100
callq 0x5fd6
leaq 0xfc00(%rip), %r12      # 0x14250
movq %r12, %rdi
movl $0x100, %esi            # imm = 0x100
movl $0x100, %edx            # imm = 0x100
callq 0x5fd6
movq %r15, %rdi
movq %r14, %rsi
callq 0xa75e
movq %r12, %rdi
movq %r14, %rsi
callq 0x6056
leaq 0x168(%rsp), %rdi
callq 0x3bcd
cmpb $0x0, 0xffdc(%rip)      # 0x14668
je 0x4829
leaq 0xc215(%rip), %rdi      # 0x108ae
callq 0x21c0
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x4bf8
leaq 0xf(%rsp), %rax
movb $0x0, (%rax)
leaq 0x40(%rsp), %r12
leaq 0xfb91(%rip), %rbp      # 0x14250
leaq 0xffaa(%rip), %r15      # 0x14670
pxor %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movq %r12, %rdi
movl %ebx, %esi
callq 0x25b9
movl 0x48(%rsp), %esi
testl %esi, %esi
je 0x47de
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
addl $0x1f, %esi
andl $-0x20, %esi
leaq 0x10(%rsp), %rdi
callq 0x4aa0
movl 0x48(%rsp), %edi
addq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
subq %rdi, %rdx
xorl %esi, %esi
callq 0x20b0
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl 0x48(%rsp), %edx
callq 0x20f0
movq %rbp, %rdi
movq %r15, %rsi
movl $0x20, %edx
callq 0x603e
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
subq %rdx, %rcx
shrq $0x5, %rcx
movq %rbp, %rdi
movq %rdx, %rsi
movl $0x1, %r8d
callq 0xa682
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
movdqu -0x20(%rax), %xmm0
movups -0x10(%rax), %xmm1
movl %eax, %edx
subl %esi, %edx
movdqa %xmm0, 0xfeeb(%rip)     # 0x14670
movaps %xmm1, 0xfef4(%rip)     # 0x14680
movl 0xf9ce(%rip), %edi      # 0x14160
callq 0xfbbb
movl %eax, %r13d
testl %eax, %eax
js 0x47aa
leaq 0xf(%rsp), %rdi
callq 0x3de3
jmp 0x47c7
callq 0x2030
movl (%rax), %edx
leaq 0xbce1(%rip), %rdi      # 0x10499
movl %r13d, %esi
xorl %eax, %eax
callq 0x2040
movb $0x1, 0xf(%rsp)
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x47de
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
movq %r14, %rdi
movq %r12, %rsi
callq 0x5b56
movb 0xf(%rsp), %r13b
testb %r13b, %r13b
jne 0x4806
movl $0x1, %edi
callq 0x20d0
testb %r13b, %r13b
je 0x46c6
leaq 0x58(%rsp), %rdi
callq 0x4c04
movl 0xf94a(%rip), %edi      # 0x14160
callq 0xfba4
movl $0xffffffff, 0xf93b(%rip) # imm = 0xFFFFFFFF
xorl %eax, %eax
jmp 0x484f
leaq 0xc068(%rip), %rdi      # 0x10898
callq 0x21c0
movl 0xf925(%rip), %edi      # 0x14160
callq 0xfba4
movl $0xffffffff, 0xf916(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4863
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x4882
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2160
jmp 0x4882
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x4c04
movq %rbx, %rdi
callq 0x2240