#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
struct i2c_rdwr_ioctl_data {
  int nmsgs;
  struct i2c_msg *msgs;
};
struct i2c_msg {
  unsigned int len;
  int /*<<< orphan*/ *buf;
  int /*<<< orphan*/ addr;
};

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ close(int);
int i2c_open(unsigned int);
int ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int i2c_write(unsigned chan, uint8_t addr, uint8_t reg, uint8_t *buf,
              unsigned count) {
  int fd;
  int ret;
  struct i2c_msg message;
  struct i2c_rdwr_ioctl_data msgset;
  fd = i2c_open(chan);
  if (fd < 0)
    return fd;
  message.addr = addr;
  message.buf = buf;
  message.len = count;
  msgset.msgs = &message;
  msgset.nmsgs = 1;
  ret = ioctl(fd, I2C_RDWR, &msgset);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<i2c_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <i2c_open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1169 <i2c_write+0x39>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <i2c_write+0x82>
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %esi,%ebp
call   1040 <i2c_open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     118a <i2c_write+0x5a>
mov    %ebp,0x20(%rsp)
mov    %r15,0x18(%rsp)
mov    %r14d,0x10(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x1,(%rsp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.i2c_msg = type { i32, i32*, i32 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@I2C_RDWR = global i32 0
define i32 @i2c_write(i32 %0, i32 %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.i2c_msg
%15 = alloca %struct.i2c_rdwr_ioctl_data
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i32 %4, i32* %11
%16 = load i32, i32* %7
%17 = call i32 @i2c_open(i32 %16)
store i32 %17, i32* %12
%18 = load i32, i32* %12
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %5
%21 = load i32, i32* %12
store i32 %21, i32* %6
br label %37
22: ; preds = %5
%23 = load i32, i32* %8
%24 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %14, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32*, i32** %10
%26 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %14, i32 0, i32 1
store i32* %25, i32** %26
%27 = load i32, i32* %11
%28 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %14, i32 0, i32 0
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %15, i32 0, i32 1
store %struct.i2c_msg* %14, %struct.i2c_msg** %29
%30 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %15, i32 0, i32 0
store i32 1, i32* %30
%31 = load i32, i32* %12
%32 = load i32, i32* @I2C_RDWR
%33 = call i32 @ioctl(i32 %31, i32 %32, %struct.i2c_rdwr_ioctl_data* %15)
store i32 %33, i32* %13
%34 = load i32, i32* %12
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %13
store i32 %36, i32* %6
br label %37
37: ; preds = %22, %20
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
%struct.i2c_msg = type { i32, i32*, i32 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@I2C_RDWR = global i32 0
define i32 @i2c_write(i32 %0, i32 %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca %struct.i2c_msg
%7 = alloca %struct.i2c_rdwr_ioctl_data
%8 = bitcast %struct.i2c_msg* %6 to i8*
%9 = bitcast %struct.i2c_rdwr_ioctl_data* %7 to i8*
%10 = tail call i32 @i2c_open(i32 %0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %5
%13 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %6, i64 0, i32 2
store i32 %1, i32* %13
%14 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %6, i64 0, i32 1
store i32* %3, i32** %14
%15 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %6, i64 0, i32 0
store i32 %4, i32* %15
%16 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %7, i64 0, i32 1
store %struct.i2c_msg* %6, %struct.i2c_msg** %16
%17 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %7, i64 0, i32 0
store i32 1, i32* %17
%18 = load i32, i32* @I2C_RDWR
%19 = call i32 @ioctl(i32 %10, i32 %18, %struct.i2c_rdwr_ioctl_data* %7)
%20 = call i32 @close(i32 %10)
br label %21
21: ; preds = %5, %12
%22 = phi i32 [ %19, %12 ], [ %10, %5 ]
ret i32 %22
}