#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 {
  int addr;
  int flags;
  int len;
  unsigned char *buf;
};

/* Variables and functions */
int I2C_M_NOSTART;
int I2C_M_RD;
int /*<<< orphan*/ I2C_RDWR;
int ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int i2c_read(int fd, int addr, int offset, unsigned char *buf, int len) {
  struct i2c_rdwr_ioctl_data msg_rdwr;
  struct i2c_msg i2cmsg[2];
  int error;
  msg_rdwr.msgs = i2cmsg;
  msg_rdwr.nmsgs = 2;
  buf[0] = offset;
  i2cmsg[0].addr = addr;
  i2cmsg[0].flags = 0x00 | I2C_M_NOSTART;
  i2cmsg[0].len = 1;
  i2cmsg[0].buf = buf;
  i2cmsg[1].addr = addr;
  i2cmsg[1].flags = I2C_M_RD;
  i2cmsg[1].len = len;
  i2cmsg[1].buf = buf;
  error = ioctl(fd, I2C_RDWR, &msg_rdwr);
  if (error < 0) {
    return error;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i2c_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x2,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
mov    %eax,-0x60(%rbp)
mov    0x2e98(%rip),%rax
mov    (%rax),%eax
or     $0x0,%eax
mov    %eax,-0x5c(%rbp)
movl   $0x1,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    11b4 <i2c_read+0xa4>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <i2c_read+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read>:
sub    $0x48,%rsp
mov    %rsp,%rax
mov    %rax,0x40(%rsp)
movl   $0x2,0x38(%rsp)
mov    %dl,(%rcx)
mov    %esi,(%rsp)
mov    0x2eb8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
movl   $0x1,0x8(%rsp)
mov    %rcx,0x10(%rsp)
mov    %esi,0x18(%rsp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %r8d,0x20(%rsp)
mov    %rcx,0x28(%rsp)
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
lea    0x38(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %eax,%ecx
sar    $0x1f,%ecx
and    %ecx,%eax
add    $0x48,%rsp
ret

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

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