#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 */
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
scalar_t__ snprintf(char *, int, char *, char);
int /*<<< orphan*/ stderr;
int rpicm_i2c_open_bus(int i2c_bus) {
  if (i2c_bus < 0 || i2c_bus > 2) {
    fprintf(stderr, "rpicm_i2c_open_bus(int) recieved unexpected arg (%d)\n",
            i2c_bus);
    return -1;
  }
  int fd;
  char filename[12];
  char i2c_bus_id = (char)i2c_bus;
  if ((snprintf(filename, sizeof(filename), "/dev/i2c-%c", i2c_bus_id)) < 0) {
    fprintf(stderr, "rpicm_i2c_open_bus(int) snprintf error\n");
    return -1;
  }
  if ((fd = open(filename, O_RDWR)) < 0) {
    fprintf(stderr, "rpicm_i2c_open_bus(int) failed to open %s\n", filename);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<rpicm_i2c_open_bus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     114f <rpicm_i2c_open_bus+0x1f>
cmpl   $0x2,-0x8(%rbp)
jle    1175 <rpicm_i2c_open_bus+0x45>
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe9e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <rpicm_i2c_open_bus+0xdc>
mov    -0x8(%rbp),%eax
mov    %al,-0x19(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
lea    0xeab(%rip),%rdx
movsbl -0x19(%rbp),%ecx
call   1030 <snprintf@plt>
cmp    $0x0,%rax
jge    11c1 <rpicm_i2c_open_bus+0x91>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <rpicm_i2c_open_bus+0xdc>
lea    -0x18(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1206 <rpicm_i2c_open_bus+0xd6>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <rpicm_i2c_open_bus+0xdc>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rpicm_i2c_open_bus>:
sub    $0x18,%rsp
mov    %edi,%edx
cmp    $0x3,%edi
jb     1154 <rpicm_i2c_open_bus+0x24>
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    0xeb5(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11c3 <rpicm_i2c_open_bus+0x93>
movsbl %dl,%ecx
lea    0xed8(%rip),%rdx
lea    0xc(%rsp),%rdi
mov    $0xc,%esi
call   1030 <snprintf@plt>
test   %rax,%rax
js     118e <rpicm_i2c_open_bus+0x5e>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11a7 <rpicm_i2c_open_bus+0x77>
add    $0x18,%rsp
ret
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xea4(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11c3 <rpicm_i2c_open_bus+0x93>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xeb3(%rip),%rsi
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c"rpicm_i2c_open_bus(int) recieved unexpected arg (%d)\0A\00"
@.str.1 = [12 x i8] c"/dev/i2c-%c\00"
@.str.2 = [40 x i8] c"rpicm_i2c_open_bus(int) snprintf error\0A\00"
@O_RDWR = global i32 0
@.str.3 = [43 x i8] c"rpicm_i2c_open_bus(int) failed to open %s\0A\00"
define i32 @rpicm_i2c_open_bus(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [12 x i8]
%6 = alloca i8
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %9, %1
%13 = load i32, i32* @stderr
%14 = load i32, i32* %3
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 -1, i32* %2
br label %37
16: ; preds = %9
%17 = load i32, i32* %3
%18 = trunc i32 %17 to i8
store i8 %18, i8* %6
%19 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%20 = load i8, i8* %6
%21 = call i64 @snprintf(i8* %19, i32 12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 signext %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
26: ; preds = %16
%27 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* @O_RDWR
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %4
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = getelementptr inbounds [12 x i8], [12 x i8]* %5, i64 0, i64 0
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %33)
store i32 -1, i32* %2
br label %37
35: ; preds = %26
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %31, %23, %12
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c"rpicm_i2c_open_bus(int) recieved unexpected arg (%d)\0A\00"
@.str.1 = [12 x i8] c"/dev/i2c-%c\00"
@.str.2 = [40 x i8] c"rpicm_i2c_open_bus(int) snprintf error\0A\00"
@O_RDWR = global i32 0
@.str.3 = [43 x i8] c"rpicm_i2c_open_bus(int) failed to open %s\0A\00"
define i32 @rpicm_i2c_open_bus(i32 %0) {
%2 = alloca [12 x i8]
%3 = icmp ugt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %24
7: ; preds = %1
%8 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%9 = trunc i32 %0 to i8
%10 = call i64 @snprintf(i8* %8, i32 12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8 signext %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %22
15: ; preds = %7
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %8, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i8* %8)
br label %22
22: ; preds = %15, %19, %12
%23 = phi i32 [ -1, %12 ], [ -1, %19 ], [ %17, %15 ]
br label %24
24: ; preds = %22, %4
%25 = phi i32 [ -1, %4 ], [ %23, %22 ]
ret i32 %25
}