void scan_i2c_bus(int file) {
  int port;
  printf("\n%s(+):", __FUNCTION__);
  for (port = 0; port < 127; port++) {
    int res;
    if (set_slave(file, port) != -1) {
      res = i2c_smbus_read_byte(file);
      if (res >= 0) {
        printf("i2c chip found at: %x, val = %d\n", port, res);
      }
    }
  }
  printf("\n%s(-):", __FUNCTION__);
}


x86 assembly (clang, -O0):
<scan_i2c_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xebe(%rip),%rdi
lea    0xebf(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x7f,-0x8(%rbp)
jge    11ba <scan_i2c_bus+0x8a>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    $0x0,%al
call   1030 <set_slave@plt>
cmp    $0xffffffff,%eax
je     11a7 <scan_i2c_bus+0x77>
mov    -0x4(%rbp),%edi
mov    $0x0,%al
call   1050 <i2c_smbus_read_byte@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     11a2 <scan_i2c_bus+0x72>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11a7 <scan_i2c_bus+0x77>
jmp    11ac <scan_i2c_bus+0x7c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1157 <scan_i2c_bus+0x27>
lea    0xe75(%rip),%rdi
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan_i2c_bus>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
lea    0xec3(%rip),%rdi
lea    0xec4(%rip),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec3(%rip),%r14
xor    %ebp,%ebp
jmp    1168 <scan_i2c_bus+0x38>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0x7f,%ebp
je     1195 <scan_i2c_bus+0x65>
mov    %ebx,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <set_slave@plt>
cmp    $0xffffffff,%eax
je     1160 <scan_i2c_bus+0x30>
mov    %ebx,%edi
xor    %eax,%eax
call   1050 <i2c_smbus_read_byte@plt>
test   %eax,%eax
js     1160 <scan_i2c_bus+0x30>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1160 <scan_i2c_bus+0x30>
lea    0xe9a(%rip),%rdi
lea    0xe65(%rip),%rsi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <printf@plt>

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

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