#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;
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stdin;
int main(int argc, char **argv) {
  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) {
    printf("ARRRGGGGGGGG File open failed\n");
    return (1);
  }
  int addr = 0x04;
  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    printf("ARRRGGGGGGGG ioctl failed\n");
    return (1);
  }
  char buf[10];
  while (1) {
    int res = read(file, buf, 1);
    if (res != 1) {
      printf("read failed !!!!!!%d\n", res);
    } else {
      putc(buf[0], stdin);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    $0x13,%esi
lea    0xe70(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ce <main+0x6e>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125f <main+0xff>
movl   $0x4,-0x34(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    -0x34(%rbp),%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    120d <main+0xad>
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125f <main+0xff>
jmp    1212 <main+0xb2>
mov    -0x14(%rbp),%edi
lea    -0x3e(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
je     1246 <main+0xe6>
mov    -0x44(%rbp),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125a <main+0xfa>
mov    -0x3e(%rbp),%al
mov    0x2d90(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1070 <putc@plt>
jmp    1212 <main+0xb2>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xe7e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x13,%esi
mov    $0x1,%ecx
call   1050 <snprintf@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1210 <main+0xa0>
mov    %eax,%ebx
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1219 <main+0xa9>
lea    0x6(%rsp),%r14
mov    0x2e0a(%rip),%r12
lea    0xe2f(%rip),%r15
jmp    11ec <main+0x7c>
nop
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    11e0 <main+0x70>
movsbl 0x6(%rsp),%edi
mov    (%r12),%esi
call   1080 <putc@plt>
jmp    11ec <main+0x7c>
lea    0xe25(%rip),%rdi
jmp    1220 <main+0xb0>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/i2c-%d\00"
@O_RDWR = global i32 0
@I2C_SLAVE = global i32 0
@.str.3 = [22 x i8] c"read failed !!!!!!%d\0A\00"
@stdin = global i32 0
@str = [26 x i8] c"ARRRGGGGGGGG ioctl failed\00"
@str.4 = [30 x i8] c"ARRRGGGGGGGG File open failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [20 x i8]
%4 = alloca [10 x i8]
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 1)
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %2
%11 = load i32, i32* @I2C_SLAVE
%12 = call i64 @ioctl(i32 %8, i32 %11, i32 4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %26, label %14
14: ; preds = %10
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
br label %16
16: ; preds = %25, %14
%17 = call i32 @read(i32 %8, i8* %15, i32 1)
%18 = icmp eq i32 %17, 1
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %17)
br label %25
21: ; preds = %16
%22 = load i8, i8* %15
%23 = load i32, i32* @stdin
%24 = call i32 @putc(i8 signext %22, i32 %23)
br label %25
25: ; preds = %21, %19
br label %16
26: ; preds = %10, %2
%27 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @str.4, i64 0, i64 0), %2 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0), %10 ]
%28 = call i32 @puts(i8* dereferenceable(1) %27)
ret i32 1
}