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

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int i2c_write_read(int handle, unsigned char addr_w, unsigned char *buf_w,
                   unsigned int len_w, unsigned char addr_r,
                   unsigned char *buf_r, unsigned int len_r) {
  struct i2c_rdwr_ioctl_data msgset;
  struct i2c_msg msgs[2];
  msgs[0].addr = addr_w;
  msgs[0].len = len_w;
  msgs[0].flags = 0;
  msgs[0].buf = buf_w;
  msgs[1].addr = addr_r;
  msgs[1].len = len_r;
  msgs[1].flags = 1;
  msgs[1].buf = buf_r;
  msgset.nmsgs = 2;
  msgset.msgs = msgs;
  if (ioctl(handle, I2C_RDWR, (unsigned long)&msgset) < 0) {
    fprintf(stderr, "i2c_write_read error: %s\n", strerror(errno));
    return -1;
  }
  return (len_r);
}


x86 assembly (clang, -O0):
<i2c_write_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %r8b,%al
mov    0x10(%rbp),%r8d
mov    %edi,-0x8(%rbp)
mov    %sil,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x70(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
movl   $0x0,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x1d(%rbp),%al
mov    %al,-0x58(%rbp)
mov    0x10(%rbp),%eax
mov    %eax,-0x54(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x2,-0x38(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11f3 <i2c_write_read+0xc3>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xe1e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <i2c_write_read+0xc9>
mov    0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_write_read>:
push   %rbx
sub    $0x40,%rsp
mov    0x50(%rsp),%ebx
mov    %sil,(%rsp)
mov    %ecx,0x4(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rdx,0x10(%rsp)
mov    %r8b,0x18(%rsp)
mov    %ebx,0x1c(%rsp)
movl   $0x1,0x20(%rsp)
mov    %r9,0x28(%rsp)
movl   $0x2,0x30(%rsp)
mov    %rsp,%rax
mov    %rax,0x38(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0x30(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
jns    11b9 <i2c_write_read+0x89>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe56(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i8, i32, i32, i8* }
@I2C_RDWR = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"i2c_write_read error: %s\0A\00"
@errno = global i32 0
define i32 @i2c_write_read(i32 %0, i8 zeroext %1, i8* %2, i32 %3, i8 zeroext %4, i8* %5, i32 %6) {
%8 = alloca %struct.i2c_rdwr_ioctl_data
%9 = alloca [2 x %struct.i2c_msg]
%10 = bitcast %struct.i2c_rdwr_ioctl_data* %8 to i8*
%11 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 0, i32 0
%12 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 0
store i8 %1, i8* %11
%13 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 0, i32 1
store i32 %3, i32* %13
%14 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 0, i32 2
store i32 0, i32* %14
%15 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 0, i32 3
store i8* %2, i8** %15
%16 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 1, i32 0
store i8 %4, i8* %16
%17 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 1, i32 1
store i32 %6, i32* %17
%18 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 1, i32 2
store i32 1, i32* %18
%19 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %9, i64 0, i64 1, i32 3
store i8* %5, i8** %19
%20 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %8, i64 0, i32 0
store i32 2, i32* %20
%21 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %8, i64 0, i32 1
store %struct.i2c_msg* %12, %struct.i2c_msg** %21
%22 = load i32, i32* @I2C_RDWR
%23 = ptrtoint %struct.i2c_rdwr_ioctl_data* %8 to i64
%24 = call i64 @ioctl(i32 %0, i32 %22, i64 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %7
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %7, %26
%32 = phi i32 [ -1, %26 ], [ %6, %7 ]
ret i32 %32
}