// File: /workspace/github_c_makefile_star2_O0/alexgirao[P]aimant/aimant.c
// Function: read_pid
static int read_pid(const char *pid_file, int *pid)
{
	int fd;
	char buf[100];
	int bufsz = sizeof(buf);
	struct stat st[1];
	int open_try = 1;

	for (;;) {
		if ((fd = open(pid_file, O_RDONLY)) < 0) {
			int save_errno = errno;
			assert(fd == -1);
			if (errno == ENOENT && open_try < NUMBER_OF_OPEN_TRIES) {
				DEBUG("open(pid_file=[%s], O_RDONLY) got ENOENT (file does not exist, retrying), try %i of %i", pid_file, open_try+1, NUMBER_OF_OPEN_TRIES);
				usleep(100 * 1000);
				open_try++;
				continue;
			}
			DEBUG("open(pid_file=[%s], O_RDONLY), errno=%i", pid_file, save_errno);
			errno = save_errno;
			perror(pid_file);
			return -1;
		}
		break;
	}
	if (fstat(fd, st) < 0) {
		int save_errno = errno;
		DEBUG("fstat(fd=[%i, from [%s]], st), errno=%i", fd, pid_file, save_errno);
		errno = save_errno;
		perror(pid_file);
		return -1;
	}

	assert(st->st_size < bufsz);

	assert(read_exact(fd, buf, st->st_size) == st->st_size);
	buf[st->st_size] = 0;

	assert(close(fd) == 0);
	fd = -1;

	if (*pid) *pid = atoi(buf);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x64, -0x84(%rbp)
movl $0x1, -0x124(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movb $0x0, %al
callq 0x22f0
movl %eax, -0x1c(%rbp)
cmpl $0x0, %eax
jge 0x4dbb
callq 0x2070
movl (%rax), %eax
movl %eax, -0x128(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x4ce5
jmp 0x4d04
leaq 0xbd42(%rip), %rdi      # 0x10a2e
leaq 0xb369(%rip), %rsi      # 0x1005c
movl $0x4e7, %edx            # imm = 0x4E7
leaq 0xc02c(%rip), %rcx      # 0x10d2b
callq 0x21a0
callq 0x2070
cmpl $0x2, (%rax)
jne 0x4d66
cmpl $0xa, -0x124(%rbp)
jge 0x4d66
jmp 0x4d19
movq -0x10(%rbp), %rcx
movl -0x124(%rbp), %r8d
addl $0x1, %r8d
leaq 0xb32d(%rip), %rdi      # 0x1005c
movl $0x4e9, %esi            # imm = 0x4E9
leaq 0xc012(%rip), %rdx      # 0x10d4d
movl $0xa, %r9d
movb $0x0, %al
callq 0xdfe0
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x23c0
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x4cb7
jmp 0x4d68
movq -0x10(%rbp), %rcx
movl -0x128(%rbp), %r8d
leaq 0xb2e2(%rip), %rdi      # 0x1005c
movl $0x4ee, %esi            # imm = 0x4EE
leaq 0xc01e(%rip), %rdx      # 0x10da4
movb $0x0, %al
callq 0xdfe0
movl -0x128(%rbp), %eax
movl %eax, -0x130(%rbp)
callq 0x2070
movl -0x130(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x2310
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4f0b
jmp 0x4dbd
movl -0x1c(%rbp), %edi
leaq -0x120(%rbp), %rsi
callq 0x2360
cmpl $0x0, %eax
jge 0x4e34
callq 0x2070
movl (%rax), %eax
movl %eax, -0x12c(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %r8
movl -0x12c(%rbp), %r9d
leaq 0xb269(%rip), %rdi      # 0x1005c
movl $0x4f7, %esi            # imm = 0x4F7
leaq 0xbfcd(%rip), %rdx      # 0x10dcc
movb $0x0, %al
callq 0xdfe0
movl -0x12c(%rbp), %eax
movl %eax, -0x134(%rbp)
callq 0x2070
movl -0x134(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x2310
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x4f0b
movq -0xf0(%rbp), %rax
movslq -0x84(%rbp), %rcx
cmpq %rcx, %rax
jge 0x4e49
jmp 0x4e68
leaq 0xbfa4(%rip), %rdi      # 0x10df4
leaq 0xb205(%rip), %rsi      # 0x1005c
movl $0x4fd, %edx            # imm = 0x4FD
leaq 0xbec8(%rip), %rcx      # 0x10d2b
callq 0x21a0
movl -0x1c(%rbp), %edi
leaq -0x80(%rbp), %rsi
movq -0xf0(%rbp), %rax
movl %eax, %edx
callq 0x68b0
cltq
cmpq -0xf0(%rbp), %rax
jne 0x4e8a
jmp 0x4ea9
leaq 0xbf77(%rip), %rdi      # 0x10e08
leaq 0xb1c4(%rip), %rsi      # 0x1005c
movl $0x4ff, %edx            # imm = 0x4FF
leaq 0xbe87(%rip), %rcx      # 0x10d2b
callq 0x21a0
movq -0xf0(%rbp), %rax
movb $0x0, -0x80(%rbp,%rax)
movl -0x1c(%rbp), %edi
callq 0x21d0
cmpl $0x0, %eax
jne 0x4ec4
jmp 0x4ee3
leaq 0xbc70(%rip), %rdi      # 0x10b3b
leaq 0xb18a(%rip), %rsi      # 0x1005c
movl $0x502, %edx            # imm = 0x502
leaq 0xbe4d(%rip), %rcx      # 0x10d2b
callq 0x21a0
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
je 0x4f04
leaq -0x80(%rbp), %rdi
callq 0x2340
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw (%rax,%rax)