#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int rpicm_i2c_detect_bus() {
  int fd;
  if ((fd = open("/dev/i2c-0", O_RDWR)) != -1) {
    close(fd);
    return 0;
  } else if ((fd = open("/dev/i2c-1", O_RDWR)) != -1) {
    close(fd);
    return 1;
  } else if ((fd = open("/dev/i2c-2", O_RDWR)) != -1) {
    close(fd);
    return 2;
  }
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<rpicm_i2c_detect_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2eb9(%rip),%rax
mov    (%rax),%esi
lea    0xec8(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     115d <rpicm_i2c_detect_bus+0x3d>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e0 <rpicm_i2c_detect_bus+0xc0>
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     1192 <rpicm_i2c_detect_bus+0x72>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <rpicm_i2c_detect_bus+0xc0>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     11c7 <rpicm_i2c_detect_bus+0xa7>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0x2,-0x4(%rbp)
jmp    11e0 <rpicm_i2c_detect_bus+0xc0>
jmp    11cc <rpicm_i2c_detect_bus+0xac>
jmp    11d1 <rpicm_i2c_detect_bus+0xb1>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpicm_i2c_detect_bus>:
push   %rbx
mov    0x2ec0(%rip),%rbx
mov    (%rbx),%esi
lea    0xecf(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     113f <rpicm_i2c_detect_bus+0x1f>
xor    %ebx,%ebx
jmp    1175 <rpicm_i2c_detect_bus+0x55>
mov    (%rbx),%esi
lea    0xec3(%rip),%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     1159 <rpicm_i2c_detect_bus+0x39>
mov    $0x1,%ebx
jmp    1175 <rpicm_i2c_detect_bus+0x55>
mov    (%rbx),%esi
lea    0xeb4(%rip),%rdi
call   1040 <open@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
setne  %cl
lea    (%rcx,%rcx,2),%ebx
add    $0xffffffff,%ebx
mov    %eax,%edi
call   1030 <close@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"/dev/i2c-1\00"
@.str.2 = [11 x i8] c"/dev/i2c-2\00"
define i32 @rpicm_i2c_detect_bus() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = icmp ne i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* %2
%8 = call i32 @close(i32 %7)
store i32 0, i32* %1
br label %28
9: ; preds = %0
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %2
%15 = call i32 @close(i32 %14)
store i32 1, i32* %1
br label %28
16: ; preds = %9
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i32 %18, i32* %2
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
store i32 2, i32* %1
br label %28
23: ; preds = %16
br label %24
24: ; preds = %23
br label %25
25: ; preds = %24
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %1
br label %28
28: ; preds = %25, %20, %13, %6
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/i2c-0\00"
@O_RDWR = global i32 0
@.str.1 = [11 x i8] c"/dev/i2c-1\00"
@.str.2 = [11 x i8] c"/dev/i2c-2\00"
define i32 @rpicm_i2c_detect_bus() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %13
4: ; preds = %0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
%12 = select i1 %11, i32 -1, i32 2
br label %13
13: ; preds = %8, %4, %0
%14 = phi i32 [ %2, %0 ], [ %6, %4 ], [ %10, %8 ]
%15 = phi i32 [ 0, %0 ], [ 1, %4 ], [ %12, %8 ]
%16 = tail call i32 @close(i32 %14)
ret i32 %15
}