// File: /workspace/github_c_makefile_star2_O0/aparrish[P]rwet-examples-c/randlines.c
// Function: main
int main(int argc, char* argv[]) {

	srand(time(NULL));

	/* read in lines */
	line_t* head;
	line_t* last = NULL;
	line_t* current = NULL;
	while (!feof(stdin)) {
		/* malloc a new line_t */
		current = malloc(sizeof(line_t));
		current->next = NULL;
		current->line_no = line_count;

		/* ap_getline populates current->line with pointer to malloced char* */
		current->len = ap_getline(&(current->line), stdin);

		/* set this line_t as the 'next' of previous line_t */
		if (last == NULL) {
			head = current;
		}
		else {
			last->next = current;
		}
		last = current;
		line_count++;
	}

	/* print lines out randomly, removing from linked list as it goes */
	int offset;
	int i;
	line_t* current_line = NULL;
	line_t* prev_line = NULL;
	while (line_count > 0) {
		prev_line = NULL;
		current_line = head;
		offset = rand() % line_count; /* random offset from head of list */

		/* traverse the list until we get to offset, keeping track of previous
 		 * node (so we can remove the node at offset) */
		for (i = 0; i < offset; i++) {
			prev_line = current_line;
			current_line = current_line->next;
		}
		printf("%s", current_line->line);

		/* if offset was zero, the next line is the new head of the list */
		if (offset == 0) 
			head = current_line->next;

		/* the previous line's next now points to the current line's next */
		if (prev_line != NULL)
			prev_line->next = current_line->next;

		/* free the line and the struct */
		free(current_line->line);
		free(current_line);

		line_count--;
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x1070
movl %eax, %edi
callq 0x1050
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq 0x2de3(%rip), %rax      # 0x3fe0
movq (%rax), %rdi
callq 0x1060
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1213
jmp 0x128f
movl $0x18, %edi
callq 0x1080
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x10(%rax)
movl 0x2e41(%rip), %ecx      # 0x4074
movq -0x28(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x28(%rbp), %rdi
movq 0x2d9b(%rip), %rax      # 0x3fe0
movq (%rax), %rsi
callq 0x1370
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
cmpq $0x0, -0x20(%rbp)
jne 0x1267
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x1273
movq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x20(%rbp)
movl 0x2df3(%rip), %eax      # 0x4074
addl $0x1, %eax
movl %eax, 0x2dea(%rip)      # 0x4074
jmp 0x11f6
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x0, 0x2dce(%rip)      # 0x4074
jle 0x1360
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
callq 0x10b0
cltd
idivl 0x2dac(%rip)            # 0x4074
movl %edx, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x12f9
movq -0x38(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x12d2
movq -0x38(%rbp), %rax
movq (%rax), %rsi
leaq 0xcfd(%rip), %rdi       # 0x2004
movb $0x0, %al
callq 0x1040
cmpl $0x0, -0x2c(%rbp)
jne 0x1320
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x1337
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x1030
movq -0x38(%rbp), %rdi
callq 0x1030
movl 0x2d22(%rip), %eax      # 0x4074
addl $-0x1, %eax
movl %eax, 0x2d19(%rip)      # 0x4074
jmp 0x129f
xorl %eax, %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)