#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 */

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int write_word_read_byte(int busHandle, int addr, unsigned char *bytes,
                         int write_len, unsigned char *read_buf, int read_len,
                         int *res_write, int *res_read) {
  if (ioctl(busHandle, I2C_SLAVE, addr) < 0) {
    return *res_write = -1;
  }
  if (write(busHandle, bytes, write_len) != write_len) {
    *res_write = -2;
  } else {
    *res_write = write_len;
  }
  return *res_read = read(busHandle, read_buf, read_len);
}


x86 assembly (clang, -O0):
<write_word_read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1189 <write_word_read_byte+0x59>
mov    0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <write_word_read_byte+0xa1>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     11b0 <write_word_read_byte+0x80>
mov    0x10(%rbp),%rax
movl   $0xfffffffe,(%rax)
jmp    11b9 <write_word_read_byte+0x89>
mov    -0x1c(%rbp),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <read@plt>
mov    0x18(%rbp),%rcx
mov    %eax,(%rcx)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_word_read_byte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8,%r15
mov    %ecx,%ebx
mov    %rdx,%r12
mov    %esi,%edx
mov    %edi,%ebp
mov    0x40(%rsp),%r13
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1192 <write_word_read_byte+0x62>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
mov    $0xfffffffe,%eax
cmove  %ebx,%eax
mov    %eax,0x0(%r13)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
mov    0x48(%rsp),%rcx
mov    %eax,(%rcx)
jmp    119f <write_word_read_byte+0x6f>
movl   $0xffffffff,0x0(%r13)
ff
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@I2C_SLAVE = global i32 0
define i32 @write_word_read_byte(i32 %0, i32 %1, i8* %2, i32 %3, i8* %4, i32 %5, i32* %6, i32* %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32*
store i32 %0, i32* %10
store i32 %1, i32* %11
store i8* %2, i8** %12
store i32 %3, i32* %13
store i8* %4, i8** %14
store i32 %5, i32* %15
store i32* %6, i32** %16
store i32* %7, i32** %17
%18 = load i32, i32* %10
%19 = load i32, i32* @I2C_SLAVE
%20 = load i32, i32* %11
%21 = call i64 @ioctl(i32 %18, i32 %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %8
%24 = load i32*, i32** %16
store i32 -1, i32* %24
store i32 -1, i32* %9
br label %43
25: ; preds = %8
%26 = load i32, i32* %10
%27 = load i8*, i8** %12
%28 = load i32, i32* %13
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
%30 = load i32, i32* %13
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = load i32*, i32** %16
store i32 -2, i32* %33
br label %37
34: ; preds = %25
%35 = load i32, i32* %13
%36 = load i32*, i32** %16
store i32 %35, i32* %36
br label %37
37: ; preds = %34, %32
%38 = load i32, i32* %10
%39 = load i8*, i8** %14
%40 = load i32, i32* %15
%41 = call i32 @read(i32 %38, i8* %39, i32 %40)
%42 = load i32*, i32** %17
store i32 %41, i32* %42
store i32 %41, i32* %9
br label %43
43: ; preds = %37, %23
%44 = load i32, i32* %9
ret i32 %44
}

LLVM IR (-O2):
@I2C_SLAVE = global i32 0
define i32 @write_word_read_byte(i32 %0, i32 %1, i8* nocapture %2, i32 %3, i8* nocapture %4, i32 %5, i32* nocapture writeonly %6, i32* nocapture writeonly %7) {
%9 = load i32, i32* @I2C_SLAVE
%10 = tail call i64 @ioctl(i32 %0, i32 %9, i32 %1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 -1, i32* %6
br label %18
13: ; preds = %8
%14 = tail call i32 @write(i32 %0, i8* %2, i32 %3)
%15 = icmp eq i32 %14, %3
%16 = select i1 %15, i32 %3, i32 -2
store i32 %16, i32* %6
%17 = tail call i32 @read(i32 %0, i8* %4, i32 %5)
store i32 %17, i32* %7
br label %18
18: ; preds = %13, %12
%19 = phi i32 [ -1, %12 ], [ %17, %13 ]
ret i32 %19
}