#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 EFAULT;
int /*<<< orphan*/ perror(char *);
int read(int, int *, int);
int write(int, int *, int);
int i2c_read_word(int file, uint8_t reg_addr, uint16_t *data) {
  if (NULL == data) {
    return -EFAULT;
  }
  int res;
  uint8_t tmp[2];
  if ((res = write(file, &reg_addr, 1)) < 0) {
    perror("write() register address failed");
    return res;
  }
  if ((res = read(file, tmp, 2)) < 0) {
    perror("read() data failed");
    return res;
  }
  *data = (tmp[0] << 8) + tmp[1];
  return 0;
}


x86 assembly (clang, -O0):
<i2c_read_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1161 <i2c_read_word+0x31>
xor    %eax,%eax
mov    0x2ea1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <i2c_read_word+0xaf>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1195 <i2c_read_word+0x65>
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <i2c_read_word+0xaf>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x2,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c9 <i2c_read_word+0x99>
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11df <i2c_read_word+0xaf>
mov    -0x24(%rbp),%ecx
shl    $0x8,%ecx
add    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_word>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,0x4(%rsp)
test   %rdx,%rdx
je     1180 <i2c_read_word+0x50>
mov    %rdx,%r14
mov    %edi,%ebx
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1196 <i2c_read_word+0x66>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11a6 <i2c_read_word+0x76>
mov    0x8(%rsp),%eax
shl    $0x8,%eax
add    0xc(%rsp),%eax
mov    %eax,(%r14)
xor    %ebp,%ebp
jmp    118b <i2c_read_word+0x5b>
mov    0x2e71(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebp
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
jmp    118b <i2c_read_word+0x5b>
mov    %eax,%ebp
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
jmp    118b <i2c_read_word+0x5b>

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

LLVM IR (-O2):
@EFAULT = global i32 0
@.str = [32 x i8] c"write() register address failed\00"
@.str.1 = [19 x i8] c"read() data failed\00"
define i32 @i2c_read_word(i32 %0, i32 %1, i32* writeonly %2) {
%4 = alloca i32
%5 = alloca [2 x i32]
store i32 %1, i32* %4
%6 = icmp eq i32* %2, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @EFAULT
%9 = sub nsw i32 0, %8
br label %30
10: ; preds = %3
%11 = bitcast [2 x i32]* %5 to i8*
%12 = call i32 @write(i32 %0, i32* %4, i32 1)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %28
16: ; preds = %10
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%18 = call i32 @read(i32 %0, i32* %17, i32 2)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %28
22: ; preds = %16
%23 = load i32, i32* %17
%24 = shl i32 %23, 8
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%26 = load i32, i32* %25
%27 = add nsw i32 %24, %26
store i32 %27, i32* %2
br label %28
28: ; preds = %22, %20, %14
%29 = phi i32 [ %12, %14 ], [ %18, %20 ], [ 0, %22 ]
br label %30
30: ; preds = %28, %7
%31 = phi i32 [ %9, %7 ], [ %29, %28 ]
ret i32 %31
}