// File: /workspace/github_cpp_makefile_star2_O3/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 $0x800f8, %rsp          # imm = 0x800F8
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, 0x8(%rsp)
movq (%rsi), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x4260
movl %eax, %r14d
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x4290
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r15, %rdi
callq 0x4110
leaq 0xf0(%rsp), %rbx
movl $0x80000, %edx          # imm = 0x80000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4200
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movl $0x0, 0x40(%rsp)
movl $0x80000, %edx          # imm = 0x80000
movl %r14d, (%rsp)
movl %r14d, %edi
movq %rbx, %rsi
callq 0x44a0
testq %rax, %rax
je 0xc268
movq %rax, %rbx
leaq 0xf0(%rsp), %r15
leaq 0x158f(%rip), %r13      # 0xd766
leaq 0x10(%rsp), %r12
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x30(%rsp), %rdx
callq 0x4080
xorl %r14d, %r14d
movzbl 0x30(%rsp,%r14), %edx
movq %r12, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x40b0
movl $0x2, %edx
movl %ebp, %edi
movq %r12, %rsi
callq 0x4170
incq %r14
cmpq $0xa, %r14
jne 0xc1ef
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x4110
movl $0x80000, %edx          # imm = 0x80000
movq %r15, %rdi
xorl %esi, %esi
callq 0x4200
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movl $0x0, 0x40(%rsp)
movl $0x80000, %edx          # imm = 0x80000
movl (%rsp), %edi
movq %r15, %rsi
callq 0x44a0
movq %rax, %rbx
testq %rax, %rax
jne 0xc1dc
movl (%rsp), %edi
callq 0x4670
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x4570
leaq 0x115d(%rip), %rsi      # 0xd3e6
movl $0x1, %edx
movl %ebp, %edi
callq 0x4170
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %rcx
movq %rcx, (%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rbx
leaq 0x14a9(%rip), %r14      # 0xd766
leaq 0x50(%rsp), %r15
leaq 0x10(%rsp), %r12
xorl %r13d, %r13d
movzbl 0x70(%rsp,%r13), %edx
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x40b0
movq %rbx, 0x10(%rsp)
movq %r15, %rdi
callq 0x41a0
leaq (%rsp,%rax), %rdx
addq $0x50, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x7562
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x8(%rsp), %rdi
callq 0x4140
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xc328
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4350
incq %r13
cmpq $0xa, %r13
jne 0xc2ca
movq 0x8(%rsp), %rax
movq (%rax), %rsi
movl $0x14, %edx
movl %ebp, %edi
callq 0x4170
movl %ebp, %edi
callq 0x4670
movq 0x8(%rsp), %rax
addq $0x800f8, %rsp          # imm = 0x800F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xc365
movq %rax, %r14
jmp 0xc384
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %rbx, %rdi
je 0xc384
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x4350
movq 0x8(%rsp), %rax
movq (%rax), %rdi
cmpq (%rsp), %rdi
je 0xc3a1
movq (%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x4350
movq %r14, %rdi
callq 0x45d0