#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 /*<<< orphan*/ I2C_SLAVE;
int ioctl(int, int /*<<< orphan*/, int);
int read(int, int /*<<< orphan*/ *, int);
int write(int, int /*<<< orphan*/ *, int);
int i2c_transact(int fd, int chip_addr, uint8_t *buffer, int nw, int nr) {
  int err = ioctl(fd, I2C_SLAVE, chip_addr);
  if ((err == 0) && (nw >= 0))
    err = write(fd, buffer, nw);
  if ((err > 0) && (nr >= 0))
    err = read(fd, buffer, nr);
  return err;
}


x86 assembly (clang, -O0):
<i2c_transact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1186 <i2c_transact+0x56>
cmpl   $0x0,-0x14(%rbp)
jl     1186 <i2c_transact+0x56>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11ac <i2c_transact+0x7c>
cmpl   $0x0,-0x18(%rbp)
jl     11ac <i2c_transact+0x7c>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_transact>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %ecx,%ebx
mov    %rdx,%r14
mov    %esi,%edx
mov    %edi,%ebp
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
test   %ebx,%ebx
js     1165 <i2c_transact+0x35>
test   %eax,%eax
jne    1165 <i2c_transact+0x35>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %r15d,%r15d
js     1185 <i2c_transact+0x55>
test   %eax,%eax
jle    1185 <i2c_transact+0x55>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <read@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@I2C_SLAVE = global i32 0
define i32 @i2c_transact(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
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = load i32, i32* %6
%13 = load i32, i32* @I2C_SLAVE
%14 = load i32, i32* %7
%15 = call i32 @ioctl(i32 %12, i32 %13, i32 %14)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %5
%19 = load i32, i32* %9
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* %6
%23 = load i32*, i32** %8
%24 = load i32, i32* %9
%25 = call i32 @write(i32 %22, i32* %23, i32 %24)
store i32 %25, i32* %11
br label %26
26: ; preds = %21, %18, %5
%27 = load i32, i32* %11
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32, i32* %10
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* %6
%34 = load i32*, i32** %8
%35 = load i32, i32* %10
%36 = call i32 @read(i32 %33, i32* %34, i32 %35)
store i32 %36, i32* %11
br label %37
37: ; preds = %32, %29, %26
%38 = load i32, i32* %11
ret i32 %38
}

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