// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rdi, %r12
leaq 0x220(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movl $0x8, %edx
callq 0x2150
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x240(%rsp,%rax)
je 0x2397
movq 0x2c9b(%rip), %rdi      # 0x4fd8
leaq 0xcc0(%rip), %rsi       # 0x3004
movl $0xc, %edx
callq 0x2110
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq 0x2c7a(%rip), %rdi      # 0x4fd8
callq 0x2110
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2090
jmp 0x252d
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
movl $0x10, %edx
callq 0x21a0
movq 0x20(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x40(%rsp,%rax)
je 0x240c
movq 0x2c15(%rip), %rbx      # 0x4fd8
leaq 0xc47(%rip), %rsi       # 0x3011
movl $0x39, %edx
movq %rbx, %rdi
callq 0x2110
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq 0x2be3(%rip), %rdi      # 0x4fd8
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x2090
jmp 0x24fb
movq %rsp, %r13
movq 0x220(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %edx
movq %r15, %rdi
movq %r13, %rsi
callq 0x2190
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x24e2
movq (%r14), %rsi
movq 0x8(%r14), %rcx
movq %r13, %rdi
xorl %edx, %edx
callq 0x2160
cmpq $-0x1, %rax
je 0x2497
movq %rax, %rbp
movq 0x8(%r14), %rdx
movq (%rbx), %rcx
movq 0x8(%rbx), %r8
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2100
addq 0x8(%rbx), %rbp
movq (%r14), %rsi
movq 0x8(%r14), %rcx
movq %r13, %rdi
movq %rbp, %rdx
callq 0x2160
movq %rax, %rbp
cmpq $-0x1, %rax
jne 0x2462
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r12, %rdi
callq 0x2110
cmpq $0x0, 0x8(%rsp)
je 0x240f
movq 0x20(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x20e0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2090
jmp 0x240f
leaq 0x220(%rsp), %rdi
callq 0x2040
xorl %ebx, %ebx
leaq 0x20(%rsp), %rdi
callq 0x2120
movq 0x2ace(%rip), %rax      # 0x4fd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x28(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x20(%rsp,%rcx)
callq 0x20c0
leaq 0x118(%rsp), %rdi
callq 0x2070
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x2548
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x20f0
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 0x256d
jmp 0x2574
movq %rax, %rbx
jmp 0x2581
jmp 0x2574
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2130
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x259c
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x20f0
leaq 0x220(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2170