// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
leaq 0x220(%rsp), %r13
pushq $0x8
popq %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x2140
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq (%r13), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x240(%rsp,%rax)
je 0x2350
movq 0x2caf(%rip), %rdi      # 0x4fd8
leaq 0xcd4(%rip), %rsi       # 0x3004
callq 0x20e0
movq %rax, %rdi
movq %r15, %rsi
callq 0x20d0
movq %rax, %rdi
callq 0x2090
pushq $0x1
popq %rbx
jmp 0x242d
leaq 0x20(%rsp), %rdi
pushq $0x10
popq %rdx
movq %r12, %rsi
callq 0x2170
movq 0x20(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x40(%rsp,%rax)
je 0x2393
movq 0x2c61(%rip), %rdi      # 0x4fd8
leaq 0xc93(%rip), %rsi       # 0x3011
callq 0x20e0
movq %rax, %rdi
callq 0x2090
pushq $0x1
popq %rbx
jmp 0x2423
leaq 0x220(%rsp), %r15
movq %rsp, %r12
leaq 0x20(%rsp), %r13
movq %r15, %rdi
movq %r12, %rsi
callq 0x2030
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x240a
xorl %ebp, %ebp
movq %r12, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x2190
cmpq $-0x1, %rax
je 0x23ed
movq %rax, %rbp
movq 0x8(%r14), %rdx
movq %r12, %rdi
movq %rax, %rsi
movq %rbx, %rcx
callq 0x2060
addq 0x8(%rbx), %rbp
jmp 0x23be
movq %r13, %rdi
movq %r12, %rsi
callq 0x20d0
cmpq $0x0, 0x8(%rsp)
je 0x23a3
movq %r13, %rdi
callq 0x2090
jmp 0x23a3
leaq 0x220(%rsp), %rdi
callq 0x2040
xorl %ebx, %ebx
leaq 0x20(%rsp), %rdi
callq 0x2100
leaq 0x20(%rsp), %rdi
callq 0x2110
movq %rsp, %rdi
callq 0x2080
leaq 0x220(%rsp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x245a
jmp 0x2461
movq %rax, %rbx
jmp 0x246e
jmp 0x2461
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2110
movq %rsp, %rdi
callq 0x2080
leaq 0x220(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2150