// File: liskin[P]hybrid-screenclone/screenclone.cc
// Function: main
int main( int argc, char *argv[] )
{
	XInitThreads();

	std::string src_name( ":0" ), dst_name( ":1" );
	unsigned screen_number = 0;

	int opt;
	while ( ( opt = getopt( argc, argv, "s:d:x:h" ) ) != -1 )
		switch ( opt ) {
		case 's':
			src_name = optarg;
			break;
		case 'd':
			dst_name = optarg;
			break;
		case 'x':
			screen_number = atoi( optarg );
			break;
		default:
			usage( argv[ 0 ] );
		}

	if ( src_name == dst_name )
		ERR;
	display src( src_name ), dst( dst_name );

	auto screens = src.xinerama_screens();
	if ( screen_number < 0 || screen_number >= screens.size() )
		ERR;
	auto &screen = screens[ screen_number ];

	// Clone src not to fight with the blocking loop.
	mouse_replayer mouse( src.clone(), dst, screen );
	image_replayer image( src, dst, screen );

	window root = src.root();
	root.create_damage();

	src.record_pointer_events( &mouse );
	src.select_cursor_input( root );

	for ( ;; ) {
		do {
			const XEvent e = src.next_event();
			if ( e.type == src.damage_event + XDamageNotify ) {
				const XDamageNotifyEvent &de = * (const XDamageNotifyEvent *) &e;
				image.damage( de.area );
			} else if ( e.type == src.xfixes_event + XFixesCursorNotify ) {
				mouse.cursor_changed();
			}
		} while ( src.pending() );

		root.clear_damage();
		image.copy_if_damaged();
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4e0, %rsp            # imm = 0x4E0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x3200
leaq -0x31(%rbp), %rdi
movq %rdi, -0x498(%rbp)
callq 0x3510
movq -0x498(%rbp), %rdx
leaq 0x36d6(%rip), %rsi      # 0x80f2
leaq -0x30(%rbp), %rdi
callq 0x5200
jmp 0x4a27
leaq -0x31(%rbp), %rdi
callq 0x3310
leaq -0x69(%rbp), %rdi
movq %rdi, -0x4a0(%rbp)
callq 0x3510
movq -0x4a0(%rbp), %rdx
leaq 0x36a7(%rip), %rsi      # 0x80f5
leaq -0x68(%rbp), %rdi
callq 0x5200
jmp 0x4a59
leaq -0x69(%rbp), %rdi
callq 0x3310
movl $0x0, -0x70(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x3681(%rip), %rdx      # 0x80f8
callq 0x3050
movl %eax, -0x74(%rbp)
cmpl $-0x1, %eax
je 0x4b51
movl -0x74(%rbp), %eax
movl %eax, -0x4a4(%rbp)
subl $0x64, %eax
je 0x4b11
jmp 0x4a98
movl -0x4a4(%rbp), %eax
subl $0x73, %eax
je 0x4ae9
jmp 0x4aa5
movl -0x4a4(%rbp), %eax
subl $0x78, %eax
je 0x4b28
jmp 0x4b3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x3310
jmp 0x50d1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x69(%rbp), %rdi
callq 0x3310
jmp 0x50c8
movq 0x74c0(%rip), %rax      # 0xbfb0
movq (%rax), %rsi
leaq -0x30(%rbp), %rdi
callq 0x33c0
jmp 0x4afe
jmp 0x4b4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x50bf
movq 0x7498(%rip), %rax      # 0xbfb0
movq (%rax), %rsi
leaq -0x68(%rbp), %rdi
callq 0x33c0
jmp 0x4b26
jmp 0x4b4c
movq 0x7481(%rip), %rax      # 0xbfb0
movq (%rax), %rdi
callq 0x3470
movl %eax, -0x70(%rbp)
jmp 0x4b4c
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x4910
jmp 0x4b4a
jmp 0x4b4c
jmp 0x4a69
leaq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x53c0
testb $0x1, %al
jne 0x4b67
jmp 0x4d12
movb $0x1, -0x139(%rbp)
movl $0x10, %edi
callq 0x30f0
movq %rax, -0x4b8(%rbp)
leaq -0x138(%rbp), %rdi
movq %rdi, -0x4b0(%rbp)
callq 0x33e0
movq -0x4b0(%rbp), %rsi
leaq 0x3464(%rip), %rdx      # 0x8004
leaq -0x118(%rbp), %rdi
callq 0x51b0
jmp 0x4bae
leaq 0x34d2(%rip), %rdx      # 0x8087
leaq -0xf8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x51b0
jmp 0x4bca
leaq 0x352f(%rip), %rdx      # 0x8100
leaq -0xd8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x51b0
jmp 0x4be6
leaq 0x3486(%rip), %rdx      # 0x8073
leaq -0xb8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x51b0
jmp 0x4c02
leaq 0x34fb(%rip), %rdx      # 0x8104
leaq -0x98(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x51b0
jmp 0x4c1e
movq -0x4b8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3490
jmp 0x4c33
movq -0x4b8(%rbp), %rdi
movb $0x0, -0x139(%rbp)
movq 0x73a8(%rip), %rsi      # 0xbff0
movq 0x7379(%rip), %rdx      # 0xbfc8
callq 0x34d0
jmp 0x50da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4cea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4cde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4cd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4cc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4cba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x3150
leaq -0xb8(%rbp), %rdi
callq 0x3150
leaq -0xd8(%rbp), %rdi
callq 0x3150
leaq -0xf8(%rbp), %rdi
callq 0x3150
leaq -0x118(%rbp), %rdi
callq 0x3150
leaq -0x138(%rbp), %rdi
callq 0x3150
testb $0x1, -0x139(%rbp)
jne 0x4d01
jmp 0x4d0d
movq -0x4b8(%rbp), %rdi
callq 0x31a0
jmp 0x50bf
leaq -0x158(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x36f0
jmp 0x4d24
leaq -0x170(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x36f0
jmp 0x4d36
leaq -0x188(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x4040
jmp 0x4d4b
cmpl $0x0, -0x70(%rbp)
jb 0x4d7a
movl -0x70(%rbp), %eax
movq %rax, -0x4c0(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x5450
movq %rax, %rcx
movq -0x4c0(%rbp), %rax
cmpq %rcx, %rax
jb 0x4f25
movb $0x1, -0x249(%rbp)
movl $0x10, %edi
callq 0x30f0
movq %rax, -0x4d0(%rbp)
leaq -0x248(%rbp), %rdi
movq %rdi, -0x4c8(%rbp)
callq 0x33e0
movq -0x4c8(%rbp), %rsi
leaq 0x3251(%rip), %rdx      # 0x8004
leaq -0x228(%rbp), %rdi
callq 0x51b0
jmp 0x4dc1
leaq 0x32bf(%rip), %rdx      # 0x8087
leaq -0x208(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x51b0
jmp 0x4ddd
leaq 0x3325(%rip), %rdx      # 0x8109
leaq -0x1e8(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x51b0
jmp 0x4df9
leaq 0x3273(%rip), %rdx      # 0x8073
leaq -0x1c8(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x51b0
jmp 0x4e15
leaq 0x32e8(%rip), %rdx      # 0x8104
leaq -0x1a8(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x51b0
jmp 0x4e31
movq -0x4d0(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x3490
jmp 0x4e46
movq -0x4d0(%rbp), %rdi
movb $0x0, -0x249(%rbp)
movq 0x7195(%rip), %rsi      # 0xbff0
movq 0x7166(%rip), %rdx      # 0xbfc8
callq 0x34d0
jmp 0x50da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4efd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4ef1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4ee5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4ed9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x4ecd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x3150
leaq -0x1c8(%rbp), %rdi
callq 0x3150
leaq -0x1e8(%rbp), %rdi
callq 0x3150
leaq -0x208(%rbp), %rdi
callq 0x3150
leaq -0x228(%rbp), %rdi
callq 0x3150
leaq -0x248(%rbp), %rdi
callq 0x3150
testb $0x1, -0x249(%rbp)
jne 0x4f14
jmp 0x4f20
movq -0x4d0(%rbp), %rdi
callq 0x31a0
jmp 0x50b3
movl -0x70(%rbp), %eax
movl %eax, %esi
leaq -0x188(%rbp), %rdi
callq 0x5480
movq %rax, -0x258(%rbp)
leaq -0x308(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x3ce0
jmp 0x4f52
movq -0x258(%rbp), %rcx
leaq -0x2f0(%rbp), %rdi
leaq -0x308(%rbp), %rsi
leaq -0x170(%rbp), %rdx
callq 0x54a0
jmp 0x4f75
movq -0x258(%rbp), %rcx
leaq -0x3b0(%rbp), %rdi
leaq -0x158(%rbp), %rsi
leaq -0x170(%rbp), %rdx
callq 0x55c0
jmp 0x4f98
leaq -0x3c8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x3d90
jmp 0x4fad
leaq -0x3c8(%rbp), %rdi
callq 0x4320
jmp 0x4fbb
leaq -0x158(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x5810
jmp 0x4fd0
leaq -0x158(%rbp), %rdi
leaq -0x3c8(%rbp), %rsi
callq 0x4010
jmp 0x4fe5
jmp 0x4fe7
jmp 0x4fe9
leaq -0x488(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x3de0
jmp 0x4ffe
movl -0x488(%rbp), %eax
movl -0x150(%rbp), %ecx
addl $0x0, %ecx
cmpl %ecx, %eax
jne 0x5048
leaq -0x488(%rbp), %rax
movq %rax, -0x490(%rbp)
movq -0x490(%rbp), %rsi
addq $0x40, %rsi
leaq -0x3b0(%rbp), %rdi
callq 0x5ec0
jmp 0x5038
jmp 0x506d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x50b3
movl -0x488(%rbp), %eax
movl -0x148(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jne 0x506b
leaq -0x2f0(%rbp), %rdi
callq 0x5f10
jmp 0x5069
jmp 0x506b
jmp 0x506d
jmp 0x506f
leaq -0x158(%rbp), %rdi
callq 0x3ff0
movl %eax, -0x4d4(%rbp)
jmp 0x5083
movl -0x4d4(%rbp), %eax
cmpl $0x0, %eax
jne 0x4fe9
leaq -0x3c8(%rbp), %rdi
callq 0x4530
jmp 0x50a0
leaq -0x3b0(%rbp), %rdi
callq 0x60e0
jmp 0x50ae
jmp 0x4fe7
leaq -0x188(%rbp), %rdi
callq 0x5360
leaq -0x68(%rbp), %rdi
callq 0x3150
leaq -0x30(%rbp), %rdi
callq 0x3150
movq -0x40(%rbp), %rdi
callq 0x3500
nopw (%rax,%rax)