#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 char __u8;
typedef int /*<<< orphan*/ __s32;

/* Variables and functions */
int /*<<< orphan*/ I2C_SLAVE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int write(int, char *, int);
int main(void) {
  int file;
  int adapter_nr = 1;
  char filename[20];
  snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
  file = open(filename, O_RDWR);
  if (file < 0) {
    exit(1);
  }
  int addr = 0x48;
  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    exit(1);
  }
  __u8 reg = 0x00;
  __s32 res;
  char buf[10];
  buf[0] = reg;
  if (write(file, buf, 1) != 1) {
  }
  if (read(file, buf, 1) != 1) {
  } else {
    printf("Channel	Data:	%u\n", buf[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x13,%esi
lea    0xe67(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c7 <main+0x57>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x48,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    11f6 <main+0x86>
mov    $0x1,%edi
call   1090 <exit@plt>
movb   $0x0,-0x25(%rbp)
mov    -0x25(%rbp),%al
mov    %al,-0x36(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x36(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     121f <main+0xaf>
jmp    121f <main+0xaf>
mov    -0x8(%rbp),%edi
lea    -0x36(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
je     123e <main+0xce>
jmp    124e <main+0xde>
lea    0xdc7(%rip),%rdi
movsbl -0x36(%rbp),%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xe82(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x13,%esi
mov    $0x1,%ecx
call   1050 <snprintf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     120e <main+0x9e>
mov    %eax,%ebx
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x48,%edx
call   1060 <ioctl@plt>
test   %rax,%rax
js     120e <main+0x9e>
movb   $0x0,0x6(%rsp)
lea    0x6(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1204 <main+0x94>
movsbl 0x6(%rsp),%esi
lea    0xe0d(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@.str.1 = [18 x i8] c"Channel\09Data:\09%u\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
%8 = alloca [10 x i8]
store i32 0, i32* %1
store i32 1, i32* %3
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @snprintf(i8* %9, i32 19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
store i32 72, i32* %5
%20 = load i32, i32* %2
%21 = load i32, i32* @I2C_SLAVE
%22 = load i32, i32* %5
%23 = call i64 @ioctl(i32 %20, i32 %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
store i8 0, i8* %6
%28 = load i8, i8* %6
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
store i8 %28, i8* %29
%30 = load i32, i32* %2
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 1)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %27
br label %35
35: ; preds = %34, %27
%36 = load i32, i32* %2
%37 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%38 = call i32 @read(i32 %36, i8* %37, i32 1)
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %41
40: ; preds = %35
br label %45
41: ; preds = %35
%42 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%43 = load i8, i8* %42
%44 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8 signext %43)
br label %45
45: ; preds = %41, %40
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@.str.1 = [18 x i8] c"Channel\09Data:\09%u\0A\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = alloca [10 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 1)
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @I2C_SLAVE
%12 = call i64 @ioctl(i32 %6, i32 %11, i32 72)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
store i8 0, i8* %17
%18 = call i32 @write(i32 %6, i8* %17, i32 1)
%19 = call i32 @read(i32 %6, i8* %17, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8, i8* %17
%23 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8 signext %22)
br label %24
24: ; preds = %16, %21
ret i32 0
}