// File: /workspace/github_cpp_makefile_star2_O2/girdhari9[P]mTORRENT/CreateTorrentFile.cpp
// Function: CalculateSHA(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
string CalculateSHA(string SrcFileName,int mtorrentPtr){
    unsigned char src_buffer[FILE_SIZE];
    unsigned char hashChunk[SHA_DIGEST_LENGTH];
    unsigned char fullhash[SHA_DIGEST_LENGTH];

    SHA_CTX sc;

    int srcFilePtr = open(SrcFileName.c_str(), O_RDONLY);

    SHA1_Init(&sc);
    SHA1_Update(&sc, SrcFileName.c_str(), SrcFileName.length());
    for (;;) {
        size_t read_size;
        bzero(src_buffer, FILE_SIZE);
        bzero(hashChunk, SHA_DIGEST_LENGTH);
        read_size = read(srcFilePtr,src_buffer,FILE_SIZE);
        if (read_size == 0)
            break;
        SHA1((unsigned char*) src_buffer, read_size, hashChunk);
        
        char SHA_1[20];
        for (int z = 0; z < 10; z++){
            sprintf((char *)SHA_1, "%02x", hashChunk[z]);
            write(mtorrentPtr,SHA_1,2);
        }
        SHA1_Update(&sc, src_buffer, read_size);
    }

    close(srcFilePtr);

    SHA1_Final(fullhash, &sc);
    write(mtorrentPtr,"\n",1);

    char SHA_1[20];
    string SHAFULL;
    for (int z = 0; z < 10; z++){
        sprintf((char *)SHA_1, "%02x", fullhash[z]);
        SHAFULL += (string)SHA_1;
    }
    write(mtorrentPtr,SHAFULL.c_str(),SHA_DIGEST_LENGTH);
    close(mtorrentPtr);
    return SHAFULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x80108, %rsp          # imm = 0x80108
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, 0x10(%rsp)
movq (%rsi), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x4270
movl %eax, 0x1c(%rsp)
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x42a0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r14, %rdi
callq 0x4140
leaq 0x100(%rsp), %r12
leaq 0x14cc(%rip), %r15      # 0xb766
leaq 0x60(%rsp), %rbx
pushq $0x2
popq %r13
movl $0x80000, %edx          # imm = 0x80000
movq %r12, %rdi
xorl %esi, %esi
callq 0x4210
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andl $0x0, 0x30(%rsp)
movl $0x80000, %edx          # imm = 0x80000
movl 0x1c(%rsp), %edi
movq %r12, %rsi
callq 0x4490
testq %rax, %rax
je 0xa336
movq %rax, %r14
movq %r12, %rdi
movq %rax, %rsi
leaq 0x20(%rsp), %rdx
callq 0x4080
xorl %r12d, %r12d
cmpq $0xa, %r12
je 0xa316
movzbl 0x20(%rsp,%r12), %edx
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x40b0
movl %ebp, %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x4180
incq %r12
jmp 0xa2eb
leaq 0xa0(%rsp), %rdi
leaq 0x100(%rsp), %r12
movq %r12, %rsi
movq %r14, %rdx
callq 0x4140
jmp 0xa2a3
movl 0x1c(%rsp), %edi
callq 0x4660
leaq 0x40(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x4550
leaq 0x108e(%rip), %rsi      # 0xb3e6
pushq $0x1
popq %rdx
movl %ebp, %edi
callq 0x4180
movq 0x10(%rsp), %rcx
leaq 0x10(%rcx), %rax
movq %rax, (%rcx)
andq $0x0, 0x8(%rcx)
movb $0x0, 0x10(%rcx)
leaq 0x13e8(%rip), %rbx      # 0xb766
leaq 0x80(%rsp), %r14
leaq 0x60(%rsp), %r15
leaq 0xf(%rsp), %r12
xorl %r13d, %r13d
cmpq $0xa, %r13
je 0xa3d4
movzbl 0x40(%rsp,%r13), %edx
movq %r14, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x40b0
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x6040
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x4650
movq %r15, %rdi
callq 0x4678
incq %r13
jmp 0xa393
movq 0x10(%rsp), %rax
movq (%rax), %rsi
pushq $0x14
popq %rdx
movl %ebp, %edi
callq 0x4180
movl %ebp, %edi
callq 0x4660
movq 0x10(%rsp), %rax
addq $0x80108, %rsp          # imm = 0x80108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa406
movq %rax, %rbx
jmp 0xa418
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4678
movq 0x10(%rsp), %rdi
callq 0x4678
movq %rbx, %rdi
callq 0x45b0