// File: /workspace/github_c_makefile_star2_O1/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 %rbx
subq $0x1018, %rsp           # imm = 0x1018
movq %rdi, %r14
leaq 0x10(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x10b0
movl 0xc(%r14), %ebx
shll $0x8, %ebx
movq (%r14), %rdx
movl 0x8(%r14), %ecx
leaq 0x15a7(%rip), %rsi      # 0x3108
leaq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x1120
testl %eax, %eax
jle 0x1c23
movq 0x8(%rsp), %rdi
leaq 0x15be(%rip), %rsi      # 0x313f
callq 0x1150
testq %rax, %rax
jne 0x1ccb
movq 0x3462(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x1480(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x146d(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x156b(%rip), %rsi      # 0x3142
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x1465(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x144f(%rip), %rsi      # 0x3050
leaq 0x152b(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x1453(%rip), %rsi      # 0x306c
movl $0x25, %edx
jmp 0x1e78
movq 0x33ce(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x13ec(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%r14), %rcx
leaq 0x13d9(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%r14), %rdi
leaq 0x14ac(%rip), %rsi      # 0x3112
movl %ebx, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rcx
leaq 0x13d4(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%r14), %rdi
leaq 0x13be(%rip), %rsi      # 0x3050
leaq 0x149a(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rdi
leaq 0x13c2(%rip), %rsi      # 0x306c
movl $0x22, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10c0
movq (%r14), %rcx
jmp 0x1e8f
movq %rax, %rdi
callq 0x1110
movl %eax, %ebp
incl %ebx
leaq 0x10(%rsp), %r14
callq 0x11c0
cltq
imulq $-0x7f7f7f7f, %rax, %rsi # imm = 0x80808081
shrq $0x20, %rsi
addl %eax, %esi
movl %esi, %ecx
shrl $0x1f, %ecx
sarl $0x7, %esi
addl %ecx, %esi
movl %esi, %ecx
shll $0x8, %ecx
subl %ecx, %esi
addl %eax, %esi
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
callq 0x10b0
movl %ebx, %r15d
decl %r15d
jle 0x1d32
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rsi
callq 0x1060
cmpq $-0x1, %rax
jne 0x1d13
jmp 0x1de7
movl %ebp, %edi
callq 0x1140
cmpl $-0x1, %eax
je 0x1eaf
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
cmpq $-0x1, %rax
jne 0x1cdc
movq 0x329e(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x12bc(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x12a9(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x1422(%rip), %rsi      # 0x31bd
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x12a1(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x128b(%rip), %rsi      # 0x3050
leaq 0x1367(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x128f(%rip), %rsi      # 0x306c
movl $0x38, %edx
jmp 0x1e78
movq 0x320a(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x1228(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x1215(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x132b(%rip), %rsi      # 0x315a
movl %ebp, %ecx
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x120b(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x11f5(%rip), %rsi      # 0x3050
leaq 0x12d1(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x11f9(%rip), %rsi      # 0x306c
movl $0x30, %edx
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0x10c0
movq (%rbx), %rcx
leaq 0x11e3(%rip), %rdi      # 0x3079
movl $0xa, %esi
movl $0x1, %edx
callq 0x1190
movl $0x1, %edi
callq 0x1180
movq 0x3142(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x1160(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x114d(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x1294(%rip), %rsi      # 0x318b
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x1145(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x112f(%rip), %rsi      # 0x3050
leaq 0x120b(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x1133(%rip), %rsi      # 0x306c
movl $0x35, %edx
jmp 0x1e78