#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;

/* Variables and functions */
int /*<<< orphan*/ printf(char *);
int read(int, int *, int);
int /*<<< orphan*/ usleep(int);
int write(int, int *, int);
int read_i2c_reg(int file_i2c, uint8_t reg, uint16_t *data) {
  uint8_t readbuffer[2];
  if (write(file_i2c, &reg, 1) != 1) {
    printf("Failed to write to the i2c bus.\n");
    return -1;
  } else {
    usleep(1000);
    if (read(file_i2c, readbuffer, 2) != 2) {
      printf("Failed to read from the i1c bus.\n");
      return -2;
    } else {
      *data = ((uint16_t)readbuffer[0] << 8) + readbuffer[1];
      return 0;
    }
  }
}


x86 assembly (clang, -O0):
<read_i2c_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1184 <read_i2c_reg+0x44>
lea    0xe8d(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <read_i2c_reg+0x96>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
je     11c0 <read_i2c_reg+0x80>
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d6 <read_i2c_reg+0x96>
mov    -0x20(%rbp),%ecx
shl    $0x8,%ecx
add    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_i2c_reg>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %edi,%ebp
mov    %esi,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1194 <read_i2c_reg+0x54>
mov    $0x3e8,%edi
call   1060 <usleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1050 <read@plt>
cmp    $0x2,%eax
jne    11a7 <read_i2c_reg+0x67>
mov    0x10(%rsp),%eax
shl    $0x8,%eax
add    0x14(%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
jmp    11b8 <read_i2c_reg+0x78>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11b8 <read_i2c_reg+0x78>
lea    0xe52(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [33 x i8] c"Failed to write to the i2c bus.\0A\00"
@.str.1 = [34 x i8] c"Failed to read from the i1c bus.\0A\00"
define i32 @read_i2c_reg(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [2 x i32]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%9 = load i32, i32* %5
%10 = call i32 @write(i32 %9, i32* %6, i32 1)
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %30
14: ; preds = %3
%15 = call i32 @usleep(i32 1000)
%16 = load i32, i32* %5
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%18 = call i32 @read(i32 %16, i32* %17, i32 2)
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %4
br label %30
22: ; preds = %14
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%24 = load i32, i32* %23
%25 = shl i32 %24, 8
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%27 = load i32, i32* %26
%28 = add nsw i32 %25, %27
%29 = load i32*, i32** %7
store i32 %28, i32* %29
store i32 0, i32* %4
br label %30
30: ; preds = %22, %20, %12
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@str = [33 x i8] c"Failed to read from the i1c bus.\00"
@str.2 = [32 x i8] c"Failed to write to the i2c bus.\00"
define i32 @read_i2c_reg(i32 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca [2 x i32]
store i32 %1, i32* %4
%6 = bitcast [2 x i32]* %5 to i8*
%7 = call i32 @write(i32 %0, i32* %4, i32 1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.2, i64 0, i64 0))
br label %24
11: ; preds = %3
%12 = tail call i32 @usleep(i32 1000)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%14 = call i32 @read(i32 %0, i32* %13, i32 2)
%15 = icmp eq i32 %14, 2
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %24
18: ; preds = %11
%19 = load i32, i32* %13
%20 = shl i32 %19, 8
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%22 = load i32, i32* %21
%23 = add nsw i32 %20, %22
store i32 %23, i32* %2
br label %24
24: ; preds = %18, %16, %9
%25 = phi i32 [ -1, %9 ], [ -2, %16 ], [ 0, %18 ]
ret i32 %25
}