#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);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int i2c_connect(unsigned char i2c_id, unsigned char addr) {
  char filename[14];
  strcpy(filename, "/dev/i2c-");
  char id[5];
  sprintf(id, "%u", i2c_id);
  strcat(filename, id);
  fputs(filename, stderr);
  fputs("\n", stderr);
  int file;
  if ((file = open(filename, O_RDWR)) < 0) {
    perror("open");
    return -1;
  }
  if (ioctl(file, I2C_SLAVE, addr) < 0) {
    perror("ioctl I2C_SLAVE");
    fprintf(stderr, "Kernel module is using the device - force-attaching!\n");
    if (ioctl(file, I2C_SLAVE_FORCE, addr) < 0) {
      perror("ioctl I2C_SLAVE_FORCE");
      close(file);
      return -1;
    }
  }
  return file;
}


x86 assembly (clang, -O0):
<i2c_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %al,-0x6(%rbp)
lea    -0x14(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x19(%rbp),%rdi
lea    0xe4b(%rip),%rsi
movzbl -0x5(%rbp),%edx
call   10b0 <sprintf@plt>
lea    -0x14(%rbp),%rdi
lea    -0x19(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x14(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1040 <fputs@plt>
lea    -0x14(%rbp),%rdi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1232 <i2c_connect+0xa2>
lea    0xdec(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <i2c_connect+0x12c>
mov    -0x20(%rbp),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
movzbl -0x6(%rbp),%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    12b6 <i2c_connect+0x126>
lea    0xdba(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
movzbl -0x6(%rbp),%edx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    12b1 <i2c_connect+0x121>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <i2c_connect+0x12c>
jmp    12b6 <i2c_connect+0x126>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xe58(%rip),%rsi
lea    0x12(%rsp),%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
lea    0xe4e(%rip),%rsi
lea    0xd(%rsp),%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   10b0 <sprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
mov    0x2deb(%rip),%r12
mov    (%r12),%esi
mov    %r15,%rdi
call   1040 <fputs@plt>
mov    (%r12),%esi
lea    0xe62(%rip),%rdi
call   1040 <fputs@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     123a <i2c_connect+0xaa>
mov    %eax,%ebx
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
movzbl %r14b,%ebp
mov    %ebx,%edi
mov    %ebp,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
js     124d <i2c_connect+0xbd>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdcc(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    122b <i2c_connect+0x9b>
lea    0xdbe(%rip),%rdi
call   1090 <perror@plt>
mov    (%r12),%edi
lea    0xdbe(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1050 <ioctl@plt>
test   %rax,%rax
jns    122b <i2c_connect+0x9b>
lea    0xdd1(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1246 <i2c_connect+0xb6>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/i2c-\00"
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
@O_RDWR = global i32 0
@.str.3 = [5 x i8] c"open\00"
@I2C_SLAVE = global i32 0
@.str.4 = [16 x i8] c"ioctl I2C_SLAVE\00"
@.str.5 = [54 x i8] c"Kernel module is using the device - force-attaching!\0A\00"
@I2C_SLAVE_FORCE = global i32 0
@.str.6 = [22 x i8] c"ioctl I2C_SLAVE_FORCE\00"
define i32 @i2c_connect(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca [14 x i8]
%7 = alloca [5 x i8]
%8 = alloca i32
store i8 %0, i8* %4
store i8 %1, i8* %5
%9 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%12 = load i8, i8* %4
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %12)
%14 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %7, i64 0, i64 0
%16 = call i32 @strcat(i8* %14, i8* %15)
%17 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* @stderr
%19 = call i32 @fputs(i8* %17, i32 %18)
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [14 x i8], [14 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %2
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
28: ; preds = %2
%29 = load i32, i32* %8
%30 = load i32, i32* @I2C_SLAVE
%31 = load i8, i8* %5
%32 = call i64 @ioctl(i32 %29, i32 %30, i8 zeroext %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %48
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
%38 = load i32, i32* %8
%39 = load i32, i32* @I2C_SLAVE_FORCE
%40 = load i8, i8* %5
%41 = call i64 @ioctl(i32 %38, i32 %39, i8 zeroext %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %50
47: ; preds = %34
br label %48
48: ; preds = %47, %28
%49 = load i32, i32* %8
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %43, %26
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/i2c-\00"
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
@O_RDWR = global i32 0
@.str.3 = [5 x i8] c"open\00"
@I2C_SLAVE = global i32 0
@.str.4 = [16 x i8] c"ioctl I2C_SLAVE\00"
@.str.5 = [54 x i8] c"Kernel module is using the device - force-attaching!\0A\00"
@I2C_SLAVE_FORCE = global i32 0
@.str.6 = [22 x i8] c"ioctl I2C_SLAVE_FORCE\00"
define i32 @i2c_connect(i8 zeroext %0, i8 zeroext %1) {
%3 = alloca [14 x i8]
%4 = alloca [5 x i8]
%5 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %0)
%9 = call i32 @strcat(i8* %5, i8* %7)
%10 = load i32, i32* @stderr
%11 = call i32 @fputs(i8* %5, i32 %10)
%12 = load i32, i32* @stderr
%13 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %5, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %33
19: ; preds = %2
%20 = load i32, i32* @I2C_SLAVE
%21 = call i64 @ioctl(i32 %15, i32 %20, i8 zeroext %1)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
%27 = load i32, i32* @I2C_SLAVE_FORCE
%28 = call i64 @ioctl(i32 %15, i32 %27, i8 zeroext %1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%32 = call i32 @close(i32 %15)
br label %33
33: ; preds = %19, %23, %30, %17
%34 = phi i32 [ -1, %17 ], [ -1, %30 ], [ %15, %23 ], [ %15, %19 ]
ret i32 %34
}