#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*/ value;
typedef int /*<<< orphan*/ regAddr;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, unsigned char *, int);
unsigned char readI2cReg(int i2cFileDesc, unsigned char regAddr) {
  int res = write(i2cFileDesc, &regAddr, sizeof(regAddr));
  if (res != sizeof(regAddr)) {
    perror("I2C: Unable to write to i2c register.");
    exit(1);
  }
  char value = 0;
  res = read(i2cFileDesc, &value, sizeof(value));
  if (res != sizeof(value)) {
    perror("I2C: Unable to read from i2c register");
    exit(1);
  }
  return value;
}


x86 assembly (clang, -O0):
<readI2cReg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x1,%rax
je     1189 <readI2cReg+0x49>
lea    0xe86(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movb   $0x0,-0xd(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
cmp    $0x1,%rax
je     11c5 <readI2cReg+0x85>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movzbl -0xd(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readI2cReg>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %sil,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    1185 <readI2cReg+0x45>
movb   $0x0,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    118e <readI2cReg+0x4e>
mov    0xe(%rsp),%al
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe74(%rip),%rdi
jmp    1195 <readI2cReg+0x55>
lea    0xe91(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"I2C: Unable to write to i2c register.\00"
@.str.1 = [38 x i8] c"I2C: Unable to read from i2c register\00"
define zeroext i8 @readI2cReg(i32 %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8
store i32 %0, i32* %3
store i8 %1, i8* %4
%7 = load i32, i32* %3
%8 = call i32 @write(i32 %7, i8* %4, i32 1)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = icmp ne i64 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
store i8 0, i8* %6
%16 = load i32, i32* %3
%17 = call i32 @read(i32 %16, i8* %6, i32 1)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = icmp ne i64 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = load i8, i8* %6
ret i8 %25
}

LLVM IR (-O2):
@.str = [38 x i8] c"I2C: Unable to write to i2c register.\00"
@.str.1 = [38 x i8] c"I2C: Unable to read from i2c register\00"
define zeroext i8 @readI2cReg(i32 %0, i8 zeroext %1) {
%3 = alloca i8
%4 = alloca i8
store i8 %1, i8* %3
%5 = call i32 @write(i32 %0, i8* %3, i32 1)
%6 = icmp eq i32 %5, 1
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
store i8 0, i8* %4
%11 = call i32 @read(i32 %0, i8* %4, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = load i8, i8* %4
ret i8 %17
}