// File: /workspace/github_cpp_makefile_star2_O2/manpat[P]iterator_trait/main.cpp
// Function: main
int main() {
	int arr[] { 1, 2, 3, 4, 5 };

	// Construct an iterator
	int sum = 0;
	for (const auto v : iter(arr)) {
		sum += v;
	}

	std::printf("sum:  %d\n", sum);

	// Map values
	int sum2 = 0;
	for (const auto v : iter(arr).map([](int v) { return v * 2; })) {
		sum2 += v;
	}

	std::printf("sum2: %d\n", sum2);

	// Sum squares with map-fold
	int sum3 = iter(arr)
		.map([](int v) { return v * v; })
		.fold(0, [](int a, int) { return a + 1; });

	std::printf("sum3: %d\n", sum3);

	// Construct a string from an std::array + char doubling and uppercasing
	// NOTE: implicit cast from int return of std::toupper to char in fold
	auto str = iter(std::array<char, 3>{'a', 'b', 'c'})
		.map([](char v) { return std::toupper(v); })
		.fold(std::string{}, [](auto& a, char c) { a += c; return a += c; });

	std::printf("str:  %s\n", str.c_str());

	// Enumerate const char[]
	for (const auto kv : iter("ABCD").enumerate()) {
		if (kv.second)
			std::printf("%d '%c' ", (int)kv.first, kv.second);
	}

	std::putchar('\n');

	// Filter every second letter in string
	std::string str_to_filter {"Hello I'm a string"};
	auto filter_it = iter(str_to_filter).enumerate()
		.filter([](const auto& kv) { return !(kv.first & 1); })
		.map([](const auto& kv) { return kv.second; });

	std::printf("'%s' -> '", str_to_filter.data());
	for (auto c : filter_it) {
		std::putchar(c);
	}
	std::puts("'");

	// Zip two strings together
	const char* zip_string_a = "HloImasrn";
	const char* zip_string_b = "el '  tig";

	auto iter_a = iter(zip_string_a, zip_string_a + std::strlen(zip_string_a));
	auto iter_b = iter(zip_string_b, zip_string_b + std::strlen(zip_string_b));
	auto zip_string = iter_a.zip(iter_b)
		.fold(std::string{}, [](auto& s, auto ab) {
			s += ab.first;
			return s += ab.second;
		});

	std::printf("%s\n", zip_string.data());

	// Chain two strings together
	auto chain_string = iter("Hello").chain(iter(" world"))
		.filter([](char c) { return c != 0; }) // Skip the null terminator
		.fold(std::string{}, [](auto& s, auto c) { return s += c; });

	std::printf("%s\n", chain_string.data());
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movaps 0xe1c(%rip), %xmm0      # 0x2010
leaq 0x174(%rsp), %r12
movaps %xmm0, -0x14(%r12)
movl $0x5, -0x4(%r12)
leaq 0x160(%rsp), %r13
leaq 0x80(%rsp), %rsi
movq %r13, (%rsi)
movq %r12, 0x8(%rsi)
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x1a28
xorl %ebx, %ebx
leaq 0x48(%rsp), %r15
movb %bl, 0x10(%r15)
movb %bl, 0x1c(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1946
testb %al, %al
je 0x1261
movq %r14, %rdi
callq 0x1962
addl %eax, %ebx
movq %r14, %rdi
callq 0x1978
jmp 0x123e
leaq 0xdbc(%rip), %rdi       # 0x2024
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
leaq 0x8(%rsp), %rbx
movq %r13, (%rbx)
movq %r12, 0x8(%rbx)
movb $0x1, 0x18(%rbx)
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x185e
movb 0x4(%r14), %al
movb %al, 0x24(%rbx)
cmpb $0x1, %al
jne 0x12a6
movl 0x48(%rsp), %ecx
movl %ecx, 0x28(%rsp)
jmp 0x12ab
movb $0x0, 0x20(%rsp)
xorl %ebx, %ebx
leaq 0x48(%rsp), %r14
leaq 0x8(%rsp), %r15
cmpb $0x0, 0x20(%rsp)
je 0x12f2
testb $0x1, %al
je 0x17dc
addl 0x28(%rsp), %ebx
movq %r14, %rdi
movq %r15, %rsi
callq 0x185e
movb 0x4c(%rsp), %al
movb %al, 0x2c(%rsp)
cmpb $0x1, %al
jne 0x12eb
movl 0x48(%rsp), %ecx
movl %ecx, 0x28(%rsp)
jmp 0x12b7
movb $0x0, 0x20(%rsp)
jmp 0x12b7
leaq 0xd35(%rip), %rdi       # 0x202e
xorl %r12d, %r12d
movl %ebx, %esi
xorl %eax, %eax
callq 0x1030
leaq 0xd2c(%rip), %rdi       # 0x2038
pushq $0x5
popq %rsi
xorl %eax, %eax
callq 0x1030
movw $0x6261, 0x48(%rsp)     # imm = 0x6261
leaq 0x130(%rsp), %r15
movq %r15, -0x10(%r15)
andq $0x0, -0x8(%r15)
movb $0x63, 0x4a(%rsp)
movb $0x0, (%r15)
leaq 0x120(%rsp), %rbx
leaq 0x8(%rsp), %r14
cmpq $0x3, %r12
je 0x138f
movsbl 0x48(%rsp,%r12), %edi
callq 0x10d0
movsbl %al, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1070
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1070
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1090
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1080
movq %r14, %rdi
callq 0x1050
incq %r12
jmp 0x1344
leaq 0x90(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x120(%rsp), %rcx
cmpq %r15, %rcx
je 0x13c2
movq %rcx, 0x80(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x90(%rsp)
jmp 0x13c9
movups (%r15), %xmm0
movups %xmm0, (%rax)
leaq 0x120(%rsp), %rdi
movq 0x8(%rdi), %rax
movq %rax, 0x88(%rsp)
movq %r15, (%rdi)
andq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
callq 0x1050
movq 0x80(%rsp), %rsi
leaq 0xc45(%rip), %rdi       # 0x2042
xorl %eax, %eax
callq 0x1030
leaq 0xc41(%rip), %rax       # 0x204c
leaq 0x100(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xc34(%rip), %rax       # 0x2051
movq %rax, 0x8(%rsi)
andq $0x0, 0x10(%rsi)
leaq 0x8(%rsp), %rdi
callq 0x1aca
xorl %eax, %eax
leaq 0x48(%rsp), %rbx
movb %al, 0x18(%rbx)
movb %al, 0x30(%rbx)
leaq 0x8(%rsp), %r14
leaq 0xc08(%rip), %r15       # 0x2051
movq %r14, %rdi
movq %rbx, %rsi
callq 0x19b4
testb %al, %al
je 0x147d
movq %r14, %rdi
callq 0x19d0
testb %dl, %dl
je 0x1473
movsbl %dl, %edx
movq %r15, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1030
movq %r14, %rdi
callq 0x19e6
jmp 0x1449
pushq $0xa
popq %rdi
callq 0x10a0
leaq 0xbce(%rip), %rsi       # 0x205a
leaq 0x100(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1040
movq 0x100(%rsp), %rbx
movq 0x108(%rsp), %r14
addq %rbx, %r14
leaq 0xbb5(%rip), %rdi       # 0x206d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1030
movb $0x1, %al
leaq 0x8(%rsp), %rsi
movb %al, 0x10(%rsi)
movq %rbx, (%rsi)
movq %r14, 0x8(%rsi)
andq $0x0, 0x18(%rsi)
movb %al, 0x30(%rsi)
leaq 0x48(%rsp), %rdi
callq 0x1892
movb 0x49(%rsp), %al
movb %al, 0x41(%rsp)
cmpb $0x1, %al
jne 0x14fb
movb 0x48(%rsp), %al
movb %al, 0x40(%rsp)
jmp 0x150e
leaq 0x48(%rsp), %rsi
movb $0x0, 0x30(%rsi)
leaq 0x8(%rsp), %rdi
callq 0x1906
leaq 0x48(%rsp), %rbx
leaq 0x8(%rsp), %r14
cmpb $0x0, 0x38(%rsp)
je 0x1572
cmpb $0x0, 0x41(%rsp)
je 0x17dc
movsbl 0x40(%rsp), %edi
callq 0x10a0
cmpb $0x0, 0x38(%rsp)
je 0x17dc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1892
movb 0x49(%rsp), %al
movb %al, 0x41(%rsp)
cmpb $0x1, %al
jne 0x1560
movb 0x48(%rsp), %al
movb %al, 0x40(%rsp)
jmp 0x1518
movb $0x0, 0x78(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1906
jmp 0x1518
leaq 0xafc(%rip), %rdi       # 0x2075
callq 0x10b0
leaq 0xaf2(%rip), %rax       # 0x2077
leaq 0x48(%rsp), %rbx
movq %rax, (%rbx)
leaq 0xaec(%rip), %rax       # 0x2080
movq %rax, 0x8(%rbx)
leaq 0xae2(%rip), %rax       # 0x2081
movq %rax, 0x10(%rbx)
leaq 0xae0(%rip), %rax       # 0x208a
leaq 0xf0(%rsp), %r12
movq %r12, -0x10(%r12)
andq $0x0, -0x8(%r12)
movq %rax, 0x18(%rbx)
movb $0x0, (%r12)
leaq 0x8(%rsp), %r14
leaq 0xe0(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1c44
cmpb $0x1, 0xa(%rsp)
jne 0x1625
movzwl 0x8(%rsp), %ebp
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x1070
shrl $0x8, %ebp
movsbl %bpl, %esi
movq %r15, %rdi
callq 0x1070
movq %r14, %rdi
movq %r15, %rsi
callq 0x1090
movq %r15, %rdi
movq %r14, %rsi
callq 0x1080
movq %r14, %rdi
callq 0x1050
jmp 0x15d3
leaq 0xb0(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0xe0(%rsp), %rcx
cmpq %r12, %rcx
je 0x1658
movq %rcx, 0xa0(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0xb0(%rsp)
jmp 0x1660
movups (%r12), %xmm0
movups %xmm0, (%rax)
leaq 0xe0(%rsp), %rdi
movq 0x8(%rdi), %rax
movq %r12, (%rdi)
andq $0x0, 0x8(%rdi)
movq %rax, 0xa8(%rsp)
xorl %ebp, %ebp
movb %bpl, 0x10(%rdi)
callq 0x1050
movq 0xa0(%rsp), %rdi
callq 0x10b0
leaq 0x9f0(%rip), %rax       # 0x208b
leaq 0x8(%rsp), %rbx
movq %rax, (%rbx)
leaq 0x9e7(%rip), %rax       # 0x2091
movq %rax, 0x8(%rbx)
leaq 0x9dc(%rip), %rax       # 0x2091
movq %rax, 0x10(%rbx)
leaq 0x9d8(%rip), %rax       # 0x2098
leaq 0xd0(%rsp), %r12
movq %r12, -0x10(%r12)
andq $0x0, -0x8(%r12)
movq %rax, 0x18(%rbx)
movb %bpl, (%r12)
leaq 0x48(%rsp), %r14
leaq 0xc0(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1cb8
cmpb $0x1, 0x49(%rsp)
jne 0x172d
movb 0x48(%rsp), %al
testb %al, %al
je 0x16e8
movsbl %al, %esi
movq %r15, %rdi
callq 0x1070
movq %r14, %rdi
movq %r15, %rsi
callq 0x1090
movq %r15, %rdi
movq %r14, %rsi
callq 0x1080
movq %r14, %rdi
callq 0x1050
jmp 0x16e8
leaq 0x150(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0xc0(%rsp), %rcx
cmpq %r12, %rcx
je 0x1760
movq %rcx, 0x140(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x150(%rsp)
jmp 0x1768
movups (%r12), %xmm0
movups %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
movq 0x8(%rdi), %rax
leaq 0x140(%rsp), %rbx
movq %rax, 0x8(%rbx)
movq %r12, (%rdi)
andq $0x0, 0x8(%rdi)
movb $0x0, 0x10(%rdi)
callq 0x1050
movq (%rbx), %rdi
callq 0x10b0
movq %rbx, %rdi
callq 0x1050
leaq 0xa0(%rsp), %rdi
callq 0x1050
leaq 0x100(%rsp), %rdi
callq 0x1050
leaq 0x80(%rsp), %rdi
callq 0x1050
xorl %eax, %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1060
jmp 0x17eb
jmp 0x17eb
jmp 0x1810
jmp 0x1810
jmp 0x17eb
movq %rax, %rbx
jmp 0x1837
jmp 0x1810
jmp 0x1815
jmp 0x17f6
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1050
leaq 0xa0(%rsp), %rdi
jmp 0x1832
movq %rax, %rbx
jmp 0x1844
movq %rax, %rbx
jmp 0x1851
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
jmp 0x184c
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x1050
leaq 0x100(%rsp), %rdi
callq 0x1050
leaq 0x80(%rsp), %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x10c0