#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 i2c_smbus_read_byte(int);
scalar_t__ ioctl(int, int, 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, 0x0703, 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    11c6 <scan_i2c_bus+0x86>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    $0x703,%esi
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1187 <scan_i2c_bus+0x47>
lea    0xe83(%rip),%rdi
call   1060 <perror@plt>
jmp    11b3 <scan_i2c_bus+0x73>
mov    -0x4(%rbp),%edi
call   1050 <i2c_smbus_read_byte@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11ae <scan_i2c_bus+0x6e>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
jmp    11b3 <scan_i2c_bus+0x73>
jmp    11b8 <scan_i2c_bus+0x78>
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   %rbx
push   %rax
mov    %edi,%ebx
xor    %ebp,%ebp
lea    0xeae(%rip),%r14
lea    0xec1(%rip),%r15
jmp    1178 <scan_i2c_bus+0x38>
mov    %r14,%rdi
call   1060 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
cmp    $0x7f,%ebp
je     11a4 <scan_i2c_bus+0x64>
mov    %ebx,%edi
mov    $0x703,%esi
mov    %ebp,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
js     115b <scan_i2c_bus+0x1b>
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>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.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 = %26, %1
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 127
br i1 %7, label %8, label %29
8: ; preds = %5
%9 = load i32, i32* %2
%10 = load i32, i32* %3
%11 = call i64 @ioctl(i32 %9, i32 1795, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %25
15: ; preds = %8
%16 = load i32, i32* %2
%17 = call i32 @i2c_smbus_read_byte(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp sge i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* %3
%22 = load i32, i32* %4
%23 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %22)
br label %24
24: ; preds = %20, %15
br label %25
25: ; preds = %24, %13
br label %26
26: ; preds = %25
%27 = load i32, i32* %3
%28 = add nsw i32 %27, 1
store i32 %28, i32* %3
br label %5
29: ; preds = %5
ret void
}

LLVM IR (-O2):
@.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, %13
%3 = phi i32 [ 0, %1 ], [ %14, %13 ]
%4 = tail call i64 @ioctl(i32 %0, i32 1795, i32 %3)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %13
8: ; preds = %2
%9 = tail call i32 @i2c_smbus_read_byte(i32 %0)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %9)
br label %13
13: ; preds = %6, %11, %8
%14 = add nuw nsw i32 %3, 1
%15 = icmp eq i32 %14, 127
br i1 %15, label %16, label %2
16: ; preds = %13
ret void
}