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

/* Variables and functions */
scalar_t__ I2C_M_RD;
int /*<<< orphan*/ I2C_RDWR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ perror(char *);
int i2c_read(int fd, unsigned char cmd, unsigned char bnr,
             unsigned short *val) {
  unsigned char buf[2];
  struct i2c_rdwr_ioctl_data packets;
  struct i2c_msg messages[2];
  buf[0] = cmd;
  buf[1] = bnr;
  messages[0].addr = 0x48;
  messages[0].flags = 0;
  messages[0].len = 2;
  messages[0].buf = buf;
  messages[1].addr = 0x48;
  messages[1].flags = I2C_M_RD;
  messages[1].len = 2;
  messages[1].buf = (unsigned char *)val;
  packets.msgs = messages;
  packets.nmsgs = 2;
  if (ioctl(fd, I2C_RDWR, &packets) < 0) {
    perror("data could not be send");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i2c_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %dl,%al
mov    %sil,%dl
mov    %edi,-0x8(%rbp)
mov    %dl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x1a(%rbp)
mov    -0xa(%rbp),%al
mov    %al,-0x19(%rbp)
movl   $0x48,-0x60(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0x2,-0x5c(%rbp)
lea    -0x1a(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x48,-0x48(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movl   $0x2,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x2,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11ce <i2c_read+0xae>
lea    0xe43(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <i2c_read+0xb5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read>:
sub    $0x58,%rsp
mov    %sil,0xe(%rsp)
mov    %dl,0xf(%rsp)
movabs $0x200000048,%rax
mov    %rax,0x20(%rsp)
movq   $0x0,0x30(%rsp)
lea    0xe(%rsp),%rdx
mov    %rdx,0x28(%rsp)
mov    %rax,0x38(%rsp)
mov    0x2e85(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x48(%rsp)
mov    %rcx,0x40(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x2,0x10(%rsp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     119c <i2c_read+0x7c>
add    $0x58,%rsp
ret
lea    0xe5d(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x58,%rsp
ret

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, i64 }
@I2C_M_RD = global i64 0
@I2C_RDWR = global i32 0
@.str = [23 x i8] c"data could not be send\00"
define i32 @i2c_read(i32 %0, i8 zeroext %1, i8 zeroext %2, i16* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i16*
%10 = alloca [2 x i8]
%11 = alloca %struct.i2c_rdwr_ioctl_data
%12 = alloca [2 x %struct.i2c_msg]
store i32 %0, i32* %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i16* %3, i16** %9
%13 = load i8, i8* %7
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
store i8 %13, i8* %14
%15 = load i8, i8* %8
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
store i8 %15, i8* %16
%17 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 0
%18 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %17, i32 0, i32 0
store i32 72, i32* %18
%19 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 0
%20 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %19, i32 0, i32 3
store i64 0, i64* %20
%21 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 0
%22 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %21, i32 0, i32 1
store i32 2, i32* %22
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%24 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 0
%25 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %24, i32 0, i32 2
store i8* %23, i8** %25
%26 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 1
%27 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %26, i32 0, i32 0
store i32 72, i32* %27
%28 = load i64, i64* @I2C_M_RD
%29 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 1
%30 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %29, i32 0, i32 3
store i64 %28, i64* %30
%31 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 1
%32 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %31, i32 0, i32 1
store i32 2, i32* %32
%33 = load i16*, i16** %9
%34 = bitcast i16* %33 to i8*
%35 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 1
%36 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %35, i32 0, i32 2
store i8* %34, i8** %36
%37 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %12, i64 0, i64 0
%38 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %11, i32 0, i32 1
store %struct.i2c_msg* %37, %struct.i2c_msg** %38
%39 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %11, i32 0, i32 0
store i32 2, i32* %39
%40 = load i32, i32* %6
%41 = load i32, i32* @I2C_RDWR
%42 = call i64 @ioctl(i32 %40, i32 %41, %struct.i2c_rdwr_ioctl_data* %11)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %4
%45 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %47
46: ; preds = %4
store i32 0, i32* %5
br label %47
47: ; preds = %46, %44
%48 = load i32, i32* %5
ret i32 %48
}

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