#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 {
  unsigned char addr;
  int len;
  unsigned char *buf;
  scalar_t__ flags;
};
typedef int /*<<< orphan*/ outbuf;

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ perror(char *);
int set_i2c_register(int file, unsigned char addr, unsigned char reg,
                     unsigned char value) {
  unsigned char outbuf[2];
  struct i2c_rdwr_ioctl_data packets;
  struct i2c_msg messages[1];
  messages[0].addr = addr;
  messages[0].flags = 0;
  messages[0].len = sizeof(outbuf);
  messages[0].buf = outbuf;
  outbuf[0] = reg;
  outbuf[1] = value;
  packets.msgs = messages;
  packets.nmsgs = 1;
  if (ioctl(file, I2C_RDWR, &packets) < 0) {
    perror("Unable to send data");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_i2c_register>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %sil,%dl
mov    %edi,-0x8(%rbp)
mov    %dl,-0x9(%rbp)
mov    %cl,-0xa(%rbp)
mov    %al,-0xb(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x2,-0x3c(%rbp)
lea    -0xd(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xa(%rbp),%al
mov    %al,-0xd(%rbp)
mov    -0xb(%rbp),%al
mov    %al,-0xc(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11aa <set_i2c_register+0x8a>
lea    0xe67(%rip),%rdi
call   1040 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b1 <set_i2c_register+0x91>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_i2c_register>:
sub    $0x38,%rsp
mov    %sil,0x10(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0x2,0x14(%rsp)
lea    0xe(%rsp),%rax
mov    %rax,0x18(%rsp)
mov    %dl,0xe(%rsp)
mov    %cl,0xf(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x1,0x28(%rsp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0x28(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1180 <set_i2c_register+0x60>
add    $0x38,%rsp
ret
lea    0xe79(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%eax
add    $0x38,%rsp
ret

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i8, i32, i8*, i64 }
@I2C_RDWR = global i32 0
@.str = [20 x i8] c"Unable to send data\00"
define i32 @set_i2c_register(i32 %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca [2 x i8]
%11 = alloca %struct.i2c_rdwr_ioctl_data
%12 = alloca [1 x %struct.i2c_msg]
store i32 %0, i32* %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
%13 = load i8, i8* %7
%14 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%15 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %14, i32 0, i32 0
store i8 %13, i8* %15
%16 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%17 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %16, i32 0, i32 3
store i64 0, i64* %17
%18 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%19 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i32 0, i32 1
store i32 2, i32* %19
%20 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%21 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%22 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %21, i32 0, i32 2
store i8* %20, i8** %22
%23 = load i8, i8* %8
%24 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
store i8 %23, i8* %24
%25 = load i8, i8* %9
%26 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
store i8 %25, i8* %26
%27 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%28 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %11, i32 0, i32 1
store %struct.i2c_msg* %27, %struct.i2c_msg** %28
%29 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %11, i32 0, i32 0
store i32 1, i32* %29
%30 = load i32, i32* %6
%31 = load i32, i32* @I2C_RDWR
%32 = call i64 @ioctl(i32 %30, i32 %31, %struct.i2c_rdwr_ioctl_data* %11)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %4
%35 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %5
br label %37
36: ; preds = %4
store i32 0, i32* %5
br label %37
37: ; preds = %36, %34
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i8, i32, i8*, i64 }
@I2C_RDWR = global i32 0
@.str = [20 x i8] c"Unable to send data\00"
define i32 @set_i2c_register(i32 %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = alloca [2 x i8]
%6 = alloca %struct.i2c_rdwr_ioctl_data
%7 = alloca [1 x %struct.i2c_msg]
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%9 = bitcast %struct.i2c_rdwr_ioctl_data* %6 to i8*
%10 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 0
%11 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0
store i8 %1, i8* %10
%12 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 3
store i64 0, i64* %12
%13 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 1
store i32 2, i32* %13
%14 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 2
store i8* %8, i8** %14
store i8 %2, i8* %8
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %3, i8* %15
%16 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 1
store %struct.i2c_msg* %11, %struct.i2c_msg** %16
%17 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 0
store i32 1, i32* %17
%18 = load i32, i32* @I2C_RDWR
%19 = call i64 @ioctl(i32 %0, i32 %18, %struct.i2c_rdwr_ioctl_data* %6)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %4, %21
%24 = phi i32 [ 1, %21 ], [ 0, %4 ]
ret i32 %24
}