// File: 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
movq %rsp, %rbp
subq $0x80190, %rsp          # imm = 0x80190
movq %rsi, -0x80160(%rbp)
movq %rdi, %rax
movq -0x80160(%rbp), %rdi
movq %rax, -0x80170(%rbp)
movq %rax, %rcx
movq %rcx, -0x80168(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
callq 0x41f0
movq %rax, %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x4310
movl %eax, -0x800c4(%rbp)
leaq -0x800c0(%rbp), %rdi
callq 0x4350
movq -0x80160(%rbp), %rdi
callq 0x41f0
movq -0x80160(%rbp), %rdi
movq %rax, -0x80158(%rbp)
callq 0x4600
movq -0x80158(%rbp), %rsi
movq %rax, %rdx
leaq -0x800c0(%rbp), %rdi
callq 0x4170
leaq -0x80020(%rbp), %rdi
xorl %esi, %esi
movl $0x80000, %edx          # imm = 0x80000
callq 0x4260
leaq -0x80040(%rbp), %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x4260
movl -0x800c4(%rbp), %edi
leaq -0x80020(%rbp), %rsi
movl $0x80000, %edx          # imm = 0x80000
callq 0x45c0
movq %rax, -0x800d0(%rbp)
cmpq $0x0, -0x800d0(%rbp)
jne 0x14e93
jmp 0x14f28
leaq -0x80020(%rbp), %rdi
movq -0x800d0(%rbp), %rsi
leaq -0x80040(%rbp), %rdx
callq 0x40b0
movl $0x0, -0x800f4(%rbp)
cmpl $0xa, -0x800f4(%rbp)
jge 0x14f09
leaq -0x800f0(%rbp), %rdi
movslq -0x800f4(%rbp), %rax
movzbl -0x80040(%rbp,%rax), %edx
leaq 0x1877(%rip), %rsi      # 0x16754
movb $0x0, %al
callq 0x40f0
movl -0x14(%rbp), %edi
leaq -0x800f0(%rbp), %rsi
movl $0x2, %edx
callq 0x41b0
movl -0x800f4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x800f4(%rbp)
jmp 0x14eb7
leaq -0x80020(%rbp), %rsi
movq -0x800d0(%rbp), %rdx
leaq -0x800c0(%rbp), %rdi
callq 0x4170
jmp 0x14e40
movl -0x800c4(%rbp), %edi
callq 0x47b0
leaq -0x80060(%rbp), %rdi
leaq -0x800c0(%rbp), %rsi
callq 0x4690
movl -0x14(%rbp), %edi
leaq 0x14ee(%rip), %rsi      # 0x1643e
movl $0x1, %edx
callq 0x41b0
movq -0x80170(%rbp), %rdi
movb $0x0, -0x80111(%rbp)
callq 0x45a0
movl $0x0, -0x80118(%rbp)
cmpl $0xa, -0x80118(%rbp)
jge 0x1505f
movslq -0x80118(%rbp), %rax
movzbl -0x80060(%rbp,%rax), %edx
leaq 0x17ba(%rip), %rsi      # 0x16754
xorl %eax, %eax
leaq -0x80110(%rbp), %rdi
movq %rdi, -0x80180(%rbp)
callq 0x40f0
leaq -0x80139(%rbp), %rdi
movq %rdi, -0x80178(%rbp)
callq 0x4710
movq -0x80180(%rbp), %rsi
movq -0x80178(%rbp), %rdx
leaq -0x80138(%rbp), %rdi
callq 0x6d90
jmp 0x14fde
movq -0x80170(%rbp), %rdi
leaq -0x80138(%rbp), %rsi
callq 0x4550
jmp 0x14ff3
leaq -0x80138(%rbp), %rdi
callq 0x47e8
leaq -0x80139(%rbp), %rdi
callq 0x4450
movl -0x80118(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80118(%rbp)
jmp 0x14f77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80148(%rbp)
movl %eax, -0x8014c(%rbp)
jmp 0x15051
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80148(%rbp)
movl %eax, -0x8014c(%rbp)
leaq -0x80138(%rbp), %rdi
callq 0x47e8
leaq -0x80139(%rbp), %rdi
callq 0x4450
jmp 0x150d5
movq -0x80170(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x80184(%rbp)
callq 0x41f0
movl -0x80184(%rbp), %edi
movq %rax, %rsi
movl $0x14, %edx
callq 0x41b0
jmp 0x15089
movl -0x14(%rbp), %edi
callq 0x47b0
jmp 0x15093
movb $0x1, -0x80111(%rbp)
testb $0x1, -0x80111(%rbp)
jne 0x150c5
jmp 0x150b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80148(%rbp)
movl %eax, -0x8014c(%rbp)
jmp 0x150d5
movq -0x80170(%rbp), %rdi
callq 0x47e8
movq -0x80168(%rbp), %rax
addq $0x80190, %rsp          # imm = 0x80190
popq %rbp
retq
movq -0x80170(%rbp), %rdi
callq 0x47e8
movq -0x80148(%rbp), %rdi
callq 0x4700
nopl (%rax)