#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 */

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int /*<<< orphan*/ setI2cAddress(int, unsigned char);
int write(int, unsigned char *, int);
unsigned int I2CReadReg16(int fd, unsigned char i2cAddress,
                          unsigned char i2cReg) {
  unsigned char I2C_WR_Buf[8];
  unsigned char I2C_RD_Buf[8];
  I2C_WR_Buf[0] = i2cReg;
  setI2cAddress(fd, i2cAddress);
  if (write(fd, I2C_WR_Buf, 1) != 1) {
    perror("Write Error");
  }
  setI2cAddress(fd, i2cAddress);
  if (read(fd, I2C_RD_Buf, 2) != 2) {
    perror("Read Error");
  }
  return ((I2C_RD_Buf[0] << 8) + I2C_RD_Buf[1]);
}


x86 assembly (clang, -O0):
<I2CReadReg16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %sil,%cl
mov    %edi,-0x4(%rbp)
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
mov    -0x6(%rbp),%al
mov    %al,-0xe(%rbp)
mov    -0x4(%rbp),%edi
movzbl -0x5(%rbp),%esi
call   1030 <setI2cAddress@plt>
mov    -0x4(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     118e <I2CReadReg16+0x4e>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
mov    -0x4(%rbp),%edi
movzbl -0x5(%rbp),%esi
call   1030 <setI2cAddress@plt>
mov    -0x4(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
je     11c0 <I2CReadReg16+0x80>
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
movzbl -0x16(%rbp),%eax
shl    $0x8,%eax
movzbl -0x15(%rbp),%ecx
add    %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<I2CReadReg16>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    %dl,0x10(%rsp)
call   1030 <setI2cAddress@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    119b <I2CReadReg16+0x5b>
movzbl %bpl,%esi
mov    %ebx,%edi
call   1030 <setI2cAddress@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
jne    11a9 <I2CReadReg16+0x69>
movzwl 0x8(%rsp),%eax
shl    $0x10,%eax
bswap  %eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
jmp    1169 <I2CReadReg16+0x29>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
jmp    118a <I2CReadReg16+0x4a>

LLVM IR (-O0):
@.str = [12 x i8] c"Write Error\00"
@.str.1 = [11 x i8] c"Read Error\00"
define i32 @I2CReadReg16(i32 %0, i8 zeroext %1, i8 zeroext %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca [8 x i8]
%8 = alloca [8 x i8]
store i32 %0, i32* %4
store i8 %1, i8* %5
store i8 %2, i8* %6
%9 = load i8, i8* %6
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
store i8 %9, i8* %10
%11 = load i32, i32* %4
%12 = load i8, i8* %5
%13 = call i32 @setI2cAddress(i32 %11, i8 zeroext %12)
%14 = load i32, i32* %4
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %7, i64 0, i64 0
%16 = call i32 @write(i32 %14, i8* %15, i32 1)
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %3
%21 = load i32, i32* %4
%22 = load i8, i8* %5
%23 = call i32 @setI2cAddress(i32 %21, i8 zeroext %22)
%24 = load i32, i32* %4
%25 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 2)
%27 = icmp ne i32 %26, 2
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %20
%31 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 0
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = shl i32 %33, 8
%35 = getelementptr inbounds [8 x i8], [8 x i8]* %8, i64 0, i64 1
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = add nsw i32 %34, %37
ret i32 %38
}

LLVM IR (-O2):
@.str = [12 x i8] c"Write Error\00"
@.str.1 = [11 x i8] c"Read Error\00"
define i32 @I2CReadReg16(i32 %0, i8 zeroext %1, i8 zeroext %2) {
%4 = alloca [8 x i8]
%5 = alloca [8 x i8]
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 0
store i8 %2, i8* %6
%8 = tail call i32 @setI2cAddress(i32 %0, i8 zeroext %1)
%9 = call i32 @write(i32 %0, i8* %6, i32 1)
%10 = icmp eq i32 %9, 1
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = tail call i32 @setI2cAddress(i32 %0, i8 zeroext %1)
%15 = call i32 @read(i32 %0, i8* %7, i32 2)
%16 = icmp eq i32 %15, 2
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = load i8, i8* %7
%21 = zext i8 %20 to i32
%22 = shl nuw nsw i32 %21, 8
%23 = getelementptr inbounds [8 x i8], [8 x i8]* %5, i64 0, i64 1
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = or i32 %22, %25
ret i32 %26
}