// File: /workspace/github_c_makefile_star2_O2/giometti[P]mastering_UNIX_programming/chapter_04/stat.c
// Function: main
int main(int argc, char *argv[])
{
	struct stat buf;
	int ret;

	if (argc < 2) {
		fprintf(stderr, "usage: %s <file>\n", argv[0]);
		return -1;
	}

	ret = lstat(argv[1], &buf);
	if (ret < 0) {
		perror("lstat");
		return -1;
	}

	printf("File name:                %s\n", argv[1]);

#ifdef major
        printf("ID of containing device:  [%lx,%lx]\n",
		(long) major(buf.st_dev), (long) minor(buf.st_dev));
#endif

	printf("File type:                ");
        switch (buf.st_mode & S_IFMT) {
        case S_IFBLK:  printf("block device\n");            break;
        case S_IFCHR:  printf("character device\n");        break;
        case S_IFDIR:  printf("directory\n");               break;
        case S_IFIFO:  printf("FIFO/pipe\n");               break;
        case S_IFLNK:  printf("symlink\n");                 break;
        case S_IFREG:  printf("regular file\n");            break;
        case S_IFSOCK: printf("socket\n");                  break;
        default:       printf("unknown?\n");
        }

#ifdef major
	if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode))
		printf("special file major/minor: [%lu,%lu]\n",
			(long) major(buf.st_rdev),
			(long) minor(buf.st_rdev));
#endif

        printf("I-node number:            %ld\n", (long) buf.st_ino);
        printf("Mode:                     %lo (octal)\n",
					(unsigned long) buf.st_mode);
        printf("Link count:               %ld\n", (long) buf.st_nlink);
        printf("Ownership:                UID=%ld GID=%ld\n",
		(long) buf.st_uid, (long) buf.st_gid);
	printf("Preferred I/O block size: %ld bytes\n",
					(long) buf.st_blksize);
	printf("File size:                %lld bytes\n",
					(long long) buf.st_size);
	printf("Blocks allocated:         %lld\n",
					(long long) buf.st_blocks);

	printf("Last status change:       %s", ctime(&buf.st_ctime));
	printf("Last file access:         %s", ctime(&buf.st_atime));
	printf("Last file modification:   %s", ctime(&buf.st_mtime));

	return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x90, %rsp
movq %rsi, %rbx
cmpl $0x1, %edi
jle 0x137f
movq 0x8(%rbx), %rdi
movq %rsp, %rsi
callq 0x1060
testl %eax, %eax
js 0x139c
movq 0x8(%rbx), %rsi
leaq 0xe39(%rip), %rdi       # 0x201c
xorl %eax, %eax
callq 0x1050
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x1080
movl %eax, %r14d
movq %rbx, %rdi
callq 0x10a0
movl %eax, %edx
leaq 0xe30(%rip), %rdi       # 0x203a
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0xe44(%rip), %rdi       # 0x205f
xorl %eax, %eax
callq 0x1050
movl 0x18(%rsp), %r14d
movl %r14d, %eax
andl $0xf000, %eax           # imm = 0xF000
addl $0xfffff000, %eax       # imm = 0xFFFFF000
cmpl $0xbfff, %eax           # imm = 0xBFFF
ja 0x124e
shrl $0xa, %eax
leaq 0x1003(%rip), %rcx      # 0x2248
movslq (%rax,%rcx), %rdi
addq %rcx, %rdi
jmp 0x1255
leaq 0xfea(%rip), %rdi       # 0x223f
callq 0x1030
movl %r14d, %eax
andl $0xb000, %eax           # imm = 0xB000
orl $0x4000, %eax           # imm = 0x4000
cmpl $0x6000, %eax           # imm = 0x6000
jne 0x1299
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x1080
movl %eax, %r15d
movq %rbx, %rdi
callq 0x10a0
movl %eax, %edx
leaq 0xdeb(%rip), %rdi       # 0x207a
movq %r15, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x68(%rsp), %r15
movq -0x60(%r15), %rsi
leaq 0xdf6(%rip), %rdi       # 0x209f
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x1050
leaq 0xe05(%rip), %rdi       # 0x20be
movq %r14, %rsi
xorl %eax, %eax
callq 0x1050
movq -0x58(%r15), %rsi
leaq 0xe17(%rip), %rdi       # 0x20e5
xorl %eax, %eax
callq 0x1050
movl -0x4c(%r15), %esi
movl -0x48(%r15), %edx
leaq 0xe20(%rip), %rdi       # 0x2104
xorl %eax, %eax
callq 0x1050
movq -0x30(%r15), %rsi
leaq 0xe39(%rip), %rdi       # 0x212f
xorl %eax, %eax
callq 0x1050
movq -0x38(%r15), %rsi
leaq 0xe4c(%rip), %rdi       # 0x2154
xorl %eax, %eax
callq 0x1050
movq -0x28(%r15), %rsi
leaq 0xe60(%rip), %rdi       # 0x217a
xorl %eax, %eax
callq 0x1050
movq %r15, %rdi
callq 0x1040
leaq 0xe6a(%rip), %rdi       # 0x219a
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x48(%rsp), %rdi
callq 0x1040
leaq 0xe6c(%rip), %rdi       # 0x21b7
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
leaq 0x58(%rsp), %rdi
callq 0x1040
leaq 0xe6e(%rip), %rdi       # 0x21d4
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
movl %ebx, %eax
addq $0x90, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq 0x2c72(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0xc71(%rip), %rsi       # 0x2004
xorl %eax, %eax
callq 0x1070
jmp 0x13a8
leaq 0xc73(%rip), %rdi       # 0x2016
callq 0x1090
pushq $-0x1
popq %rbx
jmp 0x1370