// File: t0mm4rx[P]piscine_cpp/day01/ex07/main.cpp
// Function: find_and_replace(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int find_and_replace(std::string sourceFile, std::string outputFile, std::string find, std::string replace)
{
	std::ifstream			input(sourceFile);
	std::string				lineBuffer;
	std::string::size_type	n;

	if (input.fail())
	{
		std::cout << "Cannot open " << sourceFile << std::endl;
		return (1);
	}
	std::ofstream output(outputFile);
	if (output.fail())
	{
		std::cout << "Cannot create output file, check rights of current folder" << std::endl;
		return (1);
	}
	while (std::getline(input, lineBuffer))
    {
		n = lineBuffer.find(find);
		while (n != std::string::npos)
        {
            lineBuffer.replace(n, find.size(), replace);
			n = lineBuffer.find(find, n + replace.size());
        }
        output << lineBuffer;
		if (lineBuffer.size() > 0)
	        output << std::endl;
    }
    input.close();
    output.close();
	return (0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4d0, %rsp            # imm = 0x4D0
movq %rsi, %rax
movq %rdi, %rsi
movq %rsi, -0x490(%rbp)
movq %rax, -0x488(%rbp)
movq %rdx, -0x480(%rbp)
movq %rcx, -0x478(%rbp)
movq %rsi, -0x10(%rbp)
movq %rax, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
leaq -0x230(%rbp), %rdi
movl $0x8, %edx
callq 0x2240
leaq -0x250(%rbp), %rdi
callq 0x21e0
movq -0x230(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x230(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x469(%rbp)
jmp 0x248a
movb -0x469(%rbp), %al
testb $0x1, %al
jne 0x2496
jmp 0x2510
movq 0x2b3b(%rip), %rdi      # 0x4fd8
leaq 0xb60(%rip), %rsi       # 0x3004
callq 0x2160
movq %rax, -0x498(%rbp)
jmp 0x24b2
movq -0x490(%rbp), %rsi
movq -0x498(%rbp), %rdi
callq 0x2150
movq %rax, -0x4a0(%rbp)
jmp 0x24ce
movq -0x4a0(%rbp), %rdi
movq 0x2aec(%rip), %rsi      # 0x4fc8
callq 0x2170
jmp 0x24e3
movl $0x1, -0x4(%rbp)
movl $0x1, -0x268(%rbp)
jmp 0x272b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
jmp 0x274f
movq -0x488(%rbp), %rsi
leaq -0x468(%rbp), %rdi
movl $0x10, %edx
callq 0x2290
jmp 0x252a
movq -0x468(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x468(%rbp,%rax), %rdi
callq 0x2030
movb %al, -0x4a1(%rbp)
jmp 0x254a
movb -0x4a1(%rbp), %al
testb $0x1, %al
jne 0x2556
jmp 0x25c0
movq 0x2a7b(%rip), %rdi      # 0x4fd8
leaq 0xaad(%rip), %rsi       # 0x3011
callq 0x2160
movq %rax, -0x4b0(%rbp)
jmp 0x2572
movq -0x4b0(%rbp), %rdi
movq 0x2a48(%rip), %rsi      # 0x4fc8
callq 0x2170
jmp 0x2587
movl $0x1, -0x4(%rbp)
movl $0x1, -0x268(%rbp)
jmp 0x271f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x260(%rbp)
movl %eax, -0x264(%rbp)
leaq -0x468(%rbp), %rdi
callq 0x2210
jmp 0x274f
jmp 0x25c2
leaq -0x230(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x2060
movq %rax, -0x4b8(%rbp)
jmp 0x25de
movq -0x4b8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x21d0
movb %al, -0x4b9(%rbp)
jmp 0x25fc
movb -0x4b9(%rbp), %al
testb $0x1, %al
jne 0x260b
jmp 0x26f2
movq -0x480(%rbp), %rsi
leaq -0x250(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x22c0
movq %rax, -0x258(%rbp)
cmpq $-0x1, -0x258(%rbp)
je 0x26af
movq -0x480(%rbp), %rdi
movq -0x258(%rbp), %rax
movq %rax, -0x4c8(%rbp)
callq 0x2100
movq -0x4c8(%rbp), %rsi
movq -0x478(%rbp), %rcx
movq %rax, %rdx
leaq -0x250(%rbp), %rdi
callq 0x20a0
jmp 0x266c
movq -0x478(%rbp), %rdi
movq -0x258(%rbp), %rax
movq %rax, -0x4d0(%rbp)
callq 0x2100
movq -0x4d0(%rbp), %rdx
movq -0x480(%rbp), %rsi
addq %rax, %rdx
leaq -0x250(%rbp), %rdi
callq 0x22c0
movq %rax, -0x258(%rbp)
jmp 0x2629
leaq -0x468(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x2150
jmp 0x26c4
leaq -0x250(%rbp), %rdi
callq 0x2100
cmpq $0x0, %rax
jbe 0x26ed
movq 0x28eb(%rip), %rsi      # 0x4fc8
leaq -0x468(%rbp), %rdi
callq 0x2170
jmp 0x26eb
jmp 0x26ed
jmp 0x25c2
leaq -0x230(%rbp), %rdi
callq 0x2070
jmp 0x2700
leaq -0x468(%rbp), %rdi
callq 0x21f0
jmp 0x270e
movl $0x0, -0x4(%rbp)
movl $0x1, -0x268(%rbp)
leaq -0x468(%rbp), %rdi
callq 0x2210
leaq -0x250(%rbp), %rdi
callq 0x20e0
leaq -0x230(%rbp), %rdi
callq 0x2080
movl -0x4(%rbp), %eax
addq $0x4d0, %rsp            # imm = 0x4D0
popq %rbp
retq
leaq -0x250(%rbp), %rdi
callq 0x20e0
leaq -0x230(%rbp), %rdi
callq 0x2080
movq -0x260(%rbp), %rdi
callq 0x2260
nopw %cs:(%rax,%rax)