#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 /*<<< orphan*/ I2C_SLAVE_FORCE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(unsigned char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(unsigned char *, char *, int);
int dev_open(int bus_no, int slave_address, int force_slave) {
  int dev_fd;
  unsigned char filename[20];
  sprintf(filename, "/dev/i2c-%d", bus_no);
  dev_fd = open(filename, O_RDWR);
  if (dev_fd > 0) {
    if (ioctl(dev_fd, force_slave ? I2C_SLAVE_FORCE : I2C_SLAVE,
              slave_address) < 0) {
      close(dev_fd);
      dev_fd = -1;
    }
  }
  return (dev_fd);
}


x86 assembly (clang, -O0):
<dev_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xea1(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11dc <dev_open+0x9c>
mov    -0x10(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11a4 <dev_open+0x64>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    11b0 <dev_open+0x70>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%esi
mov    -0x8(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11d7 <dev_open+0x97>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x10(%rbp)
jmp    11dc <dev_open+0x9c>
mov    -0x10(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dev_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %esi,%r14d
mov    %edi,%edx
lea    0xeaa(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11a9 <dev_open+0x69>
test   %ebp,%ebp
jne    1185 <dev_open+0x45>
mov    0x2e55(%rip),%rax
jmp    118c <dev_open+0x4c>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
jns    11a9 <dev_open+0x69>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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