// File: /workspace/github_c_makefile_star2_O0/giometti[P]mastering_UNIX_programming/chapter_03/flockw.c
// Function: main
int main(void)
{
	int  fd;
	char buf[32];
	int ret;
	struct flock fl;

        /* Open the file */
        fd = open("file.locked", O_WRONLY);
        if (fd < 0) {
                perror("open");
                return -1;
        }
        printf("file opened\n");

	/* Establish the write lock */
	fl.l_type   = F_WRLCK;
	fl.l_whence = SEEK_SET;
	fl.l_start  = 0;        /* Offset from l_whence */
	fl.l_len    = 0;        /* length, 0 = to EOF   */
	fl.l_pid    = getpid();
	ret = fcntl(fd, F_SETLKW, &fl);
	if (ret < 0) {
                perror("fcntl(F_SETLKW)");
                return -1;
        }
        printf("write lock get\n");

	ret = write(fd, buf, 32);
        if (ret < 0) {
                perror("write");
                return -1;
        }
        printf("write done\n");

        printf("sleeping 10s...\n");
        sleep(10);

        /* Unlock the region */
        fl.l_type   = F_UNLCK;
        fl.l_whence = SEEK_SET;
        fl.l_start  = 0;        /* Offset from l_whence */
        fl.l_len    = 0;        /* length, 0 = to EOF   */
        fl.l_pid    = getpid();
        ret = fcntl(fd, F_UNLCK, &fl); 
        if (ret < 0) {
                perror("fcntl(F_SETLKW)");
                return -1;
        }
        printf("unlock done\n");

	close(fd);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
leaq 0xe3e(%rip), %rdi       # 0x2004
movl $0x1, %esi
movb $0x0, %al
callq 0x1080
movl %eax, -0x8(%rbp)
cmpl $0x0, -0x8(%rbp)
jge 0x11f3
leaq 0xe2e(%rip), %rdi       # 0x2010
callq 0x1090
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1331
leaq 0xe1b(%rip), %rdi       # 0x2015
movb $0x0, %al
callq 0x1060
movw $0x1, -0x58(%rbp)
movw $0x0, -0x56(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
callq 0x1050
movl %eax, -0x40(%rbp)
movl -0x8(%rbp), %edi
movl $0x7, %esi
leaq -0x58(%rbp), %rdx
movb $0x0, %al
callq 0x1030
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x1259
leaq 0xdda(%rip), %rdi       # 0x2022
callq 0x1090
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1331
leaq 0xdd2(%rip), %rdi       # 0x2032
movb $0x0, %al
callq 0x1060
movl -0x8(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x20, %edx
callq 0x1040
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x1299
leaq 0xdba(%rip), %rdi       # 0x2042
callq 0x1090
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1331
leaq 0xda8(%rip), %rdi       # 0x2048
movb $0x0, %al
callq 0x1060
leaq 0xda6(%rip), %rdi       # 0x2054
movb $0x0, %al
callq 0x1060
movl $0xa, %edi
callq 0x10a0
movw $0x2, -0x58(%rbp)
movw $0x0, -0x56(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
callq 0x1050
movl %eax, -0x40(%rbp)
movl -0x8(%rbp), %edi
movl $0x2, %esi
leaq -0x58(%rbp), %rdx
movb $0x0, %al
callq 0x1030
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x1314
leaq 0xd1c(%rip), %rdi       # 0x2022
callq 0x1090
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1331
leaq 0xd4a(%rip), %rdi       # 0x2065
movb $0x0, %al
callq 0x1060
movl -0x8(%rbp), %edi
callq 0x1070
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq