// File: /workspace/github_c_makefile_star2_O3/findstr[P]version-database/patch.c
// Function: patch
void
patch(struct patch_args *args)
{
	dr_t patch;
	dr_t outdir, outtemp = NULL;
	int size, dfn = 0;
	const uint8_t *p, *e;
	outdir = args->output;
	checkout(args->hash, outdir);
	patch = dir_readfile(str(args->patch), NULL);
	if (patch == NULL) {
		fprintf(stderr, "patch file '%s' %s\n",
			args->patch->buf, strerror(errno));
		exit(EINVAL);
	}
	//prepare temp file
	printf("DFF/DFX\n");
	size = outdir->size;
	outtemp = dr_new(size + 1 + sizeof(TEMP) - 1, NULL);
	memcpy(outtemp->buf, outdir->buf, size);
	outtemp->buf[size] = '/';
	memcpy(&outtemp->buf[size+1], TEMP, sizeof(TEMP) - 1);
	p = patch->buf;
	e = p + patch->size;
	while (p < e) {
		struct CTRL ctrl;
		dr_t namea, patchd;
		dr_t newfile = NULL, oldfile = NULL;
		//printf("offset:%ld\n", p - patch->buf);
		p = ctrl_read(p, &ctrl);
		switch (ctrl.act) {
		case CTRL_DFF:
			namea = ctrl.u.dff.name;
			patchd = ctrl.u.dff.patch;
			goto diff;
		case CTRL_DFX:
			namea = ctrl.u.dfx.namea;
			patchd = ctrl.u.dfx.patch;
			diff:
			++dfn;
			oldfile = dir_readfile(str(namea), outdir);
			newfile = patch_content(oldfile, patchd);
			//printf("ref:%d\n", newfile->ref);
			dir_writefile(str(ctrl.u.dff.name), newfile, outtemp);
			break;
		case CTRL_NEW:
			dir_writefile(str(ctrl.u.new.name),
				ctrl.u.new.data, outtemp);
			break;
		case CTRL_CPY:
			namea = ctrl.u.cpy.namea;
			oldfile = dir_readfile(str(namea), outdir);
			dir_writefile(str(ctrl.u.cpy.name), oldfile, outtemp);
			break;
		case CTRL_DEL:
			break;
		}
		dr_unref(oldfile);
		dr_unref(newfile);
		ctrl_destroy(&ctrl);
	}
	printf("NEW/MOV/DEL\n");
	p = patch->buf;
	e = p + patch->size;
	while (p < e) {
		struct CTRL ctrl;
		dr_t newfile = NULL, oldfile = NULL;
		p = ctrl_read(p, &ctrl);
		switch (ctrl.act) {
		case CTRL_DEL:
			dir_remove(str(ctrl.u.del.name), outdir);
			break;
		}
		dr_unref(oldfile);
		dr_unref(newfile);
		ctrl_destroy(&ctrl);
	}
	printf("apply DFF/DFX result\n");
	if (dfn > 0)
		dir_movdir(outtemp, outdir);
	dr_unref(patch);
	dr_unref(outtemp);
	return ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x10(%rbx), %r13
movq %r13, %rsi
callq 0x4eb4
movq 0x8(%rbx), %rdi
addq $0x8, %rdi
xorl %esi, %esi
callq 0x52b7
testq %rax, %rax
je 0x4dbc
movq %rax, %r15
leaq 0x5ae7(%rip), %rdi      # 0xa47f
callq 0x20a0
movslq 0x4(%r13), %rbx
leal 0x6(%rbx), %eax
movslq %eax, %r14
leaq 0xc(%r14), %rdi
callq 0x2200
movq %rax, %r12
movl $0x1, (%rax)
movl %r14d, 0x4(%rax)
leaq 0x8(%rax), %rdi
movb $0x0, 0x8(%rax,%r14)
leaq 0x8(%r13), %rsi
movq %rbx, %rdx
callq 0x21d0
movb $0x2f, 0x8(%r12,%rbx)
movl $0x6d65745f, 0x9(%r12,%rbx) # imm = 0x6D65745F
movq %r12, -0x30(%rbp)
movb $0x70, 0xd(%r12,%rbx)
leaq 0x8(%r15), %rcx
movslq 0x4(%r15), %rax
xorl %r12d, %r12d
testq %rax, %rax
movq %r15, -0x68(%rbp)
movq %rcx, -0x80(%rbp)
jle 0x4c79
leaq (%r15,%rax), %rbx
addq $0x8, %rbx
leaq -0x58(%rbp), %r14
leaq -0x60(%rbp), %r15
movl $0x0, -0x34(%rbp)
movq %rcx, %rdi
movq %r13, -0x90(%rbp)
movq %rbx, -0x88(%rbp)
movq %r15, %rsi
callq 0x6bc0
movzbl -0x60(%rbp), %edx
addl $-0x10, %edx
roll $0x1c, %edx
cmpl $0x3, %edx
movq %rax, -0x98(%rbp)
ja 0x4c59
leaq 0x597f(%rip), %rcx      # 0xa3d8
movslq (%rcx,%rdx,4), %rdx
addq %rcx, %rdx
movq %r14, %rax
leaq -0x50(%rbp), %rcx
jmpq *%rdx
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x8, %rdi
movq -0x30(%rbp), %rdx
callq 0x53d9
jmp 0x4c59
leaq -0x50(%rbp), %rax
leaq -0x48(%rbp), %rcx
movq (%rax), %rdi
movq (%rcx), %r12
addq $0x8, %rdi
movq %r13, %rsi
callq 0x52b7
movq %rax, -0x78(%rbp)
leaq 0x8(%r12), %rdi
movslq 0x4(%r12), %rax
leaq (%r12,%rax), %r13
addq $0x8, %r13
leaq -0x6c(%rbp), %rsi
callq 0x7197
movq %rax, %r14
movslq -0x6c(%rbp), %rbx
leaq 0xc(%rbx), %rdi
callq 0x2200
movl $0x1, (%rax)
movl %ebx, 0x4(%rax)
movq %rax, -0x40(%rbp)
movb $0x0, 0x8(%rax,%rbx)
movl 0x4(%r12), %esi
leaq 0x5912(%rip), %rdi      # 0xa3fc
movl %ebx, %edx
xorl %eax, %eax
callq 0x2110
cmpq %r13, %r14
jae 0x4bae
movq %r13, %r15
movq -0x40(%rbp), %r13
addq $0x8, %r13
movq -0x78(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xa0(%rbp)
movq %r13, %r12
movq %r14, %rdi
leaq -0xb8(%rbp), %rsi
callq 0x7405
movq %rax, %r14
movzbl -0xb8(%rbp), %eax
cmpl $0x2, %eax
je 0x4b6f
cmpl $0x1, %eax
jne 0x4d5f
movl -0xac(%rbp), %ebx
movq -0x40(%rbp), %rax
movslq 0x4(%rax), %rax
movq %r13, %rcx
subq %r12, %rcx
addq %rax, %rcx
cmpq %rbx, %rcx
jl 0x4d7e
movl -0xb0(%rbp), %esi
addq -0xa0(%rbp), %rsi
jmp 0x4b97
movslq -0xa8(%rbp), %rbx
movq -0x40(%rbp), %rax
movslq 0x4(%rax), %rax
movq %r13, %rcx
subq %r12, %rcx
addq %rax, %rcx
cmpq %rbx, %rcx
jl 0x4d40
movq -0xb0(%rbp), %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x21d0
addq %rbx, %r12
cmpq %r15, %r14
jb 0x4b19
incl -0x34(%rbp)
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movq -0x40(%rbp), %r15
movq %r15, %rsi
movq -0x30(%rbp), %rdx
callq 0x53d9
movq -0x90(%rbp), %r12
movq -0x88(%rbp), %rbx
leaq -0x58(%rbp), %r14
movq -0x78(%rbp), %r13
jmp 0x4c0e
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movq %r13, %r12
movq %r13, %rsi
callq 0x52b7
movq %rax, %r13
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movq %rax, %rsi
movq -0x30(%rbp), %rdx
callq 0x53d9
xorl %r15d, %r15d
testq %r13, %r13
movq %r13, %rdi
movq %r12, %r13
je 0x4c32
movl (%rdi), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%rdi)
cmpl $0x1, %ecx
jg 0x4c32
testl %eax, %eax
jne 0x4d9d
callq 0x2030
testq %r15, %r15
je 0x4c55
movl (%r15), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%r15)
cmpl $0x1, %ecx
jg 0x4c55
testl %eax, %eax
jne 0x4d9d
movq %r15, %rdi
callq 0x2030
leaq -0x60(%rbp), %r15
movq %r15, %rdi
callq 0x6f8b
movq -0x98(%rbp), %rdi
cmpq %rbx, %rdi
jb 0x4a30
cmpl $0x0, -0x34(%rbp)
setg %r12b
leaq 0x5807(%rip), %rdi      # 0xa487
callq 0x20a0
movq -0x68(%rbp), %rax
movslq 0x4(%rax), %rax
testq %rax, %rax
movq -0x80(%rbp), %r14
jle 0x4cd7
movq -0x68(%rbp), %rcx
leaq (%rcx,%rax), %r15
addq $0x8, %r15
leaq -0x60(%rbp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6bc0
movq %rax, %r14
cmpb $0x50, -0x60(%rbp)
jne 0x4cca
movq -0x58(%rbp), %rdi
addq $0x8, %rdi
movq %r13, %rsi
callq 0x54f3
movq %rbx, %rdi
callq 0x6f8b
cmpq %r15, %r14
jb 0x4ca6
leaq 0x57b5(%rip), %rdi      # 0xa493
callq 0x20a0
testb %r12b, %r12b
je 0x4cf4
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x5521
movq -0x68(%rbp), %rdi
movl (%rdi), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%rdi)
cmpl $0x1, %ecx
jg 0x4d11
testl %eax, %eax
jne 0x4d9d
callq 0x2030
movq -0x30(%rbp), %rdx
movl (%rdx), %ecx
leal -0x1(%rcx), %eax
movl %eax, (%rdx)
cmpl $0x1, %ecx
jg 0x4d2e
testl %eax, %eax
jne 0x4d9d
movq -0x30(%rbp), %rdi
callq 0x2030
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5717(%rip), %rdi      # 0xa45e
leaq 0x56e9(%rip), %rsi      # 0xa437
leaq 0x56ea(%rip), %rcx      # 0xa43f
movl $0x27, %edx
callq 0x2130
leaq 0x5c2e(%rip), %rdi      # 0xa994
leaq 0x56ca(%rip), %rsi      # 0xa437
leaq 0x56cb(%rip), %rcx      # 0xa43f
movl $0x2b, %edx
callq 0x2130
leaq 0x568f(%rip), %rdi      # 0xa414
leaq 0x56ab(%rip), %rsi      # 0xa437
leaq 0x56ac(%rip), %rcx      # 0xa43f
movl $0x22, %edx
callq 0x2130
leaq 0x52dd(%rip), %rdi      # 0xa081
leaq 0x52e2(%rip), %rsi      # 0xa08d
leaq 0x52e2(%rip), %rcx      # 0xa094
movl $0x2d, %edx
callq 0x2130
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x240a