#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 int uint8_t;
typedef int uint16_t;
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*/ outbuf;
typedef int /*<<< orphan*/ inbuf;

/* Variables and functions */
scalar_t__ I2C_M_RD;
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int read_value(int file, unsigned char addr, unsigned char reg,
               uint16_t *result) {
  unsigned char outbuf;
  uint8_t inbuf[3];
  struct i2c_rdwr_ioctl_data packets;
  struct i2c_msg messages[2];
  outbuf = reg;
  messages[0].addr = addr;
  messages[0].flags = 0;
  messages[0].len = sizeof(outbuf);
  messages[0].buf = &outbuf;
  messages[1].addr = addr;
  messages[1].flags = I2C_M_RD;
  messages[1].len = sizeof(inbuf);
  messages[1].buf = inbuf;
  packets.msgs = messages;
  packets.nmsgs = 2;
  if (ioctl(file, I2C_RDWR, &packets) < 0) {
    perror("Unable to send data");
    return 1;
  }
  fprintf(stderr, "%02hhx %02hhx %02hhx \n", inbuf[0], inbuf[1], inbuf[2]);
  uint16_t v = inbuf[0];
  v <<= 8;
  v |= inbuf[1];
  *result = v;
  return 0;
}


x86 assembly (clang, -O0):
<read_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%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,-0x19(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x70(%rbp)
movq   $0x0,-0x60(%rbp)
movl   $0x1,-0x6c(%rbp)
lea    -0x19(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x58(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
movl   $0xc,-0x54(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x2,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11d9 <read_value+0xa9>
lea    0xe38(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1220 <read_value+0xf0>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
lea    0xe21(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x74(%rbp)
mov    -0x24(%rbp),%eax
or     -0x74(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x74(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_value>:
push   %rbx
sub    $0x50,%rsp
mov    %rcx,%rbx
mov    %dl,0x3(%rsp)
mov    %sil,0x10(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0x1,0x14(%rsp)
lea    0x3(%rsp),%rax
mov    %rax,0x18(%rsp)
mov    %sil,0x28(%rsp)
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x38(%rsp)
movl   $0xc,0x2c(%rsp)
lea    0x4(%rsp),%rax
mov    %rax,0x30(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x48(%rsp)
movl   $0x2,0x40(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0x40(%rsp),%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
js     11e3 <read_value+0xb3>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xe4b(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x4(%rsp),%eax
shl    $0x8,%eax
or     0x8(%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xe16(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    11dd <read_value+0xad>

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 = [20 x i8] c"Unable to send data\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"%02hhx %02hhx %02hhx \0A\00"
define i32 @read_value(i32 %0, i8 zeroext %1, i8 zeroext %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32*
%10 = alloca i8
%11 = alloca [3 x i32]
%12 = alloca %struct.i2c_rdwr_ioctl_data
%13 = alloca [2 x %struct.i2c_msg]
%14 = alloca i32
store i32 %0, i32* %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i32* %3, i32** %9
%15 = load i8, i8* %8
store i8 %15, i8* %10
%16 = load i8, i8* %7
%17 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 0
%18 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %17, i32 0, i32 0
store i8 %16, i8* %18
%19 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, 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]* %13, i64 0, i64 0
%22 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %21, i32 0, i32 1
store i32 1, i32* %22
%23 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 0
%24 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %23, i32 0, i32 2
store i8* %10, i8** %24
%25 = load i8, i8* %7
%26 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 1
%27 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %26, i32 0, i32 0
store i8 %25, i8* %27
%28 = load i64, i64* @I2C_M_RD
%29 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, 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]* %13, i64 0, i64 1
%32 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %31, i32 0, i32 1
store i32 12, i32* %32
%33 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 0
%34 = bitcast i32* %33 to i8*
%35 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, 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]* %13, i64 0, i64 0
%38 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, 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* %12, 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* %12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %4
%45 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %65
46: ; preds = %4
%47 = load i32, i32* @stderr
%48 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 0
%49 = load i32, i32* %48
%50 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 1
%51 = load i32, i32* %50
%52 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 2
%53 = load i32, i32* %52
%54 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %49, i32 %51, i32 %53)
%55 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 0
%56 = load i32, i32* %55
store i32 %56, i32* %14
%57 = load i32, i32* %14
%58 = shl i32 %57, 8
store i32 %58, i32* %14
%59 = getelementptr inbounds [3 x i32], [3 x i32]* %11, i64 0, i64 1
%60 = load i32, i32* %59
%61 = load i32, i32* %14
%62 = or i32 %61, %60
store i32 %62, i32* %14
%63 = load i32, i32* %14
%64 = load i32*, i32** %9
store i32 %63, i32* %64
store i32 0, i32* %5
br label %65
65: ; preds = %46, %44
%66 = load i32, i32* %5
ret i32 %66
}

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 = [20 x i8] c"Unable to send data\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"%02hhx %02hhx %02hhx \0A\00"
define i32 @read_value(i32 %0, i8 zeroext %1, i8 zeroext %2, i32* nocapture writeonly %3) {
%5 = alloca i8
%6 = alloca [3 x i32]
%7 = alloca %struct.i2c_rdwr_ioctl_data
%8 = alloca [2 x %struct.i2c_msg]
%9 = bitcast [3 x i32]* %6 to i8*
%10 = bitcast %struct.i2c_rdwr_ioctl_data* %7 to i8*
%11 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 0, i32 0
store i8 %2, i8* %5
%12 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 0
store i8 %1, i8* %11
%13 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 0, i32 3
store i64 0, i64* %13
%14 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 0, i32 1
store i32 1, i32* %14
%15 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 0, i32 2
store i8* %5, i8** %15
%16 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 1, i32 0
store i8 %1, i8* %16
%17 = load i64, i64* @I2C_M_RD
%18 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 1, i32 3
store i64 %17, i64* %18
%19 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 1, i32 1
store i32 12, i32* %19
%20 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%21 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %8, i64 0, i64 1, i32 2
%22 = bitcast i8** %21 to [3 x i32]**
store [3 x i32]* %6, [3 x i32]** %22
%23 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %7, 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* %7, 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* %7)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %4
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %42
30: ; preds = %4
%31 = load i32, i32* @stderr
%32 = load i32, i32* %20
%33 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 2
%36 = load i32, i32* %35
%37 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %34, i32 %36)
%38 = load i32, i32* %20
%39 = shl i32 %38, 8
%40 = load i32, i32* %33
%41 = or i32 %39, %40
store i32 %41, i32* %3
br label %42
42: ; preds = %30, %28
%43 = phi i32 [ 1, %28 ], [ 0, %30 ]
ret i32 %43
}