#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;
  int len;
  unsigned char *buf;
  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 buf[2];
  struct i2c_rdwr_ioctl_data packets;
  struct i2c_msg messages[1];
  messages[0].addr = 0x48;
  messages[0].flags = 0;
  messages[0].len = sizeof(buf);
  messages[0].buf = buf;
  buf[0] = cmd;
  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    %dl,%al
mov    %sil,%cl
mov    %edi,-0x8(%rbp)
mov    %cl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
movl   $0x48,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x2,-0x3c(%rbp)
lea    -0xc(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x9(%rbp),%al
mov    %al,-0xc(%rbp)
mov    -0xa(%rbp),%al
mov    %al,-0xb(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11a6 <i2c_write+0x86>
lea    0xe6b(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <i2c_write+0x8d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_write>:
sub    $0x38,%rsp
movabs $0x200000048,%rax
mov    %rax,0x20(%rsp)
movq   $0x0,0x30(%rsp)
lea    0xe(%rsp),%rax
mov    %rax,0x28(%rsp)
mov    %sil,0xe(%rsp)
mov    %dl,0xf(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x1,0x10(%rsp)
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1183 <i2c_write+0x63>
add    $0x38,%rsp
ret
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
ret

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, 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) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca [2 x i8]
%9 = alloca %struct.i2c_rdwr_ioctl_data
%10 = alloca [1 x %struct.i2c_msg]
store i32 %0, i32* %5
store i8 %1, i8* %6
store i8 %2, i8* %7
%11 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %10, i64 0, i64 0
%12 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %11, i32 0, i32 0
store i32 72, i32* %12
%13 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %10, i64 0, i64 0
%14 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %13, i32 0, i32 3
store i64 0, i64* %14
%15 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %10, i64 0, i64 0
%16 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %15, i32 0, i32 1
store i32 2, i32* %16
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %10, i64 0, i64 0
%19 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i32 0, i32 2
store i8* %17, i8** %19
%20 = load i8, i8* %6
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
store i8 %20, i8* %21
%22 = load i8, i8* %7
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
store i8 %22, i8* %23
%24 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %10, i64 0, i64 0
%25 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %9, i32 0, i32 1
store %struct.i2c_msg* %24, %struct.i2c_msg** %25
%26 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %9, i32 0, i32 0
store i32 1, i32* %26
%27 = load i32, i32* %5
%28 = load i32, i32* @I2C_RDWR
%29 = call i64 @ioctl(i32 %27, i32 %28, %struct.i2c_rdwr_ioctl_data* %9)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %3
%32 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %34
33: ; preds = %3
store i32 0, i32* %4
br label %34
34: ; preds = %33, %31
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, 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) {
%4 = alloca [2 x i8]
%5 = alloca %struct.i2c_rdwr_ioctl_data
%6 = alloca [1 x %struct.i2c_msg]
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.i2c_rdwr_ioctl_data* %5 to i8*
%9 = bitcast [1 x %struct.i2c_msg]* %6 to i8*
%10 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %6, i64 0, i64 0
%11 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %6, i64 0, i64 0, i32 0
store i32 72, i32* %11
%12 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %6, i64 0, i64 0, i32 3
store i64 0, i64* %12
%13 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %6, i64 0, i64 0, i32 1
store i32 2, i32* %13
%14 = getelementptr inbounds [1 x %struct.i2c_msg], [1 x %struct.i2c_msg]* %6, i64 0, i64 0, i32 2
store i8* %7, i8** %14
store i8 %1, i8* %7
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 %2, i8* %15
%16 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %5, i64 0, i32 1
store %struct.i2c_msg* %10, %struct.i2c_msg** %16
%17 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %5, 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* %5)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %3, %21
%24 = phi i32 [ -1, %21 ], [ 0, %3 ]
ret i32 %24
}