#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 */
struct i2c_rdwr_ioctl_data {
  int nmsgs;
  struct i2c_msg *msgs;
};
struct i2c_msg {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int do_rdwr(int fd, struct i2c_msg *msgs, int nmsgs) {
  struct i2c_rdwr_ioctl_data msgset = {
      .msgs = msgs,
      .nmsgs = nmsgs,
  };
  if (msgs == NULL || nmsgs <= 0)
    return -1;
  if (ioctl(fd, I2C_RDWR, &msgset) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<do_rdwr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1145 <do_rdwr+0x35>
cmpl   $0x0,-0x14(%rbp)
jg     1151 <do_rdwr+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1183 <do_rdwr+0x73>
mov    -0x8(%rbp),%edi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    117c <do_rdwr+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1183 <do_rdwr+0x73>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_rdwr>:
sub    $0x18,%rsp
mov    %edx,0x8(%rsp)
mov    %rsi,0x10(%rsp)
mov    $0xffffffff,%eax
test   %rsi,%rsi
je     1142 <do_rdwr+0x32>
test   %edx,%edx
jle    1142 <do_rdwr+0x32>
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
sar    $0x3f,%rax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.i2c_msg = type { i32 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@I2C_RDWR = global i32 0
define i32 @do_rdwr(i32 %0, %struct.i2c_msg* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.i2c_msg*
%7 = alloca i32
%8 = alloca %struct.i2c_rdwr_ioctl_data
store i32 %0, i32* %5
store %struct.i2c_msg* %1, %struct.i2c_msg** %6
store i32 %2, i32* %7
%9 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %8, i32 0, i32 0
%10 = load i32, i32* %7
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %8, i32 0, i32 1
%12 = load %struct.i2c_msg*, %struct.i2c_msg** %6
store %struct.i2c_msg* %12, %struct.i2c_msg** %11
%13 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%14 = icmp eq %struct.i2c_msg* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i32, i32* %7
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15, %3
store i32 -1, i32* %4
br label %26
19: ; preds = %15
%20 = load i32, i32* %5
%21 = load i32, i32* @I2C_RDWR
%22 = call i64 @ioctl(i32 %20, i32 %21, %struct.i2c_rdwr_ioctl_data* %8)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %4
br label %26
25: ; preds = %19
store i32 0, i32* %4
br label %26
26: ; preds = %25, %24, %18
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
%struct.i2c_msg = type { i32 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@I2C_RDWR = global i32 0
define i32 @do_rdwr(i32 %0, %struct.i2c_msg* %1, i32 %2) {
%4 = alloca %struct.i2c_rdwr_ioctl_data
%5 = bitcast %struct.i2c_rdwr_ioctl_data* %4 to i8*
%6 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %4, i64 0, i32 0
store i32 %2, i32* %6
%7 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %4, i64 0, i32 1
store %struct.i2c_msg* %1, %struct.i2c_msg** %7
%8 = icmp eq %struct.i2c_msg* %1, null
%9 = icmp slt i32 %2, 1
%10 = or i1 %8, %9
br i1 %10, label %16, label %11
11: ; preds = %3
%12 = load i32, i32* @I2C_RDWR
%13 = call i64 @ioctl(i32 %0, i32 %12, %struct.i2c_rdwr_ioctl_data* %4)
%14 = ashr i64 %13, 63
%15 = trunc i64 %14 to i32
br label %16
16: ; preds = %11, %3
%17 = phi i32 [ -1, %3 ], [ %15, %11 ]
ret i32 %17
}