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

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ I2C_SLAVE;
int ioctl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int read(int, int *, int);
int write(int, int *, int);
int i2c_read_register(int i2c, uint8_t address, uint8_t reg, uint8_t *dst,
                      uint8_t length) {
  int errno;
  if ((errno = ioctl(i2c, I2C_SLAVE, address)) < 0) {
    printf("ioctl failed\n");
    return errno;
  }
  if (length < 1) {
    printf("Invalid register length\n");
    return -EINVAL;
  }
  dst[0] = reg;
  if ((errno = write(i2c, dst, 1)) < 0) {
    printf("Failed to write register address\n");
    return errno;
  }
  if ((errno = read(i2c, dst, length)) < 0) {
    printf("Failed to read register\n");
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i2c_read_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1050 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1190 <i2c_read_register+0x50>
lea    0xe80(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <i2c_read_register+0xef>
cmpl   $0x1,-0x1c(%rbp)
jge    11b9 <i2c_read_register+0x79>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    0x2e29(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <i2c_read_register+0xef>
mov    -0x10(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11f6 <i2c_read_register+0xb6>
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <i2c_read_register+0xef>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1228 <i2c_read_register+0xe8>
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <i2c_read_register+0xef>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_register>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %edx,%r12d
mov    %esi,%edx
mov    %edi,%ebp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
call   1050 <ioctl@plt>
test   %eax,%eax
js     11a7 <i2c_read_register+0x67>
test   %r14d,%r14d
jle    11b2 <i2c_read_register+0x72>
mov    %r12d,(%r15)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
test   %eax,%eax
js     11cb <i2c_read_register+0x8b>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
jns    11d9 <i2c_read_register+0x99>
mov    %eax,%ebp
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%ebx
jmp    11d9 <i2c_read_register+0x99>
mov    %eax,%ebx
lea    0xea1(%rip),%rdi
jmp    11d4 <i2c_read_register+0x94>
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e13(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11d9 <i2c_read_register+0x99>
mov    %eax,%ebx
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@I2C_SLAVE = global i32 0
@.str = [14 x i8] c"ioctl failed\0A\00"
@.str.1 = [25 x i8] c"Invalid register length\0A\00"
@EINVAL = global i32 0
@.str.2 = [34 x i8] c"Failed to write register address\0A\00"
@.str.3 = [25 x i8] c"Failed to read register\0A\00"
define i32 @i2c_read_register(i32 %0, i32 %1, i32 %2, i32* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i32 %4, i32* %11
%13 = load i32, i32* %7
%14 = load i32, i32* @I2C_SLAVE
%15 = load i32, i32* %8
%16 = call i32 @ioctl(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %12
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %5
%19 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* %12
store i32 %20, i32* %6
br label %49
21: ; preds = %5
%22 = load i32, i32* %11
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EINVAL
%27 = sub nsw i32 0, %26
store i32 %27, i32* %6
br label %49
28: ; preds = %21
%29 = load i32, i32* %9
%30 = load i32*, i32** %10
%31 = getelementptr inbounds i32, i32* %30, i64 0
store i32 %29, i32* %31
%32 = load i32, i32* %7
%33 = load i32*, i32** %10
%34 = call i32 @write(i32 %32, i32* %33, i32 1)
store i32 %34, i32* %12
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %12
store i32 %38, i32* %6
br label %49
39: ; preds = %28
%40 = load i32, i32* %7
%41 = load i32*, i32** %10
%42 = load i32, i32* %11
%43 = call i32 @read(i32 %40, i32* %41, i32 %42)
store i32 %43, i32* %12
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* %12
store i32 %47, i32* %6
br label %49
48: ; preds = %39
store i32 0, i32* %6
br label %49
49: ; preds = %48, %45, %36, %24, %18
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
@I2C_SLAVE = global i32 0
@EINVAL = global i32 0
@str = [24 x i8] c"Failed to read register\00"
@str.4 = [33 x i8] c"Failed to write register address\00"
@str.5 = [24 x i8] c"Invalid register length\00"
@str.6 = [13 x i8] c"ioctl failed\00"
define i32 @i2c_read_register(i32 %0, i32 %1, i32 %2, i32* nocapture %3, i32 %4) {
%6 = load i32, i32* @I2C_SLAVE
%7 = tail call i32 @ioctl(i32 %0, i32 %6, i32 %1)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.6, i64 0, i64 0))
br label %27
11: ; preds = %5
%12 = icmp slt i32 %4, 1
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
%15 = load i32, i32* @EINVAL
%16 = sub nsw i32 0, %15
br label %27
17: ; preds = %11
store i32 %2, i32* %3
%18 = tail call i32 @write(i32 %0, i32* %3, i32 1)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.4, i64 0, i64 0))
br label %27
22: ; preds = %17
%23 = tail call i32 @read(i32 %0, i32* %3, i32 %4)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %22, %25, %20, %13, %9
%28 = phi i32 [ %7, %9 ], [ %16, %13 ], [ %18, %20 ], [ %23, %25 ], [ 0, %22 ]
ret i32 %28
}