// File: /workspace/github_c_makefile_star2_O2/erikdubbelboer[P]discnt/src/cluster.c
// Function: clusterLoadConfig
int clusterLoadConfig(char *filename) {
    FILE *fp = fopen(filename,"r");
    struct stat sb;
    char *line;
    int maxline, j;

    if (fp == NULL) {
        if (errno == ENOENT) {
            return C_ERR;
        } else {
            serverLog(LL_WARNING,
                "Loading the cluster node config from %s: %s",
                filename, strerror(errno));
            exit(1);
        }
    }

    /* Check if the file is zero-length: if so return C_ERR to signal
     * we have to write the config. */
    if (fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) {
        fclose(fp);
        return C_ERR;
    }

    /* Parse the file. */
    maxline = 1024;
    line = zmalloc(maxline);
    while(fgets(line,maxline,fp) != NULL) {
        int argc;
        sds *argv;
        clusterNode *n;
        char *p, *s;

        /* Skip blank lines, they can be created either by users manually
         * editing nodes.conf or by the config writing process if stopped
         * before the truncate() call. */
        if (line[0] == '\n') continue;

        /* Split the line into arguments for processing. */
        argv = sdssplitargs(line,&argc);
        if (argv == NULL) goto fmterr;

        /* Handle the special "vars" line. Don't pretend it is the last
         * line even if it actually is when generated by Discnt. */
        if (strcasecmp(argv[0],"vars") == 0) {
            for (j = 1; j < argc; j += 2) {
                if (strcasecmp(argv[j],"someVarNameHere") == 0) {
                    /* TODO: currently not used. */
                } else {
                    serverLog(LL_WARNING,
                        "Skipping unknown cluster config variable '%s'",
                        argv[j]);
                }
            }
            sdsfreesplitres(argv,argc);
            continue;
        }

        /* Regular config lines have at least eight fields */
        if (argc != 6) goto fmterr;

        /* Create this node if it does not exist */
        if (strlen(argv[0]) != CLUSTER_NAMELEN) goto fmterr;
        n = clusterLookupNode(argv[0]);
        if (!n) {
            n = createClusterNode(argv[0],0);
            clusterAddNode(n);
        }
        /* Address and port */
        if ((p = strrchr(argv[1],':')) == NULL) goto fmterr;
        *p = '\0';
        memcpy(n->ip,argv[1],strlen(argv[1])+1);
        n->port = atoi(p+1);

        /* Parse flags */
        p = s = argv[2];
        while(p) {
            p = strchr(s,',');
            if (p) *p = '\0';
            if (!strcasecmp(s,"myself")) {
                serverAssert(server.cluster->myself == NULL);
                myself = server.cluster->myself = n;
                n->flags |= CLUSTER_NODE_MYSELF;
            } else if (!strcasecmp(s,"fail?")) {
                n->flags |= CLUSTER_NODE_PFAIL;
            } else if (!strcasecmp(s,"fail")) {
                n->flags |= CLUSTER_NODE_FAIL;
                n->fail_time = mstime();
            } else if (!strcasecmp(s,"handshake")) {
                n->flags |= CLUSTER_NODE_HANDSHAKE;
            } else if (!strcasecmp(s,"noaddr")) {
                n->flags |= CLUSTER_NODE_NOADDR;
            } else if (!strcasecmp(s,"noflags")) {
                /* nothing to do */
            } else {
                serverPanic("Unknown flag in discnt cluster config file");
            }
            if (p) s = p+1;
        }

        /* Set ping sent / pong received timestamps */
        if (atoi(argv[3])) n->ping_sent = mstime();
        if (atoi(argv[4])) n->pong_received = mstime();

        sdsfreesplitres(argv,argc);
    }
    /* Config sanity check */
    if (server.cluster->myself == NULL) goto fmterr;

    zfree(line);
    fclose(fp);

    serverLog(LL_NOTICE,"Node configuration loaded, I'm %.40s", myself->name);
    return C_OK;

fmterr:
    serverLog(LL_WARNING,
        "Unrecoverable error: corrupted cluster config file.");
    zfree(line);
    if (fp) fclose(fp);
    exit(1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
leaq 0x295be(%rip), %rsi     # 0x4e4a1
callq 0x124f0
testq %rax, %rax
je 0x24f1b
movq %rax, %rbp
movq %rax, %rdi
callq 0x12550
leaq 0x18(%rsp), %rsi
movl %eax, %edi
callq 0x12890
cmpl $-0x1, %eax
je 0x24f33
cmpq $0x0, 0x48(%rsp)
jne 0x24f33
movq %rbp, %rdi
callq 0x12220
jmp 0x24f2b
callq 0x120d0
movl (%rax), %edi
cmpl $0x2, %edi
jne 0x25272
pushq $-0x1
popq %rbx
jmp 0x251fb
movl $0x400, %edi            # imm = 0x400
callq 0x1ab29
movq %rax, %r12
leaq 0x2b20c(%rip), %rbx     # 0x50153
movq %rax, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbp, %rdx
callq 0x12460
testq %rax, %rax
je 0x251bd
cmpb $0xa, (%r12)
je 0x24f51
movq %r12, %rdi
leaq 0x4(%rsp), %rsi
callq 0x1a708
testq %rax, %rax
je 0x25229
movq %rax, %r15
movq (%rax), %r14
movq %r14, %rdi
leaq 0x2b179(%rip), %rsi     # 0x50110
callq 0x12090
testl %eax, %eax
je 0x25169
cmpl $0x6, 0x4(%rsp)
jne 0x25229
movq %r14, %rdi
callq 0x12250
cmpq $0x28, %rax
jne 0x25229
movq %r14, %rdi
callq 0x25290
movq %rax, %r14
testq %rax, %rax
jne 0x24fe6
movq (%r15), %rdi
xorl %esi, %esi
callq 0x252ba
movq %rax, %r14
movq %rax, %rdi
callq 0x2534c
movq 0x8(%r15), %rdi
pushq $0x3a
popq %rsi
callq 0x12310
testq %rax, %rax
je 0x25229
movq %rax, %r13
movb $0x0, (%rax)
leaq 0x50(%r14), %rbp
movq 0x8(%r15), %r12
movq %r12, %rdi
callq 0x12250
leaq 0x1(%rax), %rdx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x12510
incq %r13
movq %r13, %rdi
callq 0x12820
movl %eax, 0x80(%r14)
movq 0x10(%r15), %r13
movq %r13, %rbp
pushq $0x2c
popq %r12
testq %r13, %r13
je 0x2512d
movq %rbp, %rdi
movl %r12d, %esi
callq 0x122b0
movq %rax, %r13
testq %rax, %rax
je 0x2505e
movb $0x0, (%r13)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x12090
testl %eax, %eax
je 0x250d1
movq %rbp, %rdi
leaq 0x2b10c(%rip), %rsi     # 0x50183
callq 0x12090
testl %eax, %eax
je 0x250fa
movq %rbp, %rdi
leaq 0x2b0ff(%rip), %rsi     # 0x50189
callq 0x12090
testl %eax, %eax
je 0x25101
movq %rbp, %rdi
leaq 0x2b0f1(%rip), %rsi     # 0x5018e
callq 0x12090
testl %eax, %eax
je 0x25111
movq %rbp, %rdi
leaq 0x2b0e8(%rip), %rsi     # 0x50198
callq 0x12090
testl %eax, %eax
je 0x25118
movq %rbp, %rdi
leaq 0x2b0dc(%rip), %rsi     # 0x5019f
callq 0x12090
testl %eax, %eax
je 0x2511d
jmp 0x25252
leaq 0x44fb0(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
cmpq $0x0, (%rax)
jne 0x2520f
movq %r14, (%rax)
movq %r14, 0x594ad(%rip)     # 0x7e5a0
orb $0x4, 0x30(%r14)
jmp 0x2511d
orb $0x1, 0x30(%r14)
jmp 0x2511d
orb $0x2, 0x30(%r14)
callq 0x15db3
movq %rax, 0x48(%r14)
jmp 0x2511d
orb $0x8, 0x30(%r14)
jmp 0x2511d
orb $0x10, 0x30(%r14)
leaq 0x1(%r13), %rax
testq %r13, %r13
cmovneq %rax, %rbp
jmp 0x2503d
movq 0x18(%r15), %rdi
callq 0x12820
testl %eax, %eax
movq 0x8(%rsp), %r12
je 0x25148
callq 0x15db3
movq %rax, 0x38(%r14)
movq 0x20(%r15), %rdi
callq 0x12820
testl %eax, %eax
movq 0x10(%rsp), %rbp
je 0x25163
callq 0x15db3
movq %rax, 0x40(%r14)
movl 0x4(%rsp), %esi
jmp 0x251b0
pushq $0x1
popq %r12
leaq 0x2afa1(%rip), %r13     # 0x50115
movslq 0x4(%rsp), %rsi
cmpq %rsi, %r12
jge 0x251ab
movq (%r15,%r12,8), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x12090
testl %eax, %eax
je 0x251a5
pushq $0x3
popq %rdi
leaq 0x2af8a(%rip), %rsi     # 0x50125
movq %r14, %rdx
xorl %eax, %eax
callq 0x15b9e
addq $0x2, %r12
jmp 0x25174
movq 0x8(%rsp), %r12
movq %r15, %rdi
callq 0x1a53e
jmp 0x24f51
leaq 0x44ec4(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
cmpq $0x0, (%rax)
je 0x25229
movq %r12, %rdi
callq 0x1ac81
movq %rbp, %rdi
callq 0x12220
movq 0x593b8(%rip), %rdx     # 0x7e5a0
leaq 0x2afe5(%rip), %rsi     # 0x501d4
xorl %ebx, %ebx
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x15b9e
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2af44(%rip), %rdi     # 0x5015a
leaq 0x2af5c(%rip), %rsi     # 0x50179
movl $0xa5, %edx
callq 0x240ca
jmp 0x2526a
leaq 0x2afc9(%rip), %rsi     # 0x501f9
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x15b9e
movq %r12, %rdi
callq 0x1ac81
movq %rbp, %rdi
callq 0x12220
pushq $0x1
popq %rdi
callq 0x12860
leaq 0x2af4e(%rip), %rdi     # 0x501a7
leaq 0x2af19(%rip), %rsi     # 0x50179
movl $0xb4, %edx
callq 0x24359
pushq $0x1
popq %rdi
callq 0x12120
callq 0x128e0
leaq 0x2ae66(%rip), %rsi     # 0x500e4
pushq $0x3
popq %rdi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x15b9e
jmp 0x2524a