// File: /workspace/github_c_makefile_star2_O0/mikecat[P]x86_interpreter/xv6_syscall.c
// Function: xv6_open
static int xv6_open(uint32_t regs[]) {
	uint32_t name_ptr, mode;
	char* name;
	uint32_t i;
	stream_info* si;
	uint32_t fd;
	int want_read, want_write, want_create;
	if (!dmem_get_args(regs[ESP], 2, &name_ptr, &mode)) {
		regs[EAX] = -1;
		return 1;
	}

	/* ファイル名を取得する */
	name = NULL;
	i = 0;
	for (;;) {
		uint8_t c = 0;
		char* next_name;
		if (i == UINT32_MAX || !dmemory_is_allocated(name_ptr + i, 1)) {
			free(name);
			regs[EAX] = -1;
			return 1;
		}
		dmemory_read(&c, name_ptr + i, 1);
		next_name = realloc(name, i + 1);
		if (next_name == NULL) {
			perror("realloc");
			free(name);
			return -1;
		}
		name = next_name;
		name[i] = c;
		if (c == 0) break;
		if (i + name_ptr == UINT32_MAX) {
			free(name);
			regs[EAX] = -1;
			return 1;
		}
		i++;
	}

	/* ファイル情報の書き込み先を確保する */
	for (fd = 0; fd < FD_MAX; fd++) {
		if (fds[fd] == NULL) break;
	}
	for (si = NULL, i = 0; i < STREAM_MAX; i++) {
		if (streams[i].stream == NULL) {
			si = &streams[i];
			break;
		}
	}
	if (fd >= FD_MAX || si == NULL) {
		free(name);
		regs[EAX] = -1;
		return 1;
	}

	/* ファイルを開いて情報を登録する */
	want_read = ((mode & 3) != 1); /* 読み込みを有効化(O_WRONLYでない) */
	want_write = ((mode & 3) != 0); /* 書き込みを有効化(O_RDONLYでない) */
	want_create = ((mode & 0x200) != 0); /* 新規作成を有効化(O_CREATEを含む) */
	/* ファイルの内容を消さずに開くためにrを使う */
	si->stream = fopen(name, want_write ? "r+" : "r");
	if (si->stream == NULL && want_create) {
		/* 開けなかった場合、ファイルが無かったとみなしてwでの作成を試みる */
		si->stream = fopen(name, want_read ? "w+" : "w");
	}
	if (si->stream == NULL) {
		/* それでも開けなかったらエラー */
		free(name);
		regs[EAX] = -1;
		return 1;
	}
	/* ファイルが開けたので、その他の情報を登録する */
	si->ref_cnt = 1;
	si->can_read = want_read;
	si->can_write = want_write;
	si->prev_operation = POP_NONE;
	fds[fd] = si;

	/* 成功 */
	free(name);
	regs[EAX] = fd;
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %edi
movl $0x2, %esi
leaq -0x14(%rbp), %rdx
leaq -0x18(%rbp), %rcx
movb $0x0, %al
callq 0x8280
cmpl $0x0, %eax
jne 0xdc72
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x1, -0x4(%rbp)
jmp 0xdf43
movq $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movb $0x0, -0x41(%rbp)
cmpl $-0x1, -0x24(%rbp)
je 0xdca0
movl -0x14(%rbp), %edi
addl -0x24(%rbp), %edi
movl $0x1, %esi
callq 0x7f60
cmpl $0x0, %eax
jne 0xdcbf
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x1, -0x4(%rbp)
jmp 0xdf43
movl -0x14(%rbp), %esi
addl -0x24(%rbp), %esi
leaq -0x41(%rbp), %rdi
movl $0x1, %edx
callq 0x7890
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x1140
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xdd12
leaq 0x439e(%rip), %rdi      # 0x12096
callq 0x1170
movq -0x20(%rbp), %rdi
callq 0x1030
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xdf43
movq -0x50(%rbp), %rax
movq %rax, -0x20(%rbp)
movb -0x41(%rbp), %dl
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
movb %dl, (%rax,%rcx)
movzbl -0x41(%rbp), %eax
cmpl $0x0, %eax
jne 0xdd32
jmp 0xdd6a
movl -0x24(%rbp), %eax
addl -0x14(%rbp), %eax
cmpl $-0x1, %eax
jne 0xdd5c
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x1, -0x4(%rbp)
jmp 0xdf43
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xdc81
movl $0x0, -0x34(%rbp)
cmpl $0x400, -0x34(%rbp)     # imm = 0x400
jae 0xdd9c
movl -0x34(%rbp), %eax
movl %eax, %ecx
leaq 0x100ea(%rip), %rax     # 0x1de70
cmpq $0x0, (%rax,%rcx,8)
jne 0xdd8f
jmp 0xdd9c
jmp 0xdd91
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xdd71
movq $0x0, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x400, -0x24(%rbp)     # imm = 0x400
jae 0xddf3
movl -0x24(%rbp), %eax
movl %eax, %ecx
leaq 0xa0b0(%rip), %rax      # 0x17e70
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0xdde6
movl -0x24(%rbp), %eax
movl %eax, %ecx
leaq 0xa097(%rip), %rax      # 0x17e70
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
jmp 0xddf3
jmp 0xdde8
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xddab
cmpl $0x400, -0x34(%rbp)     # imm = 0x400
jae 0xde03
cmpq $0x0, -0x30(%rbp)
jne 0xde22
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x1, -0x4(%rbp)
jmp 0xdf43
movl -0x18(%rbp), %eax
andl $0x3, %eax
cmpl $0x1, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x38(%rbp)
movl -0x18(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x3c(%rbp)
movl -0x18(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
movl -0x3c(%rbp), %ecx
leaq 0x415b(%rip), %rsi      # 0x11fc9
leaq 0x45f3(%rip), %rax      # 0x12468
cmpl $0x0, %ecx
cmovneq %rax, %rsi
callq 0x1160
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xdec6
cmpl $0x0, -0x40(%rbp)
je 0xdec6
movq -0x20(%rbp), %rdi
movl -0x38(%rbp), %ecx
leaq 0x39a1(%rip), %rsi      # 0x1184a
leaq 0x45bb(%rip), %rax      # 0x1246b
cmpl $0x0, %ecx
cmovneq %rax, %rsi
callq 0x1160
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xdeec
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x10(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x1, -0x4(%rbp)
jmp 0xdf43
movq -0x30(%rbp), %rax
movl $0x1, 0x8(%rax)
movl -0x38(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x30(%rbp), %rax
movl $0x0, 0x14(%rax)
movq -0x30(%rbp), %rdx
movl -0x34(%rbp), %eax
movl %eax, %ecx
leaq 0xff4a(%rip), %rax      # 0x1de70
movq %rdx, (%rax,%rcx,8)
movq -0x20(%rbp), %rdi
callq 0x1030
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)