#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*/ memcpy(int *, int *, int);
int /*<<< orphan*/ printf(char *);
int write(int, int *, int);
int i2c_write_register(int i2c, uint8_t address, uint8_t reg, uint8_t *data,
                       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;
  }
  uint8_t addrdata[length + 1];
  addrdata[0] = reg;
  memcpy(addrdata + 1, data, length);
  if ((errno = write(i2c, addrdata, length + 1)) < 0) {
    printf("Failed to write register\n");
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i2c_write_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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_write_register+0x50>
lea    0xe80(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <i2c_write_register+0x109>
cmpl   $0x1,-0x1c(%rbp)
jge    11b9 <i2c_write_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    1249 <i2c_write_register+0x109>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,(%rdi)
add    $0x4,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1234 <i2c_write_register+0xf4>
lea    0xe0a(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    1242 <i2c_write_register+0x102>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_write_register>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r13d
mov    %rcx,%r15
mov    %edx,%r12d
mov    %esi,%edx
mov    %edi,%r14d
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1050 <ioctl@plt>
test   %eax,%eax
js     11de <i2c_write_register+0x9e>
test   %r13d,%r13d
jle    11ee <i2c_write_register+0xae>
mov    %rsp,-0x38(%rbp)
lea    0x1(%r13),%eax
mov    %eax,-0x2c(%rbp)
mov    %r13d,%eax
mov    %rsp,%rcx
lea    0x13(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
neg    %rax
mov    %r12d,(%rcx,%rax,1)
lea    0x4(%rbx),%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    -0x2c(%rbp),%edx
call   1040 <write@plt>
xor    %ebx,%ebx
test   %eax,%eax
jns    11d8 <i2c_write_register+0x98>
mov    %eax,%r14d
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%ebx
mov    -0x38(%rbp),%rsp
jmp    1205 <i2c_write_register+0xc5>
mov    %eax,%ebx
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
jmp    1205 <i2c_write_register+0xc5>
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dd7(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [26 x i8] c"Failed to write register\0A\00"
define i32 @i2c_write_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
%13 = alloca i8*
%14 = alloca i64
%15 = 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
%16 = load i32, i32* %7
%17 = load i32, i32* @I2C_SLAVE
%18 = load i32, i32* %8
%19 = call i32 @ioctl(i32 %16, i32 %17, i32 %18)
store i32 %19, i32* %12
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %5
%22 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* %12
store i32 %23, i32* %6
br label %54
24: ; preds = %5
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EINVAL
%30 = sub nsw i32 0, %29
store i32 %30, i32* %6
br label %54
31: ; preds = %24
%32 = load i32, i32* %11
%33 = add nsw i32 %32, 1
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
store i8* %35, i8** %13
%36 = alloca i32, i64 %34
store i64 %34, i64* %14
%37 = load i32, i32* %9
%38 = getelementptr inbounds i32, i32* %36, i64 0
store i32 %37, i32* %38
%39 = getelementptr inbounds i32, i32* %36, i64 1
%40 = load i32*, i32** %10
%41 = load i32, i32* %11
%42 = call i32 @memcpy(i32* %39, i32* %40, i32 %41)
%43 = load i32, i32* %7
%44 = load i32, i32* %11
%45 = add nsw i32 %44, 1
%46 = call i32 @write(i32 %43, i32* %36, i32 %45)
store i32 %46, i32* %12
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %31
%49 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %12
store i32 %50, i32* %6
store i32 1, i32* %15
br label %52
51: ; preds = %31
store i32 0, i32* %6
store i32 1, i32* %15
br label %52
52: ; preds = %51, %48
%53 = load i8*, i8** %13
br label %54
54: ; preds = %52, %27, %21
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
@I2C_SLAVE = global i32 0
@EINVAL = global i32 0
@str = [25 x i8] c"Failed to write register\00"
@str.3 = [24 x i8] c"Invalid register length\00"
@str.4 = [13 x i8] c"ioctl failed\00"
define i32 @i2c_write_register(i32 %0, i32 %1, i32 %2, i32* %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.4, i64 0, i64 0))
br label %30
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.3, i64 0, i64 0))
%15 = load i32, i32* @EINVAL
%16 = sub nsw i32 0, %15
br label %30
17: ; preds = %11
%18 = add nuw nsw i32 %4, 1
%19 = zext i32 %18 to i64
%20 = tail call i8* @llvm.stacksave()
%21 = alloca i32, i64 %19
store i32 %2, i32* %21
%22 = getelementptr inbounds i32, i32* %21, i64 1
%23 = call i32 @memcpy(i32* %22, i32* %3, i32 %4)
%24 = call i32 @write(i32 %0, i32* %21, i32 %18)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %17, %26
%29 = phi i32 [ %24, %26 ], [ 0, %17 ]
br label %30
30: ; preds = %28, %13, %9
%31 = phi i32 [ %7, %9 ], [ %16, %13 ], [ %29, %28 ]
ret i32 %31
}