#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
int EFAULT;
int /*<<< orphan*/ perror(char *);
int read(int, int /*<<< orphan*/ *, int);
int write(int, int /*<<< orphan*/ *, int);
int i2c_read_byte(int file, uint8_t reg_addr, uint8_t *data) {
  if (NULL == data) {
    return -EFAULT;
  }
  int res;
  if ((res = write(file, &reg_addr, 1)) < 0) {
    perror("write() register address failed");
    return res;
  }
  if ((res = read(file, data, 1)) < 0) {
    perror("read() data failed");
    return res;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i2c_read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1161 <i2c_read_byte+0x31>
xor    %eax,%eax
mov    0x2ea1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <i2c_read_byte+0xa0>
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_byte+0x65>
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <i2c_read_byte+0xa0>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c9 <i2c_read_byte+0x99>
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <i2c_read_byte+0xa0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_byte>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,0xc(%rsp)
test   %rdx,%rdx
je     1180 <i2c_read_byte+0x50>
mov    %rdx,%r14
mov    %edi,%ebp
lea    0xc(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1196 <i2c_read_byte+0x66>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
jns    118b <i2c_read_byte+0x5b>
mov    %eax,%ebp
lea    0xea9(%rip),%rdi
call   1050 <perror@plt>
mov    %ebp,%ebx
jmp    118b <i2c_read_byte+0x5b>
mov    0x2e71(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebx
lea    0xe61(%rip),%rdi
call   1050 <perror@plt>
jmp    118b <i2c_read_byte+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_byte(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%9 = load i32*, i32** %7
%10 = icmp eq i32* null, %9
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @EFAULT
%13 = sub nsw i32 0, %12
store i32 %13, i32* %4
br label %30
14: ; preds = %3
%15 = load i32, i32* %5
%16 = call i32 @write(i32 %15, i32* %6, i32 1)
store i32 %16, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %8
store i32 %20, i32* %4
br label %30
21: ; preds = %14
%22 = load i32, i32* %5
%23 = load i32*, i32** %7
%24 = call i32 @read(i32 %22, i32* %23, i32 1)
store i32 %24, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %8
store i32 %28, i32* %4
br label %30
29: ; preds = %21
store i32 0, i32* %4
br label %30
30: ; preds = %29, %26, %18, %11
%31 = load i32, i32* %4
ret i32 %31
}

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_byte(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
store i32 %1, i32* %4
%5 = icmp eq i32* %2, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @EFAULT
%8 = sub nsw i32 0, %7
br label %19
9: ; preds = %3
%10 = call i32 @write(i32 %0, i32* %4, i32 1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %19
14: ; preds = %9
%15 = tail call i32 @read(i32 %0, i32* %2, i32 1)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %12, %17, %14, %6
%20 = phi i32 [ %8, %6 ], [ %10, %12 ], [ %15, %17 ], [ 0, %14 ]
ret i32 %20
}