#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

const uint16_t NBR_PASSWORDS_REQUESTED = 16;

const char list[] = {
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '/', '+'
};
char give_char(uint8_t index){
    if (index >= sizeof(list)) {
        printf("Error: Index out of bounds, max %zu\n", sizeof(list));
        exit(1);
    }
    return list[index];
}

bool check_pw(char *pw) {
    int64_t xor = pw[0]^pw[1]^pw[2]^pw[3]^pw[4]^pw[5]^pw[6]^pw[7]^pw[8]^pw[9]^pw[10]^pw[11];
    int64_t sum = pw[0]+pw[1]+pw[2]+pw[3]+pw[4]+pw[5]+pw[6]+pw[7]+pw[8]+pw[9]+pw[10]+pw[11];
    int64_t iVar1 = (int64_t)(uint8_t)(xor^0xD6);
    int64_t iVar2 = (int64_t)(uint8_t)(sum-0x3a);
    int64_t iVar8 = iVar1 + iVar2 * 0x73;

    int64_t diff = (char)(
        (char)iVar8 - (char)( (int64_t)(iVar8 * 0x7f81) >> 0x17 )
    );
    // printf("XOR: %d\tSUM: %d\n", xor, sum);
    // printf("iVar1: %d\tiVar2: %d\tiVar8: %d\n", iVar1, iVar2, iVar8);
    // printf("Diff is: %d\n", diff);
    if (diff == -0x4b) { // -75 | -'K' | 18'446'744'073'709'551'541
        return true;
    } else {
        return false;
    }
}

int main(int argc, char *argv[]) {
    srand(time(NULL));

    char password[13];
    const uint8_t max_char = sizeof(list);

    password[0] = 'r';
    password[1] = 'e';
    password[2] = 'm';
    password[3] = 'i';
    password[4] = 'p';
    password[5] = 'a';
    password[6] = 's';
    password[7] = 's';
    password[8] = 'w';

    password[12] = '\0';

    for(uint16_t n = 0; n < NBR_PASSWORDS_REQUESTED; n++) {

        do{
            password[9] = give_char(rand() % max_char);
            password[10] = give_char(rand() % max_char);
            password[11] = give_char(rand() % max_char);
        }while(!check_pw(password));

        if (check_pw(password)) {
            printf("%s\n", password);
        } else {
            printf("Fuck\n");
        }

    }
    return 0;
}
