// File: /workspace/github_c_makefile_star2_O2/mist64[P]cbm6502asm/src/macrodef.c
// Function: macro_def
void macro_def(char *macprmbgn, char **maclinptr, char *macargbgn, int mactype) {
/* mactype: 1=.MACRO 2=.REPT 3=.IRP 4=.IRPC */
	unsigned int i, l, m, macrocnt = 1;

	spechar = 'D';
	print = 1;
	outline((char)0, (char)0, (char *)0, (char)0, (char)1);
	newmac = (struct macdef *)malloc(
	        l = CHUNK); /* initial size of structure */
	                    /*
	                      save macro name
	                    */
	for (m = i = 0; s1[i]; i++) newmac->name[m++] = s1[i]; /* copy name */
	newmac->name[31] = newmac->name[m++] = 0; /* terminate name string */
	m = m > 32 ? 32 : m;                /* fix up if name was > 31 chars */
	if (!strlen(newmac->name))          /* check for missing name */
		display_error(error = 'D'); /* missing macro name */
	                                    /*
	                                      get macro's parameter list
	                                    */
	parses3(1);                         /* separate fields in operand */
	while (*s3 || s3[1])                /* copy parms to macro definition */
		newmac->name[m++] = *s3++;
	newmac->name[m++] = 0;
	newmac->name[m++] = 0;
	/*
	  store the macro definition lines
	*/
	i = 1;
	while (i) {
		get_line(macprmbgn, maclinptr,
		         macargbgn); /* get a source or macro exp line */
		print = 1;

		if (strcmp(s2, ".MACRO") ==
		            0 || /* check for nested macro defs */
		    strcmp(s2, ".IRP") == 0 ||
		    strcmp(s2, ".IRPC") == 0 || strcmp(s2, ".REPT") == 0)
			macrocnt++;
		if (strcmp(s2, ".ENDM") == 0 || strcmp(s2, ".ENDR") == 0)
			macrocnt--;

		if (!strcmp(s2, ".END"))
			error_msg(
			        ".END encountered during definition of macro: ",
			        newmac->name, 4);

		spechar = 'D';
		/* check for end of macro definition */
		if (((!strcmp(s2, ".ENDM") && mactype == 1) ||
		     (!strcmp(s2, ".ENDR") && mactype > 1)) &&
		    !macrocnt) {
			newmac->name[m++] =
			        0; /* add a null indicating macro end */
			newmac->size = m + 8;
			if (error) {
				free((char *)newmac); /* don't save macro if
				                         error */
				return;
			}
			newmac->nextmac = macro_head; /* link macro definition
			                                 into list */
			/* truncated memory block */
			tmpmac =
			        (struct macdef *)realloc((char *)newmac, m + 8);
			if (!(long)(macro_head = tmpmac))
				error_msg("Available memory exceeded",
				          newmac->name, 4);
			return; /* finished */
		}
		outline((char)0, (char)0, (char *)0, (char)0, (char)1);
		/* check for macro def in macro */
		if (m + 8 + LINELENG > l) { /* alloc more memory if needed */
			tmpmac = (struct macdef *)realloc((char *)newmac,
			                                  l += CHUNK);
			if (!(long)tmpmac) /* check if allocation sucessful */
				error_msg("Available memory exceeded",
				          newmac->name, 4);
			newmac = tmpmac; /* allocation was successful */
		}
		if (strlen(line)) {
			strcpy(&newmac->name[m], line); /* copy the line */
			m += 1 + strlen(line); /* add length of line and null */
		} else {
			newmac->name[m++] = ' '; /* blank line */
			newmac->name[m++] = 0;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %ecx, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
leaq 0x882e(%rip), %rax      # 0xdc26
movb $0x44, (%rax)
leaq 0x89fa(%rip), %rax      # 0xddfc
movl $0x1, (%rax)
pushq $0x1
popq %r14
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movl %r14d, %r8d
callq 0x72ed
movl $0x200, %edi            # imm = 0x200
callq 0x1180
leaq 0x8863(%rip), %rbx      # 0xdc90
movq %rax, (%rbx)
leaq 0x8661(%rip), %rax      # 0xda98
leal -0x1(%r14), %ecx
movq (%rax), %rdx
movb (%rdx,%rcx), %sil
movq (%rbx), %rdx
testb %sil, %sil
je 0x5454
movb %sil, 0xc(%rdx,%rcx)
incl %r14d
jmp 0x5437
xorl %eax, %eax
movb %al, 0xc(%rdx,%rcx)
movq (%rbx), %rcx
movb %al, 0x2b(%rcx)
cmpl $0x20, %r14d
pushq $0x20
popq %rbp
cmovbl %r14d, %ebp
cmpb %al, 0xc(%rcx)
jne 0x5482
leaq 0x8989(%rip), %rax      # 0xde00
movb $0x44, (%rax)
pushq $0x44
popq %rdi
callq 0x1e1c
pushq $0x1
popq %rdi
callq 0x4e5f
leaq 0x8c1f(%rip), %rax      # 0xe0b0
movq (%rax), %rdx
cmpb $0x0, (%rdx)
movl %ebp, %ecx
jne 0x54a1
cmpb $0x0, 0x1(%rdx)
je 0x54b5
leaq 0x1(%rdx), %rsi
movq %rsi, (%rax)
movb (%rdx), %dl
movq (%rbx), %rsi
incl %ebp
movb %dl, 0xc(%rsi,%rcx)
jmp 0x5491
movq (%rbx), %rax
leal 0x1(%rbp), %edx
xorl %esi, %esi
movb %sil, 0xc(%rax,%rcx)
movq (%rbx), %rax
addl $0x2, %ebp
movb %sil, 0xc(%rax,%rdx)
movl $0x200, %r13d           # imm = 0x200
leaq 0x44de(%rip), %r14      # 0x99b8
pushq $0x1
popq %rbx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
callq 0x5232
leaq 0x8904(%rip), %rax      # 0xddfc
movl $0x1, (%rax)
leaq 0x8ba3(%rip), %rax      # 0xe0a8
movq (%rax), %r15
movq %r15, %rdi
leaq 0x3dd6(%rip), %rsi      # 0x92e8
callq 0x1130
testl %eax, %eax
je 0x5554
movq %r15, %rdi
leaq 0x3dbe(%rip), %rsi      # 0x92e3
callq 0x1130
testl %eax, %eax
je 0x5554
movq %r15, %rdi
leaq 0x3da5(%rip), %rsi      # 0x92dd
callq 0x1130
testl %eax, %eax
je 0x5554
movq %r15, %rdi
leaq 0x3d78(%rip), %rsi      # 0x92c3
callq 0x1130
testl %eax, %eax
jne 0x5556
incl %ebx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1130
testl %eax, %eax
je 0x5578
movq %r15, %rdi
leaq 0x444f(%rip), %rsi      # 0x99be
callq 0x1130
testl %eax, %eax
jne 0x557a
decl %ebx
movq %r15, %rdi
leaq 0x3db5(%rip), %rsi      # 0x9339
callq 0x1130
testl %eax, %eax
jne 0x55b4
leaq 0x86fc(%rip), %rax      # 0xdc90
movq (%rax), %rsi
addq $0xc, %rsi
leaq 0x4422(%rip), %rdi      # 0x99c4
pushq $0x4
popq %rdx
callq 0x1ddf
leaq 0x8af7(%rip), %rax      # 0xe0a8
movq (%rax), %r15
leaq 0x866b(%rip), %rax      # 0xdc26
movb $0x44, (%rax)
movq %r15, %rdi
movq %r14, %r12
movq %r14, %rsi
callq 0x1130
movl %ebp, %r14d
cmpl $0x1, 0xc(%rsp)
jne 0x55da
testl %eax, %eax
je 0x55f4
movq %r15, %rdi
leaq 0x43da(%rip), %rsi      # 0x99be
callq 0x1130
cmpl $0x2, 0xc(%rsp)
jl 0x55fc
testl %eax, %eax
jne 0x55fc
testl %ebx, %ebx
je 0x56b3
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
pushq $0x1
popq %r8
callq 0x72ed
leal 0x8e(%rbp), %eax
cmpl %r13d, %eax
leaq 0x8673(%rip), %r15      # 0xdc90
jbe 0x5663
movq (%r15), %rdi
addl $0x200, %r13d           # imm = 0x200
movq %r13, %rsi
callq 0x11c0
leaq 0x8650(%rip), %rcx      # 0xdc88
movq %rax, (%rcx)
testq %rax, %rax
jne 0x5660
movq (%r15), %rsi
addq $0xc, %rsi
leaq 0x43a4(%rip), %rdi      # 0x99f2
pushq $0x4
popq %rdx
callq 0x1ddf
leaq 0x862b(%rip), %rax      # 0xdc88
movq (%rax), %rax
movq %rax, (%r15)
movq (%r15), %rdi
addq $0xc, %rdi
leaq 0x8a4f(%rip), %rax      # 0xe0c0
cmpb $0x0, (%rax)
je 0x5698
addq %r14, %rdi
movq %rax, %r14
movq %rax, %rsi
callq 0x1080
movq %r14, %rdi
callq 0x10b0
addl %eax, %ebp
incl %ebp
movq %r12, %r14
jmp 0x54dd
leal 0x1(%rbp), %eax
movb $0x20, (%rdi,%r14)
movq (%r15), %rcx
addl $0x2, %ebp
movb $0x0, 0xc(%rcx,%rax)
movq %r12, %r14
jmp 0x54dd
leaq 0x85d6(%rip), %r15      # 0xdc90
movq (%r15), %rax
movb $0x0, 0xc(%rax,%r14)
addl $0x9, %ebp
movq (%r15), %rdi
movl %ebp, 0x8(%rdi)
leaq 0x872d(%rip), %rax      # 0xde00
cmpb $0x0, (%rax)
je 0x56eb
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1040
leaq 0x83c6(%rip), %rbx      # 0xdab8
movq (%rbx), %rax
movq %rax, (%rdi)
movq (%r15), %rdi
movl %ebp, %esi
callq 0x11c0
leaq 0x857f(%rip), %rcx      # 0xdc88
movq %rax, (%rcx)
movq %rax, (%rbx)
testq %rax, %rax
je 0x5723
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rsi
addq $0xc, %rsi
leaq 0x42c1(%rip), %rdi      # 0x99f2
pushq $0x4
popq %rdx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1ddf