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

/* Variables and functions */
scalar_t__ I2C_M_RD;
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ close(int);
int i2c_open(unsigned int);
int ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ memset(void **, int /*<<< orphan*/, unsigned int);
int i2c_read(unsigned chan, uint8_t addr, uint8_t reg, uint8_t *buf,
             unsigned count) {
  int fd;
  int ret;
  struct i2c_msg messages[2];
  struct i2c_rdwr_ioctl_data msgset;
  fd = i2c_open(chan);
  if (fd < 0)
    return fd;
  messages[0].addr = addr;
  messages[0].buf = &reg;
  messages[0].len = 1;
  messages[0].flags = 0;
  messages[1].addr = addr;
  messages[1].buf = buf;
  messages[1].len = count;
  messages[1].flags = I2C_M_RD;
  memset(buf, 0, count);
  msgset.msgs = messages;
  msgset.nmsgs = 2;
  ret = ioctl(fd, I2C_RDWR, &msgset);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<i2c_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <i2c_open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    117e <i2c_read+0x3e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1204 <i2c_read+0xc4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0x1,-0x68(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
movl   $0x2,-0x80(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rsi,%rbp
mov    %rdx,0x8(%rsp)
call   1050 <i2c_open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11e0 <i2c_read+0xa0>
mov    %rbp,0x38(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x1,0x28(%rsp)
movq   $0x0,0x30(%rsp)
mov    %rbp,0x58(%rsp)
mov    %r15,0x40(%rsp)
mov    %r14d,0x48(%rsp)
mov    0x2e47(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x50(%rsp)
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1030 <memset@plt>
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x2,0x10(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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