#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 */
struct i2c_rdwr_ioctl_data {
  int nmsgs;
  struct i2c_msg *msgs;
};
struct i2c_msg {
  unsigned int addr;
  int len;
  unsigned char *buf;
  scalar_t__ flags;
};

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
int ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, unsigned char);
int i2c_write(int fd, unsigned int addr, unsigned int offset,
              unsigned char *buf, unsigned char len) {
  struct i2c_rdwr_ioctl_data msg_rdwr;
  struct i2c_msg i2cmsg;
  unsigned char _buf[20];
  int error;
  _buf[0] = offset;
  memcpy((_buf + 1), buf, len);
  msg_rdwr.msgs = &i2cmsg;
  msg_rdwr.nmsgs = 1;
  i2cmsg.addr = addr;
  i2cmsg.flags = 0;
  i2cmsg.len = 1 + len;
  i2cmsg.buf = _buf;
  error = ioctl(fd, I2C_RDWR, &msg_rdwr);
  if (error < 0) {
    return error;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i2c_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %r8b,%al
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x60(%rbp)
lea    -0x60(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x19(%rbp),%al
movzbl %al,%edx
call   1040 <memcpy@plt>
lea    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x38(%rbp)
movzbl -0x19(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11b4 <i2c_write+0x94>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <i2c_write+0x9b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %r8d,%ebx
mov    %esi,%ebp
mov    %edi,%r14d
mov    %dl,0x30(%rsp)
lea    0x31(%rsp),%rdi
mov    %rcx,%rsi
mov    %r8d,%edx
call   1040 <memcpy@plt>
lea    0x8(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x1,0x20(%rsp)
mov    %ebp,0x8(%rsp)
movq   $0x0,0x18(%rsp)
add    $0x1,%ebx
mov    %ebx,0xc(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %r14d,%edi
call   1030 <ioctl@plt>
mov    %eax,%ecx
sar    $0x1f,%ecx
and    %ecx,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, i64 }
@I2C_RDWR = global i32 0
define i32 @i2c_write(i32 %0, i32 %1, i32 %2, i8* %3, i8 zeroext %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8
%12 = alloca %struct.i2c_rdwr_ioctl_data
%13 = alloca %struct.i2c_msg
%14 = alloca [20 x i8]
%15 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i8 %4, i8* %11
%16 = load i32, i32* %9
%17 = trunc i32 %16 to i8
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %14, i64 0, i64 0
store i8 %17, i8* %18
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %14, i64 0, i64 0
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8*, i8** %10
%22 = load i8, i8* %11
%23 = call i32 @memcpy(i8* %20, i8* %21, i8 zeroext %22)
%24 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
store %struct.i2c_msg* %13, %struct.i2c_msg** %24
%25 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 0
store i32 1, i32* %25
%26 = load i32, i32* %8
%27 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 0
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 3
store i64 0, i64* %28
%29 = load i8, i8* %11
%30 = zext i8 %29 to i32
%31 = add nsw i32 1, %30
%32 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %14, i64 0, i64 0
%34 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 2
store i8* %33, i8** %34
%35 = load i32, i32* %7
%36 = load i32, i32* @I2C_RDWR
%37 = call i32 @ioctl(i32 %35, i32 %36, %struct.i2c_rdwr_ioctl_data* %12)
store i32 %37, i32* %15
%38 = load i32, i32* %15
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %5
%41 = load i32, i32* %15
store i32 %41, i32* %6
br label %43
42: ; preds = %5
store i32 0, i32* %6
br label %43
43: ; preds = %42, %40
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, i64 }
@I2C_RDWR = global i32 0
define i32 @i2c_write(i32 %0, i32 %1, i32 %2, i8* %3, i8 zeroext %4) {
%6 = alloca %struct.i2c_rdwr_ioctl_data
%7 = alloca %struct.i2c_msg
%8 = alloca [20 x i8]
%9 = bitcast %struct.i2c_rdwr_ioctl_data* %6 to i8*
%10 = bitcast %struct.i2c_msg* %7 to i8*
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%12 = trunc i32 %2 to i8
store i8 %12, i8* %11
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 1
%14 = call i32 @memcpy(i8* %13, i8* %3, i8 zeroext %4)
%15 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 1
store %struct.i2c_msg* %7, %struct.i2c_msg** %15
%16 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 0
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 0
store i32 %1, i32* %17
%18 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 3
store i64 0, i64* %18
%19 = zext i8 %4 to i32
%20 = add nuw nsw i32 %19, 1
%21 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %7, i64 0, i32 2
store i8* %11, i8** %22
%23 = load i32, i32* @I2C_RDWR
%24 = call i32 @ioctl(i32 %0, i32 %23, %struct.i2c_rdwr_ioctl_data* %6)
%25 = icmp slt i32 %24, 0
%26 = select i1 %25, i32 %24, i32 0
ret i32 %26
}