// File: /workspace/github_cpp_makefile_star2_O2/Leeingnyo[P]newPuyoC/game.cpp
// Function: VSRemoteGame::GameInit()
bool VSRemoteGame::GameInit() {
    int input;

    Console::ScreenClear();
    std::cout << Console::GotoXY(X(0), X(0));
    std::cout << "하실 것을 골라주세요" << std::endl;
    std::cout << "0. 돌아가기" << std::endl;
    std::cout << "1. 서버" << std::endl;
    std::cout << "2. 클라" << std::endl;
    input = getch();

    switch (input) {
        case '0': return false;
        case '1': break;
        case '2': break;
        default: return false;
    }

    Console::ScreenClear();
    // 서버 인지 클라 인지
    int seed;
    if (input == '1') {
        socket = std::make_shared<ServerSocket>();
        auto server_socket = std::dynamic_pointer_cast<ServerSocket>(socket);
        // 서버 일 경우 연결을 만들고 자기 아이피 주소를 보여줌
        std::cout << Console::white
            << "안녕하세요. 당신은 서버 역할입니다." << std::endl
            << "클라이언트 역할의 분께 당신의 IP를 알려드려야 합니다." << std::endl
            << "혹시 공유기를 사용하시고 계시다면 포트 포워딩이 필요합니다." << std::endl
            << "ip는 http://www.myipaddress.com/show-my-ip-address/ 나" << std::endl
            << "shell 에서 (linux, MaxOS) ifconfig 명령어, 혹은 (Windows) ipconfig 명령어 를 사용하시면 됩니다." << std::endl
            << "포트는 9595를 사용하므로 열려있으면 닫아주세요" << std::endl;
        if (server_socket->Bind("0.0.0.0", 9595)) {
            std::cout << Console::red
                << "9595 포트에 Bind 실패했습니다. 이미 사용 중인지 확인해주세요." << std::endl
                << "3초 뒤 화면으로 돌아갑니다." << std::endl;
            Console::Sleep(3000);
            return false;
        }
        if (server_socket->Listen(5)) {
            std::cout << Console::red
                << "Listen 실패했습니다." << std::endl
                << "3초 뒤 화면으로 돌아갑니다." << std::endl;
            Console::Sleep(3000);
            return false;
        }
        if (server_socket->Accept()) {
            std::cout << Console::red
                << "Accept 실패했습니다." << std::endl
                << "3초 뒤 화면으로 돌아갑니다." << std::endl;
            Console::Sleep(3000);
            return false;
        }

        int generated_seed = rand();
        server_socket->Send(std::to_string(generated_seed));
        seed = generated_seed;
    }
    else {
        socket = std::make_shared<ClientSocket>();
        auto client_socket = std::dynamic_pointer_cast<ClientSocket>(socket);
        // 클라일 경우 IP 주소 입력받게 함
        std::string ip_address;
        std::cout << Console::white
            << "안녕하세요. 당신은 클라이언트 역할입니다." << std::endl
            << "서버 역할의 분께서 당신에게 IP를 알려드릴 겁니다." << std::endl
            << "그것을 입력해주시기 바랍니다." << std::endl
            << "예시) 127.0.0.1" << std::endl;
        std::cin >> ip_address;
        if (client_socket->Connect(ip_address, 9595)) {
            std::cout << Console::red
                << "Connect 실패했습니다. 올바른 IP인지 확인해주세요." << std::endl
                << "3초 뒤 화면으로 돌아갑니다." << std::endl;
            Console::Sleep(3000);
            return false;
        }
        seed = std::stoi(client_socket->Recv());
    }

    my_bipuyo_generator = std::make_shared<BiPuyoGenerator>(seed);
    my_board = std::make_shared<Board>();
    my_board->SetNextBiPuyo(my_bipuyo_generator->GenerateBipuyo());
    my_next_bipuyo = my_bipuyo_generator->GenerateBipuyo();

    other_board = std::make_shared<Board>();
    other_board->SetNextBiPuyo(BiPuyoGenerator::GenerateEmptyBipuyo());
    other_next_bipuyo = BiPuyoGenerator::GenerateEmptyBipuyo();

    Console::ScreenClear();
    std::cout << Console::GotoXY(X(0), X(0));
    std::cout << "Loading..." << std::endl;
    Console::Sleep(500);
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
callq 0x3bca
leaq -0x38(%rbp), %rsi
andq $0x0, (%rsi)
movq 0xb587(%rip), %r14      # 0xffc8
movq %r14, %rdi
callq 0x3c46
leaq 0x57f9(%rip), %rsi      # 0xa249
movq %r14, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5800(%rip), %rsi      # 0xa267
movq %r14, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x57f9(%rip), %rsi      # 0xa277
movq %r14, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x57ec(%rip), %rsi      # 0xa281
movq %r14, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
callq 0x3b49
movl %eax, %r15d
leal -0x31(%r15), %eax
cmpl $0x1, %eax
ja 0x4f4b
callq 0x3bca
leaq 0x90(%rbx), %r14
cmpl $0x31, %r15d
jne 0x4bff
leaq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x5fd8
movq %r14, %rdi
movq %r15, %rsi
callq 0x8ee2
leaq -0x30(%rbp), %rdi
callq 0x6896
leaq -0x38(%rbp), %rdi
movq %r14, %rsi
callq 0x5ff4
movq 0xb4c5(%rip), %rdi      # 0xffc8
leaq 0xb7b2(%rip), %rsi      # 0x102bc
callq 0x3c12
leaq 0x5775(%rip), %rsi      # 0xa28b
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5791(%rip), %rsi      # 0xa2be
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x57c6(%rip), %rsi      # 0xa30a
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5805(%rip), %rsi      # 0xa360
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5827(%rip), %rsi      # 0xa399
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5883(%rip), %rsi      # 0xa40c
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
movq -0x38(%rbp), %r14
leaq 0x58aa(%rip), %rsi      # 0xa44e
leaq -0xd0(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq 0x3100
leaq -0xd0(%rbp), %rsi
movq %r14, %rdi
movl $0x257b, %edx           # imm = 0x257B
callq 0x3744
movl %eax, %r14d
leaq -0xd0(%rbp), %rdi
callq 0x3140
testl %r14d, %r14d
je 0x4ebe
movq 0xb3e1(%rip), %rdi      # 0xffc8
leaq 0xb6c2(%rip), %rsi      # 0x102b0
callq 0x3c12
leaq 0x585c(%rip), %rsi      # 0xa456
jmp 0x4f11
leaq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x60a9
movq %r14, %rdi
movq %r15, %rsi
callq 0x9102
leaq -0x30(%rbp), %rdi
callq 0x6896
leaq -0x50(%rbp), %rdi
movq %r14, %rsi
callq 0x60c5
leaq -0x28(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0xb386(%rip), %rdi      # 0xffc8
leaq 0xb673(%rip), %rsi      # 0x102bc
callq 0x3c12
leaq 0x58b2(%rip), %rsi      # 0xa507
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x58d7(%rip), %rsi      # 0xa543
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5906(%rip), %rsi      # 0xa589
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x591a(%rip), %rsi      # 0xa5b4
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
movq 0xb32f(%rip), %rdi      # 0xffe0
leaq -0x38(%rbp), %rsi
callq 0x3340
movq -0x50(%rbp), %r14
leaq -0x90(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x3370
leaq -0x90(%rbp), %rsi
movq %r14, %rdi
movl $0x257b, %edx           # imm = 0x257B
callq 0x38f0
movl %eax, %r14d
leaq -0x90(%rbp), %rdi
callq 0x3140
testl %r14d, %r14d
je 0x4d43
movq 0xb2cb(%rip), %rdi      # 0xffc8
leaq 0xb5ac(%rip), %rsi      # 0x102b0
callq 0x3c12
leaq 0x58b6(%rip), %rsi      # 0xa5c6
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5783(%rip), %rsi      # 0xa4aa
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
movl $0xbb8, %edi            # imm = 0xBB8
callq 0x3bba
jmp 0x4d6b
movq -0x50(%rbp), %rsi
movq (%rsi), %rax
leaq -0x70(%rbp), %rdi
callq *0x10(%rax)
leaq -0x70(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x611a
movl %eax, -0x3c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3140
leaq -0x38(%rbp), %rdi
callq 0x3140
leaq -0x48(%rbp), %rdi
callq 0x6896
testl %r14d, %r14d
jne 0x4f4b
leaq -0x38(%rbp), %r14
leaq -0x3c(%rbp), %rsi
movq %r14, %rdi
callq 0x5fb9
leaq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x8876
leaq -0x30(%rbp), %rdi
callq 0x6896
movq %r14, %rdi
callq 0x5cb7
leaq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x8a9a
leaq -0x30(%rbp), %rdi
callq 0x6896
movq 0x20(%rbx), %r14
movq 0x10(%rbx), %rsi
leaq -0x38(%rbp), %r15
movq %r15, %rdi
callq 0x5d00
movq %r14, %rdi
movq %r15, %rsi
callq 0x5cd4
leaq -0x30(%rbp), %rdi
callq 0x6896
movq 0x10(%rbx), %rsi
leaq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x5d00
leaq 0x30(%rbx), %rdi
movq %r14, %rsi
callq 0x81ec
leaq -0x30(%rbp), %rdi
callq 0x6896
movq %r14, %rdi
callq 0x5cb7
leaq 0x60(%rbx), %rdi
movq %r14, %rsi
callq 0x8a9a
leaq -0x30(%rbp), %rdi
callq 0x6896
movq 0x60(%rbx), %r14
movq %r15, %rdi
callq 0x613c
movq %r14, %rdi
movq %r15, %rsi
callq 0x5cd4
leaq -0x30(%rbp), %rdi
callq 0x6896
leaq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x613c
addq $0x70, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x81ec
leaq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x6896
callq 0x3bca
andq $0x0, -0x8(%rbx)
movq 0xb13e(%rip), %rbx      # 0xffc8
leaq -0x38(%rbp), %rsi
movq %rbx, %rdi
callq 0x3c46
leaq 0x52f3(%rip), %rsi      # 0xa190
movq %rbx, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x3bba
movb $0x1, %al
jmp 0x4f4d
movq -0x38(%rbp), %rdi
pushq $0x5
popq %rsi
callq 0x3798
testl %eax, %eax
je 0x4eea
movq 0xb0f3(%rip), %rdi      # 0xffc8
leaq 0xb3d4(%rip), %rsi      # 0x102b0
callq 0x3c12
leaq 0x55e9(%rip), %rsi      # 0xa4d1
jmp 0x4f11
movq -0x38(%rbp), %rdi
callq 0x37ce
testl %eax, %eax
je 0x4f5b
movq 0xb0ca(%rip), %rdi      # 0xffc8
leaq 0xb3ab(%rip), %rsi      # 0x102b0
callq 0x3c12
leaq 0x55db(%rip), %rsi      # 0xa4ec
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
leaq 0x5582(%rip), %rsi      # 0xa4aa
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3150
movl $0xbb8, %edi            # imm = 0xBB8
callq 0x3bba
leaq -0x30(%rbp), %rdi
callq 0x6896
xorl %eax, %eax
addq $0xb8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x30a0
movl %eax, %r14d
movq -0x38(%rbp), %r15
leaq -0xb0(%rbp), %rdi
movl %eax, %esi
callq 0x6049
movq (%r15), %rax
leaq -0xb0(%rbp), %rsi
movq %r15, %rdi
callq *0x18(%rax)
leaq -0xb0(%rbp), %rdi
callq 0x3140
movl %r14d, -0x3c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6896
jmp 0x4d86
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
jmp 0x4fc6
jmp 0x4ff2
movq %rax, %rbx
leaq -0x70(%rbp), %rdi
jmp 0x4fd9
jmp 0x4fe0
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x3140
jmp 0x4ff5
jmp 0x4ff2
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x3140
jmp 0x4fe3
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x3140
leaq -0x48(%rbp), %rdi
jmp 0x4ff9
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x6896
movq %rbx, %rdi
callq 0x3470