// File: /workspace/github_c_makefile_star2_O2/briandowns[P]pass/main.c
// Function: main
int main(int argc, char **argv) {
    if (argc < 2) {
        printf(USAGE, STR(app_name));
        return 1;
    }

    if (sodium_init() != 0) {
        return 1;
    }
    
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "version") == 0) {
            printf("version: %s - git: %s\n", STR(app_version), STR(git_sha));
            return 0;
        }

        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "help") == 0) {
            printf(USAGE, STR(bin_name));
            return 0;
        }
            
        char pass_dir_path[PATH_MAX];
        const char *pass_dir = getenv("PASS_DIR");
        if ((pass_dir != NULL) && (pass_dir[0] != 0)) {
            strcpy(pass_dir_path, pass_dir);
        } else {
            strcpy(pass_dir_path, getenv("HOME"));
            strcat(pass_dir_path, "/" CONFIG_DIR);
        }

        char key_file_path[PATH_MAX];
        const char *pass_key = getenv("PASS_KEY");
        if ((pass_key != NULL) && (pass_key[0] == 0)) {
            strcpy(key_file_path, pass_key);
        } else {
            strcpy(key_file_path, pass_dir_path);
            strcat(key_file_path, "/");
            strcat(key_file_path, KEY_NAME);
        }

        if (strcmp(argv[i], "init") == 0) {
            DIR* dir = opendir(pass_dir_path);
            if (ENOENT == errno) {
                mkdir(pass_dir_path, 0700);
            } else {
                closedir(dir);
            }
            
            if (access(key_file_path, F_OK ) != -1) {
                char answer;
                printf("overwrite existing key? [Y/n] ");
                if (scanf("%c", &answer) == EOF) {
                    fprintf(stderr, "error: failed to read user input\n");
                    return 1;
                }
                if (answer == 'Y') {
                    if (create_key(key_file_path) != 0) {
                        fprintf(stderr, "error: failed to generate key\n");
                        return 1;
                    }
                }
            } else {
                if (create_key(key_file_path) != 0) {
                    fprintf(stderr, "error: failed to create key\n");
                    return 1;
                }
            }

            return 0;
        }

        unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];

        FILE *key_fd = fopen(key_file_path, "r");
        fread(&key, crypto_secretstream_xchacha20poly1305_KEYBYTES, 1, key_fd);

        if ((strcmp(argv[i], "ls") == 0) || (strcmp(argv[i], "list") == 0)) {
            if (argc == 3) {
                PASSWORD_FILE_PATH;
                list(fp, 0);
            } else {
                list(pass_dir_path, 0);
            }

            break;
        }

        if (strcmp(argv[i], "set") == 0) {
            COMMAND_ARG_ERR_CHECK;

            char pass_buf[MAX_PASS_SIZE] = {0};
            strcpy(pass_buf, getpass("enter password: "));
            
            if (strlen(pass_buf) == 0) {
                fprintf(stderr, "error: empty passwords are not allowed\n");
                return 1;
            }

            PASSWORD_FILE_PATH;

            if (strchr(fp, '/') != NULL) {
                char *temp_fp = strdup(fp);
                mkdir_p(dirname(temp_fp));
                free(temp_fp);
            }

            if (encrypt_password(fp, pass_buf, key) != 0) {
                return 1;
            }

            break;
        }

        if (strcmp(argv[i], "get") == 0) {
            COMMAND_ARG_ERR_CHECK;
            PASSWORD_FILE_PATH;

            if (access(fp, F_OK ) != -1) {
                if (decrypt_password(fp, key) != 0) {
                    return 1;
                }
            } 

            break;
        }

        if ((strcmp(argv[i], "rm") == 0) || (strcmp(argv[i], "remove") == 0)) {
            COMMAND_ARG_ERR_CHECK;
            PASSWORD_FILE_PATH;

            remove(fp);

            break;
        }

        if (strcmp(argv[i], "config") == 0) {
            printf("path: %s\n", pass_dir_path);
            printf("key : %s\n", key_file_path);
            break;
        }

        if ((strcmp(argv[i], "gen") == 0) || (strcmp(argv[i], "generate") == 0)) {
            COMMAND_ARG_ERR_CHECK;

            int size = atoi(argv[2]);
            char *pass = generate_password(size);

            printf("%s\n", pass);
            free(pass);

            break;
        }

        if (strcmp(argv[i], "check") == 0) {
            COMMAND_ARG_ERR_CHECK;

            check(argv[2]);

            break;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5138, %rsp           # imm = 0x5138
pushq $0x1
popq %rbp
cmpl $0x1, %edi
jg 0x401b0b
leaq 0xe050c(%rip), %rdi     # 0x4e2004
leaq 0xe0756(%rip), %rsi     # 0x4e2255
xorl %eax, %eax
callq 0x432510
jmp 0x40228b
movq %rsi, %rbx
movl %edi, %r14d
callq 0x403e70
testl %eax, %eax
jne 0x40228b
movl %r14d, %eax
pushq $0x1
popq %r13
movq %rax, 0x8(%rsp)
cmpq %r13, %rax
je 0x402289
movq (%rbx,%r13,8), %r15
movb (%r15), %r12b
cmpb $0x2d, %r12b
jne 0x401b52
cmpb $0x76, 0x1(%r15)
jne 0x401b52
cmpb $0x0, 0x2(%r15)
je 0x401dd7
movq %r15, %rdi
leaq 0xe068d(%rip), %rsi     # 0x4e21e9
callq 0x4010b8
testl %eax, %eax
je 0x401dd7
cmpb $0x2d, %r12b
jne 0x401b81
cmpb $0x68, 0x1(%r15)
jne 0x401b81
cmpb $0x0, 0x2(%r15)
je 0x401dfa
movq %r15, %rdi
leaq 0xe06ac(%rip), %rsi     # 0x4e2237
callq 0x4010b8
testl %eax, %eax
je 0x401dfa
leaq 0xe06a6(%rip), %rdi     # 0x4e2245
callq 0x430900
testq %rax, %rax
je 0x401bc0
cmpb $0x0, (%rax)
je 0x401bc0
leaq 0x3130(%rsp), %rdi
movq %rax, %rsi
callq 0x401020
jmp 0x401bfd
leaq 0xe0687(%rip), %rdi     # 0x4e224e
callq 0x430900
leaq 0x3130(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x3130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x3133(%rsp,%rax) # imm = 0x737361
leaq 0xe0656(%rip), %rdi     # 0x4e225a
callq 0x430900
testq %rax, %rax
je 0x401c13
cmpb $0x0, (%rax)
je 0x401c63
leaq 0x2130(%rsp), %r12
movq %r12, %rdi
leaq 0x3130(%rsp), %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movw $0x2f, 0x2130(%rsp,%rax)
movq %r12, %rdi
callq 0x4010e8
movabsq $0x656b2e737361702e, %rcx # imm = 0x656B2E737361702E
movq %rcx, 0x2130(%rsp,%rax)
movw $0x79, 0x2138(%rsp,%rax)
jmp 0x401c73
leaq 0x2130(%rsp), %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
leaq 0xf0595(%rip), %rsi     # 0x4f2212
callq 0x4010b8
testl %eax, %eax
je 0x401f34
leaq 0x2130(%rsp), %rdi
leaq 0x1047ce(%rip), %rsi    # 0x506467
callq 0x4413f0
leaq 0x10(%rsp), %rdi
pushq $0x20
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x4414f0
movq (%rbx,%r13,8), %r15
movb (%r15), %r12b
cmpb $0x6c, %r12b
jne 0x401cd0
cmpb $0x73, 0x1(%r15)
jne 0x401cd0
cmpb $0x0, 0x2(%r15)
je 0x401e0f
movq %r15, %rdi
leaq 0xec085(%rip), %rsi     # 0x4edd5f
callq 0x4010b8
testl %eax, %eax
je 0x401e0f
movq %r15, %rdi
leaq 0x104d65(%rip), %rsi    # 0x506a56
callq 0x4010b8
testl %eax, %eax
je 0x401f69
movq %r15, %rdi
leaq 0xe063b(%rip), %rsi     # 0x4e2343
callq 0x4010b8
testl %eax, %eax
je 0x4020bf
cmpb $0x72, %r12b
jne 0x401d2d
cmpb $0x6d, 0x1(%r15)
jne 0x401d2d
cmpb $0x0, 0x2(%r15)
je 0x401e9f
movq %r15, %rdi
leaq 0xe0610(%rip), %rsi     # 0x4e2347
callq 0x4010b8
testl %eax, %eax
je 0x401e9f
movq %r15, %rdi
leaq 0xe0600(%rip), %rsi     # 0x4e234e
callq 0x4010b8
testl %eax, %eax
je 0x402178
movq %r15, %rdi
leaq 0xe0604(%rip), %rsi     # 0x4e2369
callq 0x4010b8
testl %eax, %eax
je 0x401da5
movq %r15, %rdi
leaq 0xe05f5(%rip), %rsi     # 0x4e236d
callq 0x4010b8
testl %eax, %eax
je 0x401da5
movq %r15, %rdi
leaq 0xe05eb(%rip), %rsi     # 0x4e2376
callq 0x4010b8
testl %eax, %eax
je 0x4021a9
incq %r13
movq 0x8(%rsp), %rax
jmp 0x401b2a
cmpl $0x3, %r14d
jne 0x4022c5
movq 0x10(%rbx), %rdi
callq 0x4300d0
movl %eax, %edi
callq 0x40261a
movq %rax, %rbx
movq %rax, %rdi
callq 0x441a70
movq %rbx, %rdi
callq 0x450ea0
jmp 0x402289
leaq 0xe0413(%rip), %rdi     # 0x4e21f1
leaq 0xe0423(%rip), %rsi     # 0x4e2208
leaq 0xe0422(%rip), %rdx     # 0x4e220e
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x432510
jmp 0x40228b
leaq 0xe0203(%rip), %rdi     # 0x4e2004
leaq 0xe0434(%rip), %rsi     # 0x4e223c
xorl %ebp, %ebp
jmp 0x401aff
cmpl $0x3, %r14d
jne 0x4021c1
leaq 0x1130(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe0417(%rip), %rdi     # 0x4e224e
callq 0x430900
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r14, %rdi
movq %r15, %rsi
callq 0x401020
movq %r14, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r13,8), %rsi
movq %r14, %rdi
callq 0x401060
movq %r14, %rdi
jmp 0x4021c9
cmpl $0x3, %r14d
jne 0x4022c5
leaq 0x1130(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe0387(%rip), %rdi     # 0x4e224e
callq 0x430900
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r14, %rdi
movq %r15, %rsi
callq 0x401020
movq %r14, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r13,8), %rsi
movq %r14, %rdi
callq 0x401060
movq %r14, %rdi
callq 0x4328c0
jmp 0x402289
leaq 0x3130(%rsp), %rdi
callq 0x484a60
movq %rax, %rbx
callq 0x42a420
cmpl $0x2, (%rax)
jne 0x40221f
leaq 0x3130(%rsp), %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x485760
jmp 0x402227
cmpl $0x3, %r14d
jne 0x4022c5
leaq 0x1130(%rsp), %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe0379(%rip), %rdi     # 0x4e230a
callq 0x486900
movq %r14, %rdi
movq %rax, %rsi
callq 0x401020
cmpb $0x0, (%r14)
je 0x4022e2
leaq 0x130(%rsp), %r14
xorl %ebp, %ebp
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe0283(%rip), %rdi     # 0x4e224e
callq 0x430900
leaq 0x4130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x4130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x4133(%rsp,%rax) # imm = 0x737361
movq %r14, %rdi
movq %r15, %rsi
callq 0x401020
movq %r14, %rdi
callq 0x4010e8
movw $0x2f, 0x130(%rsp,%rax)
movq 0x8(%rbx,%r13,8), %rsi
movq %r14, %rdi
callq 0x401060
pushq $0x2f
popq %rsi
movq %r14, %rdi
callq 0x4010f0
testq %rax, %rax
je 0x4021f1
leaq 0x130(%rsp), %rdi
callq 0x453250
movq %rax, %rbx
movq %rax, %rdi
callq 0x488400
leaq 0x1055b3(%rip), %rdx    # 0x507611
leaq 0x30(%rsp), %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x4325e0
movq %r14, %rdi
callq 0x4010e8
movzbl -0x1(%rax,%r14), %ecx
cmpb $0x2f, %cl
cmovel %ebp, %ecx
movb %cl, -0x1(%rax,%r14)
leaq 0x31(%rsp), %r15
movzbl (%r15), %eax
cmpl $0x2f, %eax
je 0x4020a5
testl %eax, %eax
jne 0x4020ba
jmp 0x4021da
movb $0x0, (%r15)
movq %r14, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x485760
movb $0x2f, (%r15)
incq %r15
jmp 0x402093
cmpl $0x3, %r14d
jne 0x4022c5
leaq 0x1130(%rsp), %r14
xorl %ebp, %ebp
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe0165(%rip), %rdi     # 0x4e224e
callq 0x430900
leaq 0x130(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r14, %rdi
movq %r15, %rsi
callq 0x401020
movq %r14, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r13,8), %rsi
movq %r14, %rdi
callq 0x401060
movq %r14, %rdi
xorl %esi, %esi
callq 0x485a30
cmpl $-0x1, %eax
je 0x4021d5
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4024bf
testl %eax, %eax
je 0x4021d5
pushq $0x1
popq %rax
movl %eax, %ebp
jmp 0x402218
leaq 0xe01d6(%rip), %rdi     # 0x4e2355
leaq 0x3130(%rsp), %rsi
xorl %eax, %eax
callq 0x432510
leaq 0xe01ca(%rip), %rdi     # 0x4e235f
leaq 0x2130(%rsp), %rsi
xorl %eax, %eax
callq 0x432510
jmp 0x402289
cmpl $0x3, %r14d
jne 0x4022c5
movq 0x10(%rbx), %rdi
callq 0x402679
jmp 0x402289
leaq 0x3130(%rsp), %rdi
xorl %esi, %esi
callq 0x40233c
jmp 0x402289
pushq $0x2
popq %rax
jmp 0x402218
leaq 0x30(%rsp), %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x485760
movq %rbx, %rdi
callq 0x450ea0
leaq 0x130(%rsp), %rdi
leaq 0x1130(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x402408
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
pushq $0x2
popq %rax
subl %ebp, %eax
cmpl $0x2, %eax
je 0x402289
jmp 0x40228b
movq %rbx, %rdi
callq 0x484c90
leaq 0x2130(%rsp), %rdi
xorl %ebp, %ebp
xorl %esi, %esi
callq 0x485a30
cmpl $-0x1, %eax
je 0x40229f
leaq 0xe0029(%rip), %rdi     # 0x4e226d
xorl %eax, %eax
callq 0x432510
leaq 0xe003a(%rip), %rdi     # 0x4e228c
leaq 0x1130(%rsp), %rsi
xorl %eax, %eax
callq 0x432900
cmpl $-0x1, %eax
je 0x402303
cmpb $0x59, 0x1130(%rsp)
jne 0x402289
leaq 0x2130(%rsp), %rdi
callq 0x4025cd
testl %eax, %eax
jne 0x402318
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x5138, %rsp           # imm = 0x5138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2130(%rsp), %rdi
callq 0x4025cd
testl %eax, %eax
je 0x40228b
movq $0x520a68, %rax         # imm = 0x520A68
movq (%rax), %rcx
leaq 0xe000f(%rip), %rdi     # 0x4e22d0
pushq $0x1c
jmp 0x40232b
movq $0x520a68, %rax         # imm = 0x520A68
movq (%rax), %rdi
leaq 0xe0017(%rip), %rsi     # 0x4e22ed
movq %r15, %rdx
xorl %eax, %eax
callq 0x432450
jmp 0x40228b
movq $0x520a68, %rax         # imm = 0x520A68
movq (%rax), %rcx
leaq 0xe0028(%rip), %rdi     # 0x4e231b
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x4415f0
jmp 0x40216e
movq $0x520a68, %rax         # imm = 0x520A68
movq (%rax), %rcx
leaq 0xdff7b(%rip), %rdi     # 0x4e228f
pushq $0x21
jmp 0x40232b
movq $0x520a68, %rax         # imm = 0x520A68
movq (%rax), %rcx
leaq 0xdff88(%rip), %rdi     # 0x4e22b1
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x4415f0
pushq $0x1
popq %rbp
jmp 0x40228b