// File: /workspace/github_c_makefile_star2_O0/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
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x4e70
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5390
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x47c3
movq 0xb887(%rip), %rax      # 0xfff8
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x8, %rax
movq %rax, -0xb0(%rbp)
callq 0x2050
movl (%rax), %edi
callq 0x22a0
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rdx
movq %rax, %rcx
leaq 0x7c2c(%rip), %rsi      # 0xc3de
movb $0x0, %al
callq 0x21a0
movl $0x16, %edi
callq 0x2270
leaq 0x7c28(%rip), %rdi      # 0xc3f2
movb $0x0, %al
callq 0x2100
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
cltq
addq $0x6, %rax
subq $0x1, %rax
movl %eax, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x4aa0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movslq -0x24(%rbp), %rdx
callq 0x21d0
movq -0x20(%rbp), %rax
movslq -0x24(%rbp), %rcx
movb $0x2f, 0x8(%rax,%rcx)
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl 0x7ba5(%rip), %edx      # 0xc3d8
movl %edx, 0x8(%rax,%rcx)
movb 0x7b9f(%rip), %dl       # 0xc3dc
movb %dl, 0xc(%rax,%rcx)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x4(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x49b8
movq $0x0, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x73c0
movq %rax, -0x30(%rbp)
movzbl -0x58(%rbp), %eax
movl %eax, -0xbc(%rbp)
subl $0x10, %eax
je 0x494b
jmp 0x48a4
movl -0xbc(%rbp), %eax
subl $0x20, %eax
je 0x48e3
jmp 0x48b1
movl -0xbc(%rbp), %eax
subl $0x30, %eax
je 0x48f5
jmp 0x48be
movl -0xbc(%rbp), %eax
subl $0x40, %eax
je 0x4962
jmp 0x48cf
movl -0xbc(%rbp), %eax
subl $0x50, %eax
je 0x4996
jmp 0x4998
movq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
jmp 0x4905
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0x5390
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x4b10
movq %rax, -0x70(%rbp)
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movq -0x70(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x5560
jmp 0x4998
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movq -0x48(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x5560
jmp 0x4998
movq -0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0x5390
movq %rax, -0x78(%rbp)
movq -0x50(%rbp), %rdi
addq $0x8, %rdi
movq -0x78(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x5560
jmp 0x4998
jmp 0x4998
movq -0x78(%rbp), %rdi
callq 0x4ce0
movq -0x70(%rbp), %rdi
callq 0x4ce0
leaq -0x58(%rbp), %rdi
callq 0x76e0
jmp 0x4860
leaq 0x7a3c(%rip), %rdi      # 0xc3fb
movb $0x0, %al
callq 0x2100
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x4(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jae 0x4a5f
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x73c0
movq %rax, -0x30(%rbp)
movb -0x98(%rbp), %al
subb $0x50, %al
jne 0x4a39
jmp 0x4a25
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
callq 0x5700
movq -0xa8(%rbp), %rdi
callq 0x4ce0
movq -0xa0(%rbp), %rdi
callq 0x4ce0
leaq -0x98(%rbp), %rdi
callq 0x76e0
jmp 0x49e5
leaq 0x79a2(%rip), %rdi      # 0xc408
movb $0x0, %al
callq 0x2100
cmpl $0x0, -0x28(%rbp)
jle 0x4a80
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x5740
movq -0x10(%rbp), %rdi
callq 0x4ce0
movq -0x20(%rbp), %rdi
callq 0x4ce0
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)