#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 uint8_t;
typedef int uint16_t;
typedef int reg;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ i2c_smbus_write_word_data(int, int, int);
scalar_t__ ioctl(int, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int wi2c(uint8_t addr, uint16_t reg, uint8_t value) {
  uint8_t res, w[2];
  w[0] = (reg)&0xff;
  w[1] = (reg >> 8) & 0xff;
  w[2] = value;
  int fd;
  if ((fd = open("/dev/i2c-0", O_RDWR)) < 0) {
    printf("open error! %d\n", fd);
    return -1;
  }
  if (ioctl(fd, 0x0703, addr) < 0) {
    printf("address error!\n");
    return -1;
  }
  i2c_smbus_write_word_data(fd, reg & 0xff,
                            ((uint16_t)value << 8) | ((reg >> 8) & 0xff));
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<wi2c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11be <wi2c+0x6e>
mov    -0x20(%rbp),%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <wi2c+0xd5>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    $0x703,%esi
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11f2 <wi2c+0xa2>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <wi2c+0xd5>
mov    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
and    $0xff,%esi
mov    -0x10(%rbp),%edx
shl    $0x8,%edx
mov    -0xc(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
or     %eax,%edx
call   1060 <i2c_smbus_write_word_data@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wi2c>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c6 <wi2c+0x66>
mov    %ebx,%edi
mov    $0x703,%esi
mov    %ebp,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     11d8 <wi2c+0x78>
mov    %r14d,%eax
shr    $0x8,%eax
movzbl %r14b,%esi
shl    $0x8,%r15d
movzbl %al,%edx
or     %r15d,%edx
mov    %ebx,%edi
call   1070 <i2c_smbus_write_word_data@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%eax
jmp    11e9 <wi2c+0x89>
lea    0xe3e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e4 <wi2c+0x84>
lea    0xe3c(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"open error! %d\0A\00"
@.str.2 = [16 x i8] c"address error!\0A\00"
define i32 @wi2c(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i32]
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32, i32* %6
%12 = and i32 %11, 255
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
store i32 %12, i32* %13
%14 = load i32, i32* %6
%15 = ashr i32 %14, 8
%16 = and i32 %15, 255
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
store i32 %16, i32* %17
%18 = load i32, i32* %7
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 2
store i32 %18, i32* %19
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %10
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = load i32, i32* %10
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 -1, i32* %4
br label %46
26: ; preds = %3
%27 = load i32, i32* %10
%28 = load i32, i32* %5
%29 = call i64 @ioctl(i32 %27, i32 1795, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %46
33: ; preds = %26
%34 = load i32, i32* %10
%35 = load i32, i32* %6
%36 = and i32 %35, 255
%37 = load i32, i32* %7
%38 = shl i32 %37, 8
%39 = load i32, i32* %6
%40 = ashr i32 %39, 8
%41 = and i32 %40, 255
%42 = or i32 %38, %41
%43 = call i32 @i2c_smbus_write_word_data(i32 %34, i32 %36, i32 %42)
%44 = load i32, i32* %10
%45 = call i32 @close(i32 %44)
store i32 1, i32* %4
br label %46
46: ; preds = %33, %31, %23
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"open error! %d\0A\00"
@str = [15 x i8] c"address error!\00"
define i32 @wi2c(i32 %0, i32 %1, i32 %2) {
%4 = lshr i32 %1, 8
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %22
10: ; preds = %3
%11 = tail call i64 @ioctl(i32 %6, i32 1795, i32 %0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %22
15: ; preds = %10
%16 = and i32 %1, 255
%17 = shl i32 %2, 8
%18 = and i32 %4, 255
%19 = or i32 %17, %18
%20 = tail call i32 @i2c_smbus_write_word_data(i32 %6, i32 %16, i32 %19)
%21 = tail call i32 @close(i32 %6)
br label %22
22: ; preds = %15, %13, %8
%23 = phi i32 [ -1, %8 ], [ -1, %13 ], [ 1, %15 ]
ret i32 %23
}