// File: /workspace/github_cpp_makefile_star2_O3/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 $0x108, %rsp            # imm = 0x108
movl $0x1, %esi
xorl %eax, %eax
leaq 0xe15(%rip), %rcx       # 0x2010
cmpq $0x10, %rax
je 0x120b
addl 0x4(%rax,%rcx), %esi
addq $0x4, %rax
jmp 0x11fb
leaq 0xe12(%rip), %rdi       # 0x2024
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x1030
leaq 0xe0b(%rip), %rdi       # 0x202e
movl $0x1e, %esi
xorl %eax, %eax
callq 0x1030
leaq 0xe02(%rip), %rdi       # 0x2038
movl $0x5, %esi
xorl %eax, %eax
callq 0x1030
movw $0x6261, 0x28(%rsp)     # imm = 0x6261
movb $0x63, 0x2a(%rsp)
leaq 0x98(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x78(%rsp), %r12
leaq 0x88(%rsp), %rbx
leaq 0x68(%rsp), %r14
movsbl 0x28(%rsp,%r15), %edi
callq 0x10d0
movsbl %al, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1060
movq %rbx, %rdi
movl %ebp, %esi
callq 0x1060
movq %r12, 0x68(%rsp)
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x1778
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1080
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x12dd
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x1070
incq %r15
cmpq $0x3, %r15
jne 0x1279
leaq 0xb8(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq 0x88(%rsp), %rsi
cmpq %r13, %rsi
je 0x1319
movq %rsi, 0xa8(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0xb8(%rsp)
jmp 0x1325
movups (%r13), %xmm0
movups %xmm0, (%rbp)
movq %rbp, %rsi
movq 0x90(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq %r13, 0x88(%rsp)
movq $0x0, 0x90(%rsp)
movb $0x0, 0x98(%rsp)
leaq 0xcea(%rip), %rdi       # 0x2042
xorl %eax, %eax
callq 0x1030
movb $0x41, %al
leaq 0xce4(%rip), %r15       # 0x204c
leaq 0xce2(%rip), %rbx       # 0x2051
xorl %r14d, %r14d
testb %al, %al
je 0x1386
movsbl %al, %edx
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1030
cmpq $0x4, %r14
je 0x1396
movb 0x1(%r14,%r15), %al
incq %r14
jmp 0x1372
movl $0xa, %edi
callq 0x1090
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0xcaa(%rip), %rsi       # 0x205a
leaq 0xcb5(%rip), %rdx       # 0x206c
leaq 0x28(%rsp), %rdi
callq 0x180e
movq 0x28(%rsp), %rbx
movq 0x30(%rsp), %r14
leaq 0xc9b(%rip), %rdi       # 0x206d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1030
testq %r14, %r14
je 0x140d
addq %rbx, %r14
leaq 0x1(%rbx), %r15
movl $0x1, %r13d
movsbl (%rbx), %edi
callq 0x1090
cmpq %r14, %r15
je 0x140d
incq %r15
incq %r13
testb $0x1, %r13b
je 0x13f6
leaq -0x1(%r15), %rbx
jmp 0x13ee
leaq 0xc61(%rip), %rdi       # 0x2075
callq 0x10a0
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movw $0x6548, %r13w          # imm = 0x6548
xorl %r15d, %r15d
leaq 0x48(%rsp), %rbx
leaq 0x68(%rsp), %r14
leaq 0xc3b(%rip), %rbp       # 0x2081
movsbl %r13b, %esi
movq %rbx, %rdi
callq 0x1060
shrl $0x8, %r13d
movsbl %r13b, %esi
movq %rbx, %rdi
callq 0x1060
movq %r12, 0x68(%rsp)
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x1778
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1080
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x149e
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x1070
cmpq $0x8, %r15
je 0x14c3
leaq 0xbcc(%rip), %rax       # 0x2077
movzbl 0x1(%r15,%rax), %eax
movzbl 0x1(%r15,%rbp), %r13d
shll $0x8, %r13d
orl %eax, %r13d
incq %r15
jmp 0x1446
leaq 0xd8(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rcx
cmpq %rcx, %rdi
je 0x14f5
movq %rdi, 0xc8(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0xd8(%rsp)
jmp 0x14fe
movups (%rcx), %xmm0
movups %xmm0, (%rax)
movq %rax, %rdi
movq 0x50(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq %rcx, 0x48(%rsp)
xorl %r15d, %r15d
movq %r15, 0x50(%rsp)
movb %r15b, 0x58(%rsp)
callq 0x10a0
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
movq %r15, -0x8(%rax)
movb %r15b, (%rax)
leaq 0x8(%rsp), %rbx
leaq 0x68(%rsp), %r14
leaq 0xb48(%rip), %rbp       # 0x208b
xorl %r13d, %r13d
cmpq $0x6, %r13
jne 0x1567
cmpq $0x7, %r15
je 0x15bf
leaq 0xb38(%rip), %rax       # 0x2091
addq %r15, %rax
incq %r15
movl $0x6, %r13d
jmp 0x1570
movq %rbp, %rax
addq %r13, %rax
incq %r13
movb (%rax), %al
testb %al, %al
je 0x1546
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1060
movq %r12, 0x68(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x1778
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1080
movq 0x68(%rsp), %rdi
cmpq %r12, %rdi
je 0x1546
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x1070
jmp 0x1546
leaq 0xf8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rcx
cmpq %rcx, %rdi
je 0x15f1
movq %rdi, 0xe8(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0xf8(%rsp)
jmp 0x15fa
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq %rbx, %rdi
leaq 0xb8(%rsp), %r14
movq 0x10(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq %rcx, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
callq 0x10a0
movq 0xe8(%rsp), %rdi
cmpq %rbx, %rdi
je 0x1644
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x1070
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
je 0x1669
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x1070
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x1685
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x1070
movq 0xa8(%rsp), %rdi
cmpq %r14, %rdi
je 0x16a2
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x1070
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1733
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x16da
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x1070
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
movq %rax, %rcx
cmpq %rax, %rdi
jmp 0x1702
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rcx
cmpq %rcx, %rdi
leaq 0xb8(%rsp), %rbp
leaq 0x38(%rsp), %r14
je 0x171c
movq (%rcx), %rsi
incq %rsi
callq 0x1070
movq 0x28(%rsp), %rdi
cmpq %r14, %rdi
je 0x1733
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x1070
movq 0xa8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x176a
movq 0xb8(%rsp), %rsi
jmp 0x1762
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r13, %rdi
je 0x176a
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x1070
movq %rbx, %rdi
callq 0x10b0