#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 i2c_smbus_read_byte(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
void scan_i2c_bus(int device) {
  int port, res;
  for (port = 0; port < 127; port++) {
    if (ioctl(device, I2C_SLAVE, port) < 0)
      perror("ioctl() I2C_SLAVE failed\n");
    else {
      res = i2c_smbus_read_byte(device);
      if (res >= 0)
        printf("i2c chip found at: %x, val = %d\n", port, res);
    }
  }
}


x86 assembly (clang, -O0):
<scan_i2c_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x7f,-0x8(%rbp)
jge    11ca <scan_i2c_bus+0x8a>
mov    -0x4(%rbp),%edi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    118b <scan_i2c_bus+0x4b>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
jmp    11b7 <scan_i2c_bus+0x77>
mov    -0x4(%rbp),%edi
call   1050 <i2c_smbus_read_byte@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11b2 <scan_i2c_bus+0x72>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
jmp    11b7 <scan_i2c_bus+0x77>
jmp    11bc <scan_i2c_bus+0x7c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1152 <scan_i2c_bus+0x12>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_i2c_bus>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%ebx
xor    %ebp,%ebp
mov    0x2e95(%rip),%r12
lea    0xea6(%rip),%r14
lea    0xeb9(%rip),%r15
jmp    1178 <scan_i2c_bus+0x38>
mov    %r14,%rdi
call   1060 <perror@plt>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0x7f,%ebp
je     11a3 <scan_i2c_bus+0x63>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1163 <scan_i2c_bus+0x23>
mov    %ebx,%edi
call   1050 <i2c_smbus_read_byte@plt>
test   %eax,%eax
js     1170 <scan_i2c_bus+0x30>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <printf@plt>
jmp    1170 <scan_i2c_bus+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@I2C_SLAVE = global i32 0
@.str = [26 x i8] c"ioctl() I2C_SLAVE failed\0A\00"
@.str.1 = [33 x i8] c"i2c chip found at: %x, val = %d\0A\00"
define void @scan_i2c_bus(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
br label %5
5: ; preds = %27, %1
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 127
br i1 %7, label %8, label %30
8: ; preds = %5
%9 = load i32, i32* %2
%10 = load i32, i32* @I2C_SLAVE
%11 = load i32, i32* %3
%12 = call i64 @ioctl(i32 %9, i32 %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %26
16: ; preds = %8
%17 = load i32, i32* %2
%18 = call i32 @i2c_smbus_read_byte(i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* %3
%23 = load i32, i32* %4
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23)
br label %25
25: ; preds = %21, %16
br label %26
26: ; preds = %25, %14
br label %27
27: ; preds = %26
%28 = load i32, i32* %3
%29 = add nsw i32 %28, 1
store i32 %29, i32* %3
br label %5
30: ; preds = %5
ret void
}

LLVM IR (-O2):
@I2C_SLAVE = global i32 0
@.str = [26 x i8] c"ioctl() I2C_SLAVE failed\0A\00"
@.str.1 = [33 x i8] c"i2c chip found at: %x, val = %d\0A\00"
define void @scan_i2c_bus(i32 %0) {
br label %2
2: ; preds = %1, %14
%3 = phi i32 [ 0, %1 ], [ %15, %14 ]
%4 = load i32, i32* @I2C_SLAVE
%5 = tail call i64 @ioctl(i32 %0, i32 %4, i32 %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %14
9: ; preds = %2
%10 = tail call i32 @i2c_smbus_read_byte(i32 %0)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %10)
br label %14
14: ; preds = %7, %12, %9
%15 = add nuw nsw i32 %3, 1
%16 = icmp eq i32 %15, 127
br i1 %16, label %17, label %2
17: ; preds = %14
ret void
}