// File: /workspace/github_c_makefile_star2_O2/cirocosta[P]stress/src/write.c
// Function: thread_handler
void*
thread_handler(void* arg)
{
	thread_arg_t* targ = (thread_arg_t*)arg;
	char file_buffer[CHUNK_SIZE] = { 0 };
	unsigned long writes = targ->count * _STRESS_MB(1) / CHUNK_SIZE;
	int i = writes;
	char* filename;
	FILE* fp;

	_STRESS_MUST(
	  asprintf(&filename, "%s/file%d", targ->base_dir, targ->thread_num) >
	    0,
	  "Couldn't create name for file %d",
	  i);

	_STRESS_MUST(
	  (fp = fopen(filename, "w+")), "Couldn't create file %s", filename);

	int fd = fileno(fp);

	while (1) {
		memset(file_buffer, rand() % 255, CHUNK_SIZE);
		while (i-- > 0) {
			_STRESS_MUST(
			  write(fd, file_buffer, CHUNK_SIZE) != -1,
			  "Couldn't properly write chunk to file %s (fd=%d)",
			  filename,
			  fd);
		}
		_STRESS_MUST(
		  fsync(fd) != -1,
		  "Couldn't sync filedescriptor with disk (fname=%s)",
		  filename);
		_STRESS_MUST(lseek(fd, 0, SEEK_SET) != -1,
		             "Couldn't seek to init of file %s",
		             filename);
		i = writes;
	}

	return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1010, %rsp           # imm = 0x1010
movl 0xc(%rdi), %ebx
shll $0x8, %ebx
movq (%rdi), %rdx
movl 0x8(%rdi), %ecx
leaq 0x9e8(%rip), %rsi       # 0x2108
leaq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x1120
testl %eax, %eax
jle 0x17d6
movq 0x8(%rsp), %rdi
leaq 0x9ff(%rip), %rsi       # 0x213f
callq 0x1150
testq %rax, %rax
jne 0x187f
movq 0x28a3(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x8c1(%rip), %rdi       # 0x2020
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rcx
leaq 0x8af(%rip), %rdi       # 0x2026
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
movq 0x8(%rsp), %rdx
leaq 0x9b1(%rip), %rsi       # 0x2142
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rcx
leaq 0x8ab(%rip), %rdi       # 0x204d
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
leaq 0x899(%rip), %rsi       # 0x2050
leaq 0x975(%rip), %rdx       # 0x2133
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rdi
leaq 0x89d(%rip), %rsi       # 0x206c
pushq $0x25
jmp 0x19fe
movq 0x281b(%rip), %r15      # 0x3ff8
movq (%r15), %rcx
leaq 0x839(%rip), %rdi       # 0x2020
pushq $0x9
popq %rsi
pushq $0x1
popq %r14
movq %r14, %rdx
callq 0x1190
movq (%r15), %rcx
leaq 0x826(%rip), %rdi       # 0x2026
pushq $0x3
popq %rsi
movq %r14, %rdx
callq 0x1190
movq (%r15), %rdi
leaq 0x8fd(%rip), %rsi       # 0x2112
movl %ebx, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rcx
leaq 0x825(%rip), %rdi       # 0x204d
pushq $0x2
popq %rsi
movq %r14, %rdx
callq 0x1190
movq (%r15), %rdi
leaq 0x813(%rip), %rsi       # 0x2050
leaq 0x8ef(%rip), %rdx       # 0x2133
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rdi
leaq 0x817(%rip), %rsi       # 0x206c
pushq $0x22
popq %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r15), %rsi
pushq $0xa
popq %rdi
callq 0x10c0
movq (%r15), %rcx
leaq 0x805(%rip), %rdi       # 0x2079
pushq $0xa
popq %rsi
movq %r14, %rdx
jmp 0x1a21
movq %rax, %rdi
callq 0x1110
movl %eax, %ebp
incl %ebx
movl $0xff, %r15d
leaq 0x10(%rsp), %r14
callq 0x11c0
cltd
idivl %r15d
movl %edx, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x10b0
movl %ebx, %r12d
decl %r12d
jle 0x18d0
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rsi
callq 0x1060
cmpq $-0x1, %rax
jne 0x18b1
jmp 0x1979
movl %ebp, %edi
callq 0x1140
cmpl $-0x1, %eax
je 0x1a2e
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
cmpq $-0x1, %rax
jne 0x1896
movq 0x2700(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x71e(%rip), %rdi       # 0x2020
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rcx
leaq 0x70c(%rip), %rdi       # 0x2026
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
movq 0x8(%rsp), %rdx
leaq 0x889(%rip), %rsi       # 0x21bd
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rcx
leaq 0x708(%rip), %rdi       # 0x204d
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
leaq 0x6f6(%rip), %rsi       # 0x2050
leaq 0x7d2(%rip), %rdx       # 0x2133
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rdi
leaq 0x6fa(%rip), %rsi       # 0x206c
pushq $0x38
jmp 0x19fe
movq 0x2678(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x696(%rip), %rdi       # 0x2020
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rcx
leaq 0x684(%rip), %rdi       # 0x2026
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
movq 0x8(%rsp), %rdx
leaq 0x79e(%rip), %rsi       # 0x215a
movl %ebp, %ecx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rcx
leaq 0x67e(%rip), %rdi       # 0x204d
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
leaq 0x66c(%rip), %rsi       # 0x2050
leaq 0x748(%rip), %rdx       # 0x2133
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rdi
leaq 0x670(%rip), %rsi       # 0x206c
pushq $0x30
popq %rdx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x10c0
movq (%r14), %rcx
leaq 0x65e(%rip), %rdi       # 0x2079
pushq $0xa
popq %rsi
movq %rbx, %rdx
callq 0x1190
pushq $0x1
popq %rdi
callq 0x1180
movq 0x25c3(%rip), %r14      # 0x3ff8
movq (%r14), %rcx
leaq 0x5e1(%rip), %rdi       # 0x2020
pushq $0x9
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rcx
leaq 0x5cf(%rip), %rdi       # 0x2026
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
movq 0x8(%rsp), %rdx
leaq 0x71a(%rip), %rsi       # 0x218b
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rcx
leaq 0x5cb(%rip), %rdi       # 0x204d
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x1190
movq (%r14), %rdi
leaq 0x5b9(%rip), %rsi       # 0x2050
leaq 0x695(%rip), %rdx       # 0x2133
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rdi
leaq 0x5bd(%rip), %rsi       # 0x206c
pushq $0x35
jmp 0x19fe