// File: /workspace/github_c_makefile_star2_O0/koendv[P]stm32duino-raspberrypi/Arduino_Tools/src/stm32flash/i2c.c
// Function: i2c_open
static port_err_t i2c_open(struct port_interface *port,
			   struct port_options *ops)
{
	struct i2c_priv *h;
	int fd, addr, ret;
	unsigned long funcs;

	/* 1. check device name match */
	if (strncmp(ops->device, "/dev/i2c-", strlen("/dev/i2c-")))
		return PORT_ERR_NODEV;

	/* 2. check options */
	addr = ops->bus_addr;
	if (addr < 0x03 || addr > 0x77) {
		fprintf(stderr, "I2C address out of range [0x03-0x77]\n");
		return PORT_ERR_UNKNOWN;
	}

	/* 3. open it */
	h = calloc(sizeof(*h), 1);
	if (h == NULL) {
		fprintf(stderr, "End of memory\n");
		return PORT_ERR_UNKNOWN;
	}
	fd = open(ops->device, O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "Unable to open special file \"%s\"\n",
			ops->device);
		free(h);
		return PORT_ERR_UNKNOWN;
	}

	/* 3.5. Check capabilities */
	ret = ioctl(fd, I2C_FUNCS, &funcs);
	if (ret < 0) {
		fprintf(stderr, "I2C ioctl(funcs) error %d\n", errno);
		close(fd);
		free(h);
		return PORT_ERR_UNKNOWN;
	}
	if ((funcs & I2C_FUNC_I2C) == 0) {
		fprintf(stderr, "Error: controller is not I2C, only SMBUS.\n");
		close(fd);
		free(h);
		return PORT_ERR_UNKNOWN;
	}

	/* 4. set options */
	ret = ioctl(fd, I2C_SLAVE, addr);
	if (ret < 0) {
		fprintf(stderr, "I2C ioctl(slave) error %d\n", errno);
		close(fd);
		free(h);
		return PORT_ERR_UNKNOWN;
	}

	h->fd = fd;
	h->addr = addr;
	port->private = h;
	return PORT_ERR_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq 0x7fc8(%rip), %rsi      # 0xa3a6
movl $0x9, %edx
callq 0x2050
cmpl $0x0, %eax
je 0x23f9
movl $0x1, -0x4(%rbp)
jmp 0x25e6
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x28(%rbp)
cmpl $0x3, -0x28(%rbp)
jl 0x240f
cmpl $0x77, -0x28(%rbp)
jle 0x2433
movq 0xdbda(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x7f90(%rip), %rsi      # 0xa3b0
movb $0x0, %al
callq 0x2160
movl $0x3, -0x4(%rbp)
jmp 0x25e6
movl $0x8, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x2471
movq 0xdb9c(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x7f78(%rip), %rsi      # 0xa3d6
movb $0x0, %al
callq 0x2160
movl $0x3, -0x4(%rbp)
jmp 0x25e6
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x2220
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x24c1
movq 0xdb5c(%rip), %rax      # 0xfff0
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
leaq 0x7f40(%rip), %rsi      # 0xa3e5
movb $0x0, %al
callq 0x2160
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0x3, -0x4(%rbp)
jmp 0x25e6
movl -0x24(%rbp), %edi
movl $0x705, %esi            # imm = 0x705
leaq -0x38(%rbp), %rdx
movb $0x0, %al
callq 0x2110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x2521
movq 0xdb0c(%rip), %rax      # 0xfff0
movq (%rax), %rax
movq %rax, -0x40(%rbp)
callq 0x2040
movq -0x40(%rbp), %rdi
movl (%rax), %edx
leaq 0x7f0a(%rip), %rsi      # 0xa407
movb $0x0, %al
callq 0x2160
movl -0x24(%rbp), %edi
callq 0x2120
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0x3, -0x4(%rbp)
jmp 0x25e6
movq -0x38(%rbp), %rax
andq $0x1, %rax
cmpq $0x0, %rax
jne 0x2564
movq 0xdaba(%rip), %rax      # 0xfff0
movq (%rax), %rdi
leaq 0x7ee2(%rip), %rsi      # 0xa422
movb $0x0, %al
callq 0x2160
movl -0x24(%rbp), %edi
callq 0x2120
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0x3, -0x4(%rbp)
jmp 0x25e6
movl -0x24(%rbp), %edi
movl -0x28(%rbp), %edx
movl $0x703, %esi            # imm = 0x703
movb $0x0, %al
callq 0x2110
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x25c0
movq 0xda6a(%rip), %rax      # 0xfff0
movq (%rax), %rax
movq %rax, -0x48(%rbp)
callq 0x2040
movq -0x48(%rbp), %rdi
movl (%rax), %edx
leaq 0x7eae(%rip), %rsi      # 0xa44d
movb $0x0, %al
callq 0x2160
movl -0x24(%rbp), %edi
callq 0x2120
movq -0x20(%rbp), %rdi
callq 0x2030
movl $0x3, -0x4(%rbp)
jmp 0x25e6
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop