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

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ perror(char *);
int i2c_write(int fd, unsigned char cmd, unsigned char par,
              unsigned char empty_par) {
  unsigned char buf[2];
  struct i2c_rdwr_ioctl_data packets;
  struct i2c_msg messages[1];
  messages[0].addr = 0x48;
  messages[0].flags = 0;
  messages[0].buf = buf;
  buf[0] = cmd;
  if (empty_par == 1) {
    messages[0].len = 1;
  } else {
    messages[0].len = sizeof(buf);
    buf[1] = par;
  }
  packets.msgs = messages;
  packets.nmsgs = 1;
  if (ioctl(fd, I2C_RDWR, &packets) < 0) {
    perror("data could not be send");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<i2c_write>:
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)
movl   $0x48,-0x40(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0xd(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0xd(%rbp)
movzbl -0xb(%rbp),%eax
cmp    $0x1,%eax
jne    1171 <i2c_write+0x51>
movl   $0x1,-0x30(%rbp)
jmp    117e <i2c_write+0x5e>
movl   $0x2,-0x30(%rbp)
mov    -0xa(%rbp),%al
mov    %al,-0xc(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11c4 <i2c_write+0xa4>
lea    0xe4d(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <i2c_write+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_write>:
sub    $0x48,%rsp
movl   $0x48,0x20(%rsp)
movq   $0x0,0x38(%rsp)
lea    0xe(%rsp),%rax
mov    %rax,0x28(%rsp)
mov    %sil,0xe(%rsp)
mov    $0x1,%eax
cmp    $0x1,%cl
je     1157 <i2c_write+0x37>
mov    %dl,0xf(%rsp)
mov    $0x2,%eax
mov    %eax,0x30(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1,0x10(%rsp)
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     118f <i2c_write+0x6f>
add    $0x48,%rsp
ret
lea    0xe6a(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x48,%rsp
ret

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i8*, i32, i64 }
@I2C_RDWR = global i32 0
@.str = [23 x i8] c"data could not be send\00"
define i32 @i2c_write(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 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%14 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 0
store i32 72, i32* %14
%15 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%16 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %15, i32 0, i32 3
store i64 0, i64* %16
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%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 i8* %17, i8** %19
%20 = load i8, i8* %7
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
store i8 %20, i8* %21
%22 = load i8, i8* %9
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %28
25: ; preds = %4
%26 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%27 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %26, i32 0, i32 2
store i32 1, i32* %27
br label %33
28: ; preds = %4
%29 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%30 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %29, i32 0, i32 2
store i32 2, i32* %30
%31 = load i8, i8* %8
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
store i8 %31, i8* %32
br label %33
33: ; preds = %28, %25
%34 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %12, i64 0, i64 0
%35 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %11, i32 0, i32 1
store %struct.i2c_msg* %34, %struct.i2c_msg** %35
%36 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %11, i32 0, i32 0
store i32 1, i32* %36
%37 = load i32, i32* %6
%38 = load i32, i32* @I2C_RDWR
%39 = call i64 @ioctl(i32 %37, i32 %38, %struct.i2c_rdwr_ioctl_data* %11)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %44
43: ; preds = %33
store i32 0, i32* %5
br label %44
44: ; preds = %43, %41
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i8*, i32, i64 }
@I2C_RDWR = global i32 0
@.str = [23 x i8] c"data could not be send\00"
define i32 @i2c_write(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 = bitcast [1 x %struct.i2c_msg]* %7 to i8*
%11 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0
%12 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 0
store i32 72, i32* %12
%13 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 3
store i64 0, i64* %13
%14 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 1
store i8* %8, i8** %14
store i8 %1, i8* %8
%15 = icmp eq i8 %3, 1
br i1 %15, label %18, label %16
16: ; preds = %4
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %2, i8* %17
br label %18
18: ; preds = %4, %16
%19 = phi i32 [ 2, %16 ], [ 1, %4 ]
%20 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %7, i64 0, i64 0, i32 2
store i32 %19, i32* %20
%21 = 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** %21
%22 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 0
store i32 1, i32* %22
%23 = load i32, i32* @I2C_RDWR
%24 = call i64 @ioctl(i32 %0, i32 %23, %struct.i2c_rdwr_ioctl_data* %6)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %18, %26
%29 = phi i32 [ -1, %26 ], [ 0, %18 ]
ret i32 %29
}