#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 I2C_M_NO_RD_ACK;
int I2C_M_RD;
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_read_no_ack(int handle, unsigned char addr_r, unsigned char *buf,
                    unsigned int length) {
  struct i2c_rdwr_ioctl_data msgset;
  struct i2c_msg msgs[1];
  msgs[0].addr = addr_r;
  msgs[0].len = length;
  msgs[0].flags = I2C_M_RD | I2C_M_NO_RD_ACK;
  msgs[0].buf = buf;
  msgset.nmsgs = 1;
  msgset.msgs = msgs;
  if (ioctl(handle, I2C_RDWR, (unsigned long)&msgset) < 0) {
    fprintf(stderr, "i2c_read_no_ack error: %s\n", strerror(errno));
    return -1;
  }
  return (length);
}


x86 assembly (clang, -O0):
<i2c_read_no_ack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x50(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
mov    0x2e64(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x30(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11d7 <i2c_read_no_ack+0xa7>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xe3a(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <i2c_read_no_ack+0xad>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_no_ack>:
push   %rbx
sub    $0x30,%rsp
mov    %ecx,%ebx
mov    %sil,(%rsp)
mov    0x2e8e(%rip),%rax
mov    0x2e7f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ebx,0x4(%rsp)
mov    %ecx,0x8(%rsp)
mov    %rdx,0x10(%rsp)
movl   $0x1,0x20(%rsp)
mov    %rsp,%rax
mov    %rax,0x28(%rsp)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
jns    11af <i2c_read_no_ack+0x7f>
mov    0x2e57(%rip),%rax
mov    (%rax),%ebx
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xe60(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x30,%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_M_RD = global i32 0
@I2C_M_NO_RD_ACK = global i32 0
@I2C_RDWR = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"i2c_read_no_ack error: %s\0A\00"
@errno = global i32 0
define i32 @i2c_read_no_ack(i32 %0, i8 zeroext %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.i2c_rdwr_ioctl_data
%11 = alloca [1 x %struct.i2c_msg]
store i32 %0, i32* %6
store i8 %1, i8* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8, i8* %7
%13 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %11, i64 0, i64 0
%14 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 0
store i8 %12, i8* %14
%15 = load i32, i32* %9
%16 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %11, i64 0, i64 0
%17 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load i32, i32* @I2C_M_RD
%19 = load i32, i32* @I2C_M_NO_RD_ACK
%20 = or i32 %18, %19
%21 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %11, i64 0, i64 0
%22 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %21, i32 0, i32 2
store i32 %20, i32* %22
%23 = load i8*, i8** %8
%24 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %11, 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 %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %10, i32 0, i32 0
store i32 1, i32* %26
%27 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %11, i64 0, i64 0
%28 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %10, i32 0, i32 1
store %struct.i2c_msg* %27, %struct.i2c_msg** %28
%29 = load i32, i32* %6
%30 = load i32, i32* @I2C_RDWR
%31 = ptrtoint %struct.i2c_rdwr_ioctl_data* %10 to i64
%32 = call i64 @ioctl(i32 %29, i32 %30, i64 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %4
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %37)
store i32 -1, i32* %5
br label %41
39: ; preds = %4
%40 = load i32, i32* %9
store i32 %40, i32* %5
br label %41
41: ; preds = %39, %34
%42 = load i32, i32* %5
ret i32 %42
}

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