// File: /workspace/github_c_makefile_star2_O1/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
cmpl $0x1, %edi
jg 0x401bab
leaq 0xe046f(%rip), %rdi     # 0x4e2004
leaq 0xe06bf(%rip), %rsi     # 0x4e225b
xorl %eax, %eax
callq 0x432690
movl $0x1, %r14d
jmp 0x401bbf
movq %rsi, %rbx
movl %edi, %ebp
callq 0x403ff0
movl $0x1, %r14d
testl %eax, %eax
je 0x401bd4
movl %r14d, %eax
addq $0x5138, %rsp           # imm = 0x5138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebp, (%rsp)
movl %ebp, %eax
movq %rax, 0x8(%rsp)
movl $0x1, %r13d
leaq 0x3130(%rsp), %r12
xorl %r14d, %r14d
movq (%rbx,%r13,8), %r15
movq %r15, %rdi
leaq 0xe05ec(%rip), %rsi     # 0x4e21e9
callq 0x4010b8
testl %eax, %eax
je 0x4023d6
movq %r15, %rdi
leaq 0xe05d8(%rip), %rsi     # 0x4e21ec
callq 0x4010b8
testl %eax, %eax
je 0x4023d6
movq %r15, %rdi
leaq 0xe060f(%rip), %rsi     # 0x4e223a
callq 0x4010b8
testl %eax, %eax
je 0x4023fa
movq %r15, %rdi
leaq 0xe05fb(%rip), %rsi     # 0x4e223d
callq 0x4010b8
testl %eax, %eax
je 0x4023fa
leaq 0xe05f5(%rip), %rdi     # 0x4e224b
callq 0x430a80
testq %rax, %rax
je 0x401c77
cmpb $0x0, (%rax)
je 0x401c77
leaq 0x2130(%rsp), %rdi
movq %rax, %rsi
callq 0x401020
jmp 0x401cbc
leaq 0xe05d6(%rip), %rdi     # 0x4e2254
callq 0x430a80
leaq 0x2130(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x401020
movq %r12, %rdi
leaq 0x3130(%rsp), %r12
callq 0x4010e8
movl $0x61702e2f, 0x2130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x2133(%rsp,%rax) # imm = 0x737361
leaq 0xe059d(%rip), %rdi     # 0x4e2260
callq 0x430a80
testq %rax, %rax
je 0x401cd2
cmpb $0x0, (%rax)
je 0x401d1a
movq %r12, %rdi
leaq 0x2130(%rsp), %rsi
callq 0x401020
movq %r12, %rdi
callq 0x4010e8
movw $0x2f, 0x3130(%rsp,%rax)
movq %r12, %rdi
callq 0x4010e8
movabsq $0x656b2e737361702e, %rcx # imm = 0x656B2E737361702E
movq %rcx, 0x3130(%rsp,%rax)
movw $0x79, 0x3138(%rsp,%rax)
jmp 0x401d25
movq %r12, %rdi
movq %rax, %rsi
callq 0x401020
movq %r15, %rdi
leaq 0xf0523(%rip), %rsi     # 0x4f2252
callq 0x4010b8
testl %eax, %eax
je 0x401f05
movq %r12, %rdi
leaq 0x104761(%rip), %rsi    # 0x5064a7
callq 0x441570
movl $0x20, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %rax, %rcx
callq 0x441670
movq (%rbx,%r13,8), %r15
movq %r15, %rdi
leaq 0xe0583(%rip), %rsi     # 0x4e22f3
callq 0x4010b8
testl %eax, %eax
je 0x401e66
movq %r15, %rdi
leaq 0xec018(%rip), %rsi     # 0x4edd9f
callq 0x4010b8
testl %eax, %eax
je 0x401e66
movq %r15, %rdi
leaq 0x104cf8(%rip), %rsi    # 0x506a96
callq 0x4010b8
testl %eax, %eax
je 0x401fdf
movq %r15, %rdi
leaq 0xe0597(%rip), %rsi     # 0x4e234c
callq 0x4010b8
testl %eax, %eax
je 0x4021e5
movq %r15, %rdi
leaq 0xe0584(%rip), %rsi     # 0x4e2350
callq 0x4010b8
testl %eax, %eax
je 0x40213e
movq %r15, %rdi
leaq 0xe0570(%rip), %rsi     # 0x4e2353
callq 0x4010b8
testl %eax, %eax
je 0x40213e
movq %r15, %rdi
leaq 0xe0560(%rip), %rsi     # 0x4e235a
callq 0x4010b8
testl %eax, %eax
je 0x402337
movq %r15, %rdi
leaq 0xe0564(%rip), %rsi     # 0x4e2375
callq 0x4010b8
testl %eax, %eax
je 0x402309
movq %r15, %rdi
leaq 0xe0551(%rip), %rsi     # 0x4e2379
callq 0x4010b8
testl %eax, %eax
je 0x402309
movq %r15, %rdi
leaq 0xe0543(%rip), %rsi     # 0x4e2382
callq 0x4010b8
xorl %ecx, %ecx
testl %eax, %eax
jne 0x4023b1
cmpl $0x3, (%rsp)
jne 0x40236a
movq 0x10(%rbx), %rdi
callq 0x4027a5
jmp 0x401f40
cmpl $0x3, (%rsp)
jne 0x401f31
movl $0x1000, %edx           # imm = 0x1000
leaq 0x1130(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe03c6(%rip), %rdi     # 0x4e2254
callq 0x430a80
movq %r12, %rbp
movl %r14d, %r12d
movq %rbx, %r14
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x401020
movq %rbx, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rbx
movl %r12d, %r14d
movq %rbp, %r12
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r13,8), %rsi
movq %r15, %rdi
callq 0x401060
movq %r15, %rdi
jmp 0x401f39
leaq 0x2130(%rsp), %r12
movq %r12, %rdi
callq 0x484be0
movq %rax, %r15
callq 0x42a5a0
cmpl $0x2, (%rax)
jne 0x401f4a
movq %r12, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4858e0
jmp 0x401f52
leaq 0x2130(%rsp), %rdi
xorl %esi, %esi
callq 0x402417
movl $0x2, %ecx
jmp 0x4023b1
movq %r15, %rdi
callq 0x484e10
leaq 0x3130(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x485bb0
cmpl $-0x1, %eax
je 0x401fc2
leaq 0xe0303(%rip), %rdi     # 0x4e2273
xorl %eax, %eax
callq 0x432690
leaq 0xe0314(%rip), %rdi     # 0x4e2292
leaq 0x1130(%rsp), %rsi
xorl %eax, %eax
callq 0x432a80
cmpl $-0x1, %eax
je 0x402363
movb $0x1, %r15b
cmpb $0x59, 0x1130(%rsp)
jne 0x401fb3
movq %r12, %rdi
callq 0x4026e9
testl %eax, %eax
jne 0x40238e
movl $0x1, %ecx
testb %r15b, %r15b
jne 0x401fd2
jmp 0x4023b1
movq %r12, %rdi
callq 0x4026e9
testl %eax, %eax
jne 0x402387
xorl %r14d, %r14d
movl $0x1, %ecx
jmp 0x4023b1
cmpl $0x3, (%rsp)
jne 0x40236a
movl $0x1000, %edx           # imm = 0x1000
leaq 0x1130(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe030c(%rip), %rdi     # 0x4e2313
callq 0x486a80
movq %r15, %rdi
movq %rax, %rsi
callq 0x401020
cmpb $0x0, 0x1130(%rsp)
je 0x4023a1
movl %r14d, 0x4(%rsp)
movl $0x1000, %edx           # imm = 0x1000
leaq 0x130(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe020c(%rip), %rdi     # 0x4e2254
callq 0x430a80
leaq 0x4130(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x401020
movq %r14, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x4130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x4133(%rsp,%rax) # imm = 0x737361
movq %r15, %rdi
movq %r14, %rsi
leaq 0x30(%rsp), %r14
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movw $0x2f, 0x130(%rsp,%rax)
movq 0x8(%rbx,%r13,8), %rsi
movq %r15, %rdi
callq 0x401060
movq %r15, %rdi
movl $0x2f, %esi
callq 0x4010f0
testq %rax, %rax
je 0x4022d8
movq %r15, %rdi
callq 0x4533d0
movq %rax, %rbp
movq %rax, %rdi
callq 0x488580
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
leaq 0x10556c(%rip), %rdx    # 0x507651
movq %rax, %rcx
xorl %eax, %eax
callq 0x432760
movq %r14, %rdi
callq 0x4010e8
movzbl -0x1(%rax,%r14), %ecx
cmpb $0x2f, %cl
movl $0x0, %edx
cmovel %edx, %ecx
movb %cl, -0x1(%rax,%r14)
leaq 0x31(%rsp), %r15
movzbl (%r15), %eax
cmpl $0x2f, %eax
je 0x402124
testl %eax, %eax
jne 0x402139
jmp 0x4022bb
movb $0x0, (%r15)
movq %r14, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4858e0
movb $0x2f, (%r15)
incq %r15
jmp 0x402112
cmpl $0x3, (%rsp)
jne 0x40236a
movl $0x1000, %edx           # imm = 0x1000
leaq 0x1130(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe00ee(%rip), %rdi     # 0x4e2254
callq 0x430a80
movq %r12, %rbp
movl %r14d, %r12d
movq %rbx, %r14
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x401020
movq %rbx, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rbx
movl %r12d, %r14d
movq %rbp, %r12
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%rbx,%r13,8), %rsi
movq %r15, %rdi
callq 0x401060
movq %r15, %rdi
callq 0x432a40
jmp 0x401f40
cmpl $0x3, (%rsp)
jne 0x40236a
movl %r14d, 0x4(%rsp)
movl $0x1000, %edx           # imm = 0x1000
leaq 0x1130(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x4010a8
leaq 0xe0042(%rip), %rdi     # 0x4e2254
callq 0x430a80
movq %rbx, %r14
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x401020
movq %rbx, %rdi
callq 0x4010e8
movl $0x61702e2f, 0x130(%rsp,%rax) # imm = 0x61702E2F
movl $0x737361, 0x133(%rsp,%rax) # imm = 0x737361
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rbx
callq 0x401020
movq %r15, %rdi
callq 0x4010e8
movw $0x2f, 0x1130(%rsp,%rax)
movq 0x8(%r14,%r13,8), %rsi
movq %r15, %rdi
callq 0x401060
movq %r15, %rdi
xorl %esi, %esi
callq 0x485bb0
cmpl $-0x1, %eax
je 0x4022ac
leaq 0x1130(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x4025d6
movl $0x1, %ecx
movl $0x1, %r14d
testl %eax, %eax
jne 0x4023b1
movl $0x2, %ecx
movl 0x4(%rsp), %r14d
jmp 0x4023b1
movq %r14, %rdi
movl $0x1c0, %esi            # imm = 0x1C0
callq 0x4858e0
movq %rbp, %rdi
callq 0x451020
leaq 0x130(%rsp), %r15
movq %r15, %rdi
leaq 0x1130(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x402524
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movl $0x1, %eax
movl 0x4(%rsp), %r14d
cmovnel %eax, %r14d
incl %ecx
jmp 0x4023b1
cmpl $0x3, (%rsp)
jne 0x40236a
movq 0x10(%rbx), %rdi
callq 0x430250
movl %eax, %edi
callq 0x40273c
movq %rax, %r15
movq %rax, %rdi
callq 0x441bf0
movq %r15, %rdi
callq 0x451020
jmp 0x401f40
leaq 0xe0023(%rip), %rdi     # 0x4e2361
leaq 0x2130(%rsp), %rsi
xorl %eax, %eax
callq 0x432690
leaq 0xe0017(%rip), %rdi     # 0x4e236b
movq %r12, %rsi
xorl %eax, %eax
callq 0x432690
jmp 0x401f40
callq 0x401144
jmp 0x402393
movq $0x520a68, %rax         # imm = 0x520A68
movq (%rax), %rdi
leaq 0xdff7b(%rip), %rsi     # 0x4e22f6
movq %r15, %rdx
xorl %eax, %eax
callq 0x4325d0
jmp 0x4023a6
callq 0x401166
jmp 0x4023a6
callq 0x401122
movl $0x1, %r14d
xorl %r15d, %r15d
jmp 0x401fb3
callq 0x401100
movl $0x1, %ecx
movl $0x1, %r14d
testl %ecx, %ecx
jne 0x4023c8
incq %r13
cmpq 0x8(%rsp), %r13
jne 0x401bef
movl $0x2, %ecx
xorl %eax, %eax
cmpl $0x2, %ecx
cmovel %eax, %r14d
jmp 0x401bbf
leaq 0xdfe17(%rip), %rdi     # 0x4e21f4
leaq 0xdfe27(%rip), %rsi     # 0x4e220b
leaq 0xdfe26(%rip), %rdx     # 0x4e2211
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x432690
jmp 0x401bbf
leaq 0xdfc03(%rip), %rdi     # 0x4e2004
leaq 0xdfe3a(%rip), %rsi     # 0x4e2242
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x432690
jmp 0x401bbf