// File: /workspace/github_c_makefile_star2_O0/giometti[P]mastering_UNIX_programming/chapter_09/limits.c
// Function: main
int main(int argc, char *argv[])
{
	int resource;
	struct rlimit rlim;
	bool do_set = false;
	int ret;

	/* Check the command line */
	if (!(argc == 2 || argc == 4)) {
		fprintf(stderr,
			"usage: %s <resource> "
				"[<soft-limit> <hard-limit>]\n",
			argv[0]);
		fprintf(stderr,
			"where <resource> can be: ");
		for (resource = 0; resource < 7; resource++)
			fprintf(stderr, "%s ", rlimit_n[resource]);
		fprintf(stderr, "\n");
		return -1;
	}
	resource = res_n2v(argv[1]);
	if (resource < 0) {
		fprintf(stderr, "invallid resource %s", argv[1]);
		return -1;

	}
	if (argc == 4) {
		rlim.rlim_cur = atoi(argv[2]);
		rlim.rlim_max = atoi(argv[3]);
		do_set = true;
	}

	if (do_set) {
		/* Try to set the new resource's values */
		ret = setrlimit(resource, &rlim);
		if (ret) {
			perror("setrlimit");
			return -1;
		}
	}

	/* Read the resource's values in order to print them */	
	ret = getrlimit(resource, &rlim);
	if (ret) {
		perror("getrlimit");
		return -1;
	}
	printf("limits for %s are soft=%lld; hard=%lld\n", argv[1],
		(long long) rlim.rlim_cur, (long long) rlim.rlim_max);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x29(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x12d8
cmpl $0x4, -0x8(%rbp)
je 0x12d8
movq 0x2db3(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0xe03(%rip), %rsi       # 0x2059
movb $0x0, %al
callq 0x1050
movq 0x2d94(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xe1d(%rip), %rsi       # 0x208b
movb $0x0, %al
callq 0x1050
movl $0x0, -0x14(%rbp)
cmpl $0x7, -0x14(%rbp)
jge 0x12b4
movq 0x2d6f(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movslq -0x14(%rbp), %rcx
leaq 0x2de9(%rip), %rax      # 0x4080
movq (%rax,%rcx,8), %rdx
leaq 0xe03(%rip), %rsi       # 0x20a5
movb $0x0, %al
callq 0x1050
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x127c
movq 0x2d3d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xdc4(%rip), %rsi       # 0x2089
movb $0x0, %al
callq 0x1050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x13cb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x11a0
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x131a
movq 0x2d03(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0xda2(%rip), %rsi       # 0x20a9
movb $0x0, %al
callq 0x1050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x13cb
cmpl $0x4, -0x8(%rbp)
jne 0x134a
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1080
cltq
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1080
cltq
movq %rax, -0x20(%rbp)
movb $0x1, -0x29(%rbp)
testb $0x1, -0x29(%rbp)
je 0x137c
movl -0x14(%rbp), %edi
leaq -0x28(%rbp), %rsi
callq 0x1060
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x137a
leaq 0xd52(%rip), %rdi       # 0x20be
callq 0x1070
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x13cb
jmp 0x137c
movl -0x14(%rbp), %edi
leaq -0x28(%rbp), %rsi
callq 0x1090
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x13a6
leaq 0xd30(%rip), %rdi       # 0x20c8
callq 0x1070
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x13cb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0xd15(%rip), %rdi       # 0x20d2
movb $0x0, %al
callq 0x1030
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq