// File: /workspace/github_c_makefile_star2_O0/buf1024[P]mysch/util.c
// Function: is_prog_running
int is_prog_running(const char* name)
{
    if(name == NULL || name[0] == 0) return -1;

    char file[MAX_PATH_LEN] = { 0 };
    snprintf(file, MAX_PATH_LEN - 1, "/tmp/%s.running", name);

    umask(0111);

    int fd = open(file, O_RDWR | O_CREAT | O_SYNC, 0666);
    if (fd < 0) {
        return -1;
    }

    struct flock lck = {0};
    lck.l_type = F_WRLCK;
    lck.l_whence = SEEK_SET;
    lck.l_start = sizeof(pid_t);

    int len = 0;
    pid_t pid = -1;

    if(fcntl(fd, F_SETLK, &lck) < 0){
        len = read(fd, &pid, sizeof(pid_t));
        if(len != sizeof(pid_t)) {
            printf("read pid failed. error: %s\n", strerror(errno));
            close(fd);
            return -1;
        }
        close(fd);
        return 1;
    }

    pid = getpid();

    lseek(fd, 0, SEEK_SET);
    len = write(fd, &pid, sizeof(pid_t));
    if(len != sizeof(pid_t)){
        lck.l_type = F_UNLCK;
        fcntl(fd, F_SETLK, &lck);
        close(fd);
        return -1;
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x6702
movq -0x10(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x670e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68d6
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2190
leaq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0xff, %esi
leaq 0x3b1e(%rip), %rdx      # 0xa256
movb $0x0, %al
callq 0x2160
movl $0x49, %edi
callq 0x2240
leaq -0x110(%rbp), %rdi
movl $0x101042, %esi         # imm = 0x101042
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x22f0
movl %eax, -0x114(%rbp)
cmpl $0x0, -0x114(%rbp)
jge 0x677c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68d6
leaq -0x138(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x2190
movw $0x1, -0x138(%rbp)
movw $0x0, -0x136(%rbp)
movq $0x4, -0x130(%rbp)
movl $0x0, -0x13c(%rbp)
movl $0xffffffff, -0x140(%rbp) # imm = 0xFFFFFFFF
movl -0x114(%rbp), %edi
movl $0x6, %esi
leaq -0x138(%rbp), %rdx
movb $0x0, %al
callq 0x20d0
cmpl $0x0, %eax
jge 0x6853
movl -0x114(%rbp), %edi
leaq -0x140(%rbp), %rsi
movl $0x4, %edx
callq 0x21d0
movl %eax, -0x13c(%rbp)
movslq -0x13c(%rbp), %rax
cmpq $0x4, %rax
je 0x683c
callq 0x2060
movl (%rax), %edi
callq 0x2370
movq %rax, %rsi
leaq 0x3a48(%rip), %rdi      # 0xa266
movb $0x0, %al
callq 0x2150
movl -0x114(%rbp), %edi
callq 0x21b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68d6
movl -0x114(%rbp), %edi
callq 0x21b0
movl $0x1, -0x4(%rbp)
jmp 0x68d6
callq 0x20f0
movl %eax, -0x140(%rbp)
movl -0x114(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2170
movl -0x114(%rbp), %edi
leaq -0x140(%rbp), %rsi
movl $0x4, %edx
callq 0x20e0
movl %eax, -0x13c(%rbp)
movslq -0x13c(%rbp), %rax
cmpq $0x4, %rax
je 0x68cf
movw $0x2, -0x138(%rbp)
movl -0x114(%rbp), %edi
movl $0x6, %esi
leaq -0x138(%rbp), %rdx
movb $0x0, %al
callq 0x20d0
movl -0x114(%rbp), %edi
callq 0x21b0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x68d6
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopw %cs:(%rax,%rax)