#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;
  int len;
  unsigned char *buf;
  scalar_t__ flags;
};
typedef int /*<<< orphan*/ output_buffer;
typedef int /*<<< orphan*/ input_buffer;

/* 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 addr, unsigned char reg,
             unsigned char *data) {
  unsigned char input_buffer, output_buffer;
  struct i2c_rdwr_ioctl_data packets;
  struct i2c_msg messages[2];
  output_buffer = reg;
  messages[0].addr = addr;
  messages[0].flags = 0;
  messages[0].len = sizeof(output_buffer);
  messages[0].buf = &output_buffer;
  messages[1].addr = addr;
  messages[1].flags = I2C_M_RD;
  messages[1].len = sizeof(input_buffer);
  messages[1].buf = &input_buffer;
  packets.msgs = messages;
  packets.nmsgs = 2;
  if (ioctl(fd, I2C_RDWR, &packets) < 0) {
    perror("Error sending data");
    return 1;
  }
  *data = input_buffer;
  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    -0xa(%rbp),%al
mov    %al,-0x1a(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x60(%rbp)
movq   $0x0,-0x50(%rbp)
movl   $0x1,-0x5c(%rbp)
lea    -0x1a(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x48(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x44(%rbp)
lea    -0x19(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x2,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11c6 <i2c_read+0xa6>
lea    0xe4b(%rip),%rdi
call   1040 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d6 <i2c_read+0xb6>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read>:
push   %rbx
sub    $0x50,%rsp
mov    %rcx,%rbx
mov    %dl,0xe(%rsp)
mov    %sil,0x10(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0x1,0x14(%rsp)
lea    0xe(%rsp),%rax
mov    %rax,0x18(%rsp)
mov    %sil,0x28(%rsp)
mov    0x2e88(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x38(%rsp)
movl   $0x1,0x2c(%rsp)
lea    0xf(%rsp),%rax
mov    %rax,0x30(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x48(%rsp)
movl   $0x2,0x40(%rsp)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0x40(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11aa <i2c_read+0x8a>
mov    0xf(%rsp),%al
mov    %al,(%rbx)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xe4f(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%eax
jmp    11a4 <i2c_read+0x84>

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

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