// File: /workspace/github_c_makefile_star2_O3/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
movl 0xc(%rdi), %ebx
shll $0x8, %ebx
movq (%rdi), %rdx
movl 0x8(%rdi), %ecx
leaq 0x15ba(%rip), %rsi      # 0x3108
leaq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0x1120
testl %eax, %eax
jle 0x1c10
movq 0x8(%rsp), %rdi
leaq 0x15d1(%rip), %rsi      # 0x313f
callq 0x1150
testq %rax, %rax
jne 0x1cb8
movq 0x3475(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x1493(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x1480(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x157e(%rip), %rsi      # 0x3142
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x1478(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x1462(%rip), %rsi      # 0x3050
leaq 0x153e(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x1466(%rip), %rsi      # 0x306c
movl $0x25, %edx
jmp 0x1e65
movq 0x33e1(%rip), %r14      # 0x4ff8
movq (%r14), %rcx
leaq 0x13ff(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%r14), %rcx
leaq 0x13ec(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%r14), %rdi
leaq 0x14bf(%rip), %rsi      # 0x3112
movl %ebx, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rcx
leaq 0x13e7(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%r14), %rdi
leaq 0x13d1(%rip), %rsi      # 0x3050
leaq 0x14ad(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rdi
leaq 0x13d5(%rip), %rsi      # 0x306c
movl $0x22, %edx
xorl %eax, %eax
callq 0x10f0
movq (%r14), %rsi
movl $0xa, %edi
callq 0x10c0
movq (%r14), %rcx
jmp 0x1e7c
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 0x1d1f
movl $0x1000, %edx           # imm = 0x1000
movl %ebp, %edi
movq %r14, %rsi
callq 0x1060
cmpq $-0x1, %rax
jne 0x1d00
jmp 0x1dd4
movl %ebp, %edi
callq 0x1140
cmpl $-0x1, %eax
je 0x1e9c
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10a0
cmpq $-0x1, %rax
jne 0x1cc9
movq 0x32b1(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x12cf(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x12bc(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x1435(%rip), %rsi      # 0x31bd
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x12b4(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x129e(%rip), %rsi      # 0x3050
leaq 0x137a(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x12a2(%rip), %rsi      # 0x306c
movl $0x38, %edx
jmp 0x1e65
movq 0x321d(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x123b(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x1228(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x133e(%rip), %rsi      # 0x315a
movl %ebp, %ecx
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x121e(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x1208(%rip), %rsi      # 0x3050
leaq 0x12e4(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x120c(%rip), %rsi      # 0x306c
movl $0x30, %edx
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rsi
movl $0xa, %edi
callq 0x10c0
movq (%rbx), %rcx
leaq 0x11f6(%rip), %rdi      # 0x3079
movl $0xa, %esi
movl $0x1, %edx
callq 0x1190
movl $0x1, %edi
callq 0x1180
movq 0x3155(%rip), %rbx      # 0x4ff8
movq (%rbx), %rcx
leaq 0x1173(%rip), %rdi      # 0x3020
movl $0x9, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rcx
leaq 0x1160(%rip), %rdi      # 0x3026
movl $0x3, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
movq 0x8(%rsp), %rdx
leaq 0x12a7(%rip), %rsi      # 0x318b
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rcx
leaq 0x1158(%rip), %rdi      # 0x304d
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movq (%rbx), %rdi
leaq 0x1142(%rip), %rsi      # 0x3050
leaq 0x121e(%rip), %rdx      # 0x3133
xorl %eax, %eax
callq 0x10f0
movq (%rbx), %rdi
leaq 0x1146(%rip), %rsi      # 0x306c
movl $0x35, %edx
jmp 0x1e65