#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 */
scalar_t__ i2c_read(unsigned char *, int);
scalar_t__ i2c_write(unsigned char *, int);
int register_read(unsigned char reg, unsigned short *data) {
  int rc = -1;
  unsigned char bite[4];
  bite[0] = reg;
  if (i2c_write(bite, 1) == 0) {
    if (i2c_read(bite, 2) == 0) {
      *data = (bite[0] << 8) | bite[1];
      rc = 0;
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<register_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x1(%rbp),%al
mov    %al,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1040 <i2c_write@plt>
cmp    $0x0,%rax
jne    1192 <register_read+0x72>
lea    -0x18(%rbp),%rdi
mov    $0x2,%esi
call   1030 <i2c_read@plt>
cmp    $0x0,%rax
jne    118d <register_read+0x6d>
movzbl -0x18(%rbp),%eax
shl    $0x8,%eax
movzbl -0x17(%rbp),%ecx
or     %ecx,%eax
mov    %ax,%cx
mov    -0x10(%rbp),%rax
mov    %cx,(%rax)
movl   $0x0,-0x14(%rbp)
jmp    1192 <register_read+0x72>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<register_read>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %dil,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    $0x1,%esi
call   1040 <i2c_write@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1166 <register_read+0x46>
lea    0x4(%rsp),%rdi
mov    $0x2,%esi
call   1030 <i2c_read@plt>
test   %rax,%rax
jne    1166 <register_read+0x46>
movzwl 0x4(%rsp),%eax
rol    $0x8,%ax
mov    %ax,(%rbx)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @register_read(i8 zeroext %0, i16* %1) {
%3 = alloca i8
%4 = alloca i16*
%5 = alloca i32
%6 = alloca [4 x i8]
store i8 %0, i8* %3
store i16* %1, i16** %4
store i32 -1, i32* %5
%7 = load i8, i8* %3
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
store i8 %7, i8* %8
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%10 = call i64 @i2c_write(i8* %9, i32 1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %2
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%14 = call i64 @i2c_read(i8* %13, i32 2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = shl i32 %19, 8
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = or i32 %20, %23
%25 = trunc i32 %24 to i16
%26 = load i16*, i16** %4
store i16 %25, i16* %26
store i32 0, i32* %5
br label %27
27: ; preds = %16, %12
br label %28
28: ; preds = %27, %2
%29 = load i32, i32* %5
ret i32 %29
}

LLVM IR (-O2):
define i32 @register_read(i8 zeroext %0, i16* nocapture writeonly %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
store i8 %0, i8* %4
%5 = call i64 @i2c_write(i8* %4, i32 1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %2
%8 = call i64 @i2c_read(i8* %4, i32 2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %7
%11 = load i8, i8* %4
%12 = zext i8 %11 to i16
%13 = shl nuw i16 %12, 8
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%15 = load i8, i8* %14
%16 = zext i8 %15 to i16
%17 = or i16 %13, %16
store i16 %17, i16* %1
br label %18
18: ; preds = %7, %10, %2
%19 = phi i32 [ 0, %10 ], [ -1, %7 ], [ -1, %2 ]
ret i32 %19
}