// File: /workspace/github_c_makefile_star2_O0/findstr[P]version-database/diff.c
// Function: diff_tree
static void
diff_tree(dr_t a, dr_t b, dr_t root)
{
	int i;
	drb_t file;
	dr_t data, afinger;
	dr_t patchhash;
	char buf[2048];
	struct ref *r;
	struct release ra, rb;
	struct tree tb, ta_name, ta_hash;
	int add, dff, cpy, del;
	a = db_aliashash(a);
	b = db_aliashash(b);
	db_readrel(&ra, a);
	db_readrel(&rb, b);
	db_readtree(&ta_name, ra.tree);
	db_readtree(&tb, rb.tree);
	afinger = dr_ref(ra.finger);
	printf("tree a:%s\n", ra.tree->buf);
	printf("tree b:%s\n", rb.tree->buf);
	release_destroy(&ra);
	release_destroy(&rb);
	tree_clone(&ta_hash, &ta_name);
	tree_sort(&ta_name, ref_namecmp);
	tree_sort(&ta_hash, ref_hashcmp);
	drb_init(&file, 1024);
	printf("diff start\n");
	printf("detect change\n");
	add = 0; dff = 0; cpy = 0; del = 0;
	for (i = 0; i < tb.refn; i++) {
		struct ref *a, *b;
		dr_t patch = NULL;
		b = &tb.refs[i];
		if ((a = tree_search(&ta_hash, b, ref_hashcmp))) {
			if (dr_cmp(b->name, a->name) != 0) {
				struct CPY C;
				C.namea = a->name;
				C.name = b->name;
				ctrl_cpy(&file, &C);
				++cpy;
			} else {
				//printf("%s is same, skip it.\n", b->name->buf);
			}
			continue;
		}
		if (b->data == NULL)
			b->data = db_read(b->hash);
		a = tree_search(&ta_name, b, ref_namecmp);
		if (a == NULL) {
			struct NEW N;
			N.name = (b->name);
			N.data = (b->data);
			ctrl_new(&file, &N);
			add++;
		} else {
			if (a->data == NULL)
				a->data = db_read(a->hash);
			patch = diff_content(a->data, b->data);
			if (b->data->size <= patch->size) {//no fix patch
				struct NEW N;
				N.name = (b->name);
				N.data = (b->data);
				ctrl_new(&file, &N);
				add++;
			} else {
				struct DFF D;
				D.name = b->name;
				D.patch = patch;
				ctrl_dff(&file, &D);
				++dff;
			}
			dr_unref(patch);
		}
	}
	printf("detect recpye\n");
#if ENABLE_DEL
	tree_sort(&tb, ref_namecmp);
	for (i = 0; i < ta_name.refn; i++) {
		r = &ta_name.refs[i];
		if (tree_search(&tb, r, ref_namecmp) == NULL) { //clear
			struct DEL d;
			d.name = r->name;
			ctrl_del(&file, &d);
			++del;
		}
	}
#endif
	data = drb_dump(&file);
	patchhash = db_hash(data);
	snprintf(buf, sizeof(buf), "%s/patch.dat", str(afinger));
	dir_writefile(buf, data, root);
	snprintf(buf, sizeof(buf),
		"{\"add\":%d, \"dff\":%d, \"cpy\":%d, "
		"\"del\":%d, \"hash\":\"%s\", \"size\":%d }",
		add, dff, cpy, del, patchhash->buf, data->size);
	dr_unref(data);
	data = dr_newstr(buf);
	snprintf(buf, sizeof(buf), "%s/patch.json", str(afinger));
	dir_writefile(buf, data, root);
	printf("%s\n\n", str(data));
	snprintf(buf, sizeof(buf), "%s/%s/patch.dat", str(root), str(afinger));
	dr_unref(data);
	dr_unref(patchhash);
	dr_unref(afinger);
	tree_destroy(&ta_name);
	tree_destroy(&ta_hash);
	tree_destroy(&tb);
	data = dr_newstr(buf);
	verify(a, b, data);
	dr_unref(a);
	dr_unref(b);
	dr_unref(data);
	return ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x970, %rsp            # imm = 0x970
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
callq 0x6240
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6240
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x888(%rbp), %rdi
callq 0x60c0
movq -0x10(%rbp), %rsi
leaq -0x8b8(%rbp), %rdi
callq 0x60c0
movq -0x878(%rbp), %rsi
leaq -0x8d8(%rbp), %rdi
callq 0x6100
movq -0x8a8(%rbp), %rsi
leaq -0x8c8(%rbp), %rdi
callq 0x6100
movq -0x860(%rbp), %rdi
callq 0x3d60
movq %rax, -0x40(%rbp)
movq -0x878(%rbp), %rsi
addq $0x8, %rsi
leaq 0x8a7f(%rip), %rdi      # 0xc248
movb $0x0, %al
callq 0x2100
movq -0x8a8(%rbp), %rsi
addq $0x8, %rsi
leaq 0x8a71(%rip), %rdi      # 0xc253
movb $0x0, %al
callq 0x2100
leaq -0x888(%rbp), %rdi
callq 0x6f70
leaq -0x8b8(%rbp), %rdi
callq 0x6f70
leaq -0x8e8(%rbp), %rdi
leaq -0x8d8(%rbp), %rsi
callq 0x6a80
leaq -0x8d8(%rbp), %rdi
leaq 0x2cde(%rip), %rsi      # 0x6500
callq 0x6a00
leaq -0x8e8(%rbp), %rdi
leaq 0x2c7b(%rip), %rsi      # 0x64b0
callq 0x6a00
leaq -0x30(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x3de0
leaq 0x8a0f(%rip), %rdi      # 0xc25e
movb $0x0, %al
callq 0x2100
leaq 0x8a0d(%rip), %rdi      # 0xc26a
movb $0x0, %al
callq 0x2100
movl $0x0, -0x8ec(%rbp)
movl $0x0, -0x8f0(%rbp)
movl $0x0, -0x8f4(%rbp)
movl $0x0, -0x8f8(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x8c8(%rbp), %eax
jge 0x3afe
movq $0x0, -0x910(%rbp)
movq -0x8c0(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x908(%rbp)
movq -0x908(%rbp), %rsi
leaq -0x8e8(%rbp), %rdi
leaq 0x2bd5(%rip), %rdx      # 0x64b0
callq 0x6a40
movq %rax, -0x900(%rbp)
cmpq $0x0, %rax
je 0x3959
movq -0x908(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x900(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x3e20
cmpl $0x0, %eax
je 0x3952
movq -0x900(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x918(%rbp)
movq -0x908(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x920(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x920(%rbp), %rsi
callq 0x7350
movl -0x8f4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8f4(%rbp)
jmp 0x3954
jmp 0x3954
jmp 0x3af0
movq -0x908(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x3984
movq -0x908(%rbp), %rax
movq (%rax), %rdi
callq 0x5e90
movq %rax, %rcx
movq -0x908(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x908(%rbp), %rsi
leaq -0x8d8(%rbp), %rdi
leaq 0x2b67(%rip), %rdx      # 0x6500
callq 0x6a40
movq %rax, -0x900(%rbp)
cmpq $0x0, -0x900(%rbp)
jne 0x39f7
movq -0x908(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x930(%rbp)
movq -0x908(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x928(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x930(%rbp), %rsi
callq 0x7020
movl -0x8ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8ec(%rbp)
jmp 0x3aee
movq -0x900(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x3a22
movq -0x900(%rbp), %rax
movq (%rax), %rdi
callq 0x5e90
movq %rax, %rcx
movq -0x900(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x900(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x908(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x3e80
movq %rax, -0x910(%rbp)
movq -0x908(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x4(%rax), %eax
movq -0x910(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jg 0x3aa3
movq -0x908(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x940(%rbp)
movq -0x908(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x938(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x940(%rbp), %rsi
callq 0x7020
movl -0x8ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8ec(%rbp)
jmp 0x3ae2
movq -0x908(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x950(%rbp)
movq -0x910(%rbp), %rax
movq %rax, -0x948(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x950(%rbp), %rsi
callq 0x7200
movl -0x8f0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8f0(%rbp)
movq -0x910(%rbp), %rdi
callq 0x3d80
jmp 0x3af0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x3893
leaq 0x8774(%rip), %rdi      # 0xc279
movb $0x0, %al
callq 0x2100
leaq -0x8c8(%rbp), %rdi
leaq 0x29e6(%rip), %rsi      # 0x6500
callq 0x6a00
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x8d8(%rbp), %eax
jge 0x3bab
movq -0x8d0(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x858(%rbp)
movq -0x858(%rbp), %rsi
leaq -0x8c8(%rbp), %rdi
leaq 0x29a1(%rip), %rdx      # 0x6500
callq 0x6a40
cmpq $0x0, %rax
jne 0x3b9b
movq -0x858(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x958(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x958(%rbp), %rsi
callq 0x7300
movl -0x8f8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8f8(%rbp)
jmp 0x3b9d
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x3b26
leaq -0x30(%rbp), %rdi
callq 0x4060
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x59b0
movq %rax, -0x48(%rbp)
leaq -0x850(%rbp), %rdi
movq -0x40(%rbp), %rcx
addq $0x8, %rcx
movl $0x800, %esi            # imm = 0x800
leaq 0x86fb(%rip), %rdx      # 0xc2db
movb $0x0, %al
callq 0x2110
leaq -0x850(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x5560
leaq -0x850(%rbp), %rdi
movl -0x8ec(%rbp), %ecx
movl -0x8f0(%rbp), %r8d
movl -0x8f4(%rbp), %r9d
movl -0x8f8(%rbp), %r11d
movq -0x48(%rbp), %r10
addq $0x8, %r10
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
movl $0x800, %esi            # imm = 0x800
leaq 0x8650(%rip), %rdx      # 0xc288
movl %r11d, (%rsp)
movq %r10, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x2110
movq -0x38(%rbp), %rdi
callq 0x3d80
leaq -0x850(%rbp), %rdi
callq 0x40a0
movq %rax, -0x38(%rbp)
leaq -0x850(%rbp), %rdi
movq -0x40(%rbp), %rcx
addq $0x8, %rcx
movl $0x800, %esi            # imm = 0x800
leaq 0x864a(%rip), %rdx      # 0xc2ca
movb $0x0, %al
callq 0x2110
leaq -0x850(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x5560
movq -0x38(%rbp), %rsi
addq $0x8, %rsi
leaq 0x8599(%rip), %rdi      # 0xc243
movb $0x0, %al
callq 0x2100
leaq -0x850(%rbp), %rdi
movq -0x18(%rbp), %rcx
addq $0x8, %rcx
movq -0x40(%rbp), %r8
addq $0x8, %r8
movl $0x800, %esi            # imm = 0x800
leaq 0x8604(%rip), %rdx      # 0xc2d8
movb $0x0, %al
callq 0x2110
movq -0x38(%rbp), %rdi
callq 0x3d80
movq -0x48(%rbp), %rdi
callq 0x3d80
movq -0x40(%rbp), %rdi
callq 0x3d80
leaq -0x8d8(%rbp), %rdi
callq 0x6960
leaq -0x8e8(%rbp), %rdi
callq 0x6960
leaq -0x8c8(%rbp), %rdi
callq 0x6960
leaq -0x850(%rbp), %rdi
callq 0x40a0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x40e0
movq -0x8(%rbp), %rdi
callq 0x3d80
movq -0x10(%rbp), %rdi
callq 0x3d80
movq -0x38(%rbp), %rdi
callq 0x3d80
addq $0x970, %rsp            # imm = 0x970
popq %rbp
retq
nop