#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 */

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
void i2c_test(void) {
  int fd, ret;
  char buffer[256] = {0};
  fd = open("/dev/i2c-0", O_RDWR);
  if (fd < 0) {
    perror("open i2c-dev error");
    exit(1);
  }
  if (ioctl(fd, I2C_SLAVE, 0x50) < 0) {
    perror("set addr error");
    exit(1);
  }
  ret = read(fd, buffer, 32);
  if (ret >= 0) {
    buffer[ret] = 0;
    printf("%s\n", buffer);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<i2c_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0xe52(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11d6 <i2c_test+0x56>
lea    0xe44(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    $0x50,%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    120c <i2c_test+0x8c>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x20,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     124c <i2c_test+0xcc>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xde6(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_test>:
push   %rbx
sub    $0x100,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0xe0e(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     124f <i2c_test+0xdf>
mov    %eax,%ebx
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x50,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1258 <i2c_test+0xe8>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x20,%edx
call   1060 <read@plt>
test   %eax,%eax
js     123f <i2c_test+0xcf>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xdf6(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x100,%rsp
pop    %rbx
ret
lea    0xdb5(%rip),%rdi
jmp    125f <i2c_test+0xef>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"open i2c-dev error\00"
@I2C_SLAVE = global i32 0
@.str.2 = [15 x i8] c"set addr error\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define void @i2c_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = bitcast [256 x i8]* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %1
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %1
%14 = load i32, i32* @I2C_SLAVE
%15 = call i64 @ioctl(i32 %13, i32 %14, i32 80)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = load i32, i32* %1
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 32)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = load i32, i32* %2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%31 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %26, %20
%33 = load i32, i32* %1
%34 = call i32 @close(i32 %33)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"open i2c-dev error\00"
@I2C_SLAVE = global i32 0
@.str.2 = [15 x i8] c"set addr error\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define void @i2c_test() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* @I2C_SLAVE
%11 = tail call i64 @ioctl(i32 %4, i32 %10, i32 80)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %9
%17 = call i32 @read(i32 %4, i8* %2, i32 32)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = zext i32 %17 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %23
23: ; preds = %19, %16
%24 = call i32 @close(i32 %4)
ret void
}