#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*/ tmp;
struct i2c_rdwr_ioctl_data {
  int nmsgs;
  struct i2c_msg *msgs;
};
struct i2c_msg {
  int dummy;
};
typedef int /*<<< orphan*/ rdwr_msg;
typedef int /*<<< orphan*/ devices;

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ I2C_TIMEOUT;
int /*<<< orphan*/ add_i2c_read_data(int, struct i2c_msg *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct i2c_msg *, int /*<<< orphan*/, int);
int open_i2c_device(char const *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strncat(char *, char *, int);
int i2c_detect(const char *i2c_character_device) {
  int fd = open_i2c_device(i2c_character_device);
  int i;
  unsigned long timeout = 50;
  struct i2c_msg rdwr_msg;
  char devices[512] = "";
  char tmp[128] = "";
  if (fd < 0) {
    printf("Can not open i2c device %s\n", i2c_character_device);
    return 20;
  }
  memset(&rdwr_msg, 0, sizeof(rdwr_msg));
  if (ioctl(fd, I2C_TIMEOUT, timeout) != 0) {
    printf("Can not set timeout\n");
    return 30;
  }
  printf("Check address:\n");
  for (i = 1; i < 0x7F; i++) {
    add_i2c_read_data(i, &rdwr_msg, 1);
    struct i2c_rdwr_ioctl_data rdwr_set;
    rdwr_set.msgs = &rdwr_msg;
    rdwr_set.nmsgs = 1;
    printf("\r0x%02X ", i);
    fflush(stdout);
    if (ioctl(fd, I2C_RDWR, &rdwr_set) >= 0) {
      snprintf(tmp, sizeof(tmp), "0x%02X ", i);
      strncat(devices, tmp, sizeof(devices));
    }
  }
  printf("\n");
  printf("Found devices at addresses:\n");
  printf("%s\n", devices);
  return 0;
}


x86 assembly (clang, -O0):
<i2c_detect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <open_i2c_device@plt>
mov    %eax,-0x14(%rbp)
movq   $0x32,-0x20(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x2b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
cmpl   $0x0,-0x14(%rbp)
jge    11f1 <i2c_detect+0x71>
mov    -0x10(%rbp),%rsi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x14,-0x4(%rbp)
jmp    1342 <i2c_detect+0x1c2>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
mov    $0x0,%al
call   1070 <ioctl@plt>
cmp    $0x0,%rax
je     123c <i2c_detect+0xbc>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1e,-0x4(%rbp)
jmp    1342 <i2c_detect+0x1c2>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x18(%rbp)
cmpl   $0x7f,-0x18(%rbp)
jge    130a <i2c_detect+0x18a>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <add_i2c_read_data@plt>
lea    -0x28(%rbp),%rax
mov    %rax,-0x2b8(%rbp)
movl   $0x1,-0x2c0(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
lea    -0x2c0(%rbp),%rdx
mov    $0x0,%al
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jl     12f7 <i2c_detect+0x177>
lea    -0x2b0(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    $0x80,%esi
lea    0xd68(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    -0x2b0(%rbp),%rsi
mov    $0x200,%edx
call   1080 <strncat@plt>
jmp    12fc <i2c_detect+0x17c>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1251 <i2c_detect+0xd1>
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x230(%rbp),%rsi
lea    0xce4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_detect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2a8,%rsp
mov    %rdi,%rbp
call   1070 <open_i2c_device@plt>
mov    %eax,%ebx
lea    0xa0(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1080 <memset@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
test   %ebx,%ebx
js     124a <i2c_detect+0xaa>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1080 <memset@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
mov    $0x32,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1090 <ioctl@plt>
test   %rax,%rax
je     1265 <i2c_detect+0xc5>
lea    0xe15(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1e,%eax
jmp    133b <i2c_detect+0x19b>
lea    0xdaf(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x14,%eax
jmp    133b <i2c_detect+0x19b>
lea    0xdb9(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
mov    0x2d4b(%rip),%r15
lea    0x10(%rsp),%r12
lea    0x20(%rsp),%r13
jmp    1298 <i2c_detect+0xf8>
nopl   0x0(%rax)
add    $0x1,%ebp
cmp    $0x7f,%ebp
je     1316 <i2c_detect+0x176>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10c0 <add_i2c_read_data@plt>
mov    %r14,0x18(%rsp)
movl   $0x1,0x10(%rsp)
lea    0xd61(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
mov    (%r15),%esi
mov    %ebx,%edi
mov    %r12,%rdx
xor    %eax,%eax
call   1090 <ioctl@plt>
test   %rax,%rax
js     1290 <i2c_detect+0xf0>
mov    %r13,%rdi
mov    $0x80,%esi
lea    0xd28(%rip),%rdx
mov    %ebp,%ecx
call   1060 <snprintf@plt>
lea    0xa0(%rsp),%rdi
mov    %r13,%rsi
mov    $0x200,%edx
call   10a0 <strncat@plt>
jmp    1290 <i2c_detect+0xf0>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xd0d(%rip),%rdi
call   1040 <puts@plt>
lea    0xa0(%rsp),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x2a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.i2c_msg = type { i32 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@.str = [28 x i8] c"Can not open i2c device %s\0A\00"
@I2C_TIMEOUT = global i32 0
@.str.1 = [21 x i8] c"Can not set timeout\0A\00"
@.str.2 = [16 x i8] c"Check address:\0A\00"
@.str.3 = [9 x i8] c"\0D0x%02X \00"
@stdout = global i32 0
@I2C_RDWR = global i32 0
@.str.4 = [8 x i8] c"0x%02X \00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [29 x i8] c"Found devices at addresses:\0A\00"
@.str.7 = [4 x i8] c"%s\0A\00"
define i32 @i2c_detect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.i2c_msg
%8 = alloca [512 x i8]
%9 = alloca [128 x i8]
%10 = alloca %struct.i2c_rdwr_ioctl_data
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i32 @open_i2c_device(i8* %11)
store i32 %12, i32* %4
store i64 50, i64* %6
%13 = bitcast [512 x i8]* %8 to i8*
%14 = bitcast [128 x i8]* %9 to i8*
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 20, i32* %2
br label %63
20: ; preds = %1
%21 = call i32 @memset(%struct.i2c_msg* %7, i32 0, i32 4)
%22 = load i32, i32* %4
%23 = load i32, i32* @I2C_TIMEOUT
%24 = load i64, i64* %6
%25 = call i64 (i32, i32, ...) @ioctl(i32 %22, i32 %23, i64 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 30, i32* %2
br label %63
29: ; preds = %20
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %5
br label %31
31: ; preds = %55, %29
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 127
br i1 %33, label %34, label %58
34: ; preds = %31
%35 = load i32, i32* %5
%36 = call i32 @add_i2c_read_data(i32 %35, %struct.i2c_msg* %7, i32 1)
%37 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %10, i32 0, i32 1
store %struct.i2c_msg* %7, %struct.i2c_msg** %37
%38 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %10, i32 0, i32 0
store i32 1, i32* %38
%39 = load i32, i32* %5
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = load i32, i32* @stdout
%42 = call i32 @fflush(i32 %41)
%43 = load i32, i32* %4
%44 = load i32, i32* @I2C_RDWR
%45 = call i64 (i32, i32, ...) @ioctl(i32 %43, i32 %44, %struct.i2c_rdwr_ioctl_data* %10)
%46 = icmp sge i64 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %34
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* %5
%50 = call i32 @snprintf(i8* %48, i32 128, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%52 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%53 = call i32 @strncat(i8* %51, i8* %52, i32 512)
br label %54
54: ; preds = %47, %34
br label %55
55: ; preds = %54
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 1
store i32 %57, i32* %5
br label %31
58: ; preds = %31
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %61)
store i32 0, i32* %2
br label %63
63: ; preds = %58, %27, %17
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
%struct.i2c_msg = type { i32 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@.str = [28 x i8] c"Can not open i2c device %s\0A\00"
@I2C_TIMEOUT = global i32 0
@.str.3 = [9 x i8] c"\0D0x%02X \00"
@stdout = global i32 0
@I2C_RDWR = global i32 0
@.str.4 = [8 x i8] c"0x%02X \00"
@str = [15 x i8] c"Check address:\00"
@str.8 = [28 x i8] c"Found devices at addresses:\00"
@str.9 = [20 x i8] c"Can not set timeout\00"
define i32 @i2c_detect(i8* %0) {
%2 = alloca %struct.i2c_msg
%3 = alloca [512 x i8]
%4 = alloca [128 x i8]
%5 = alloca %struct.i2c_rdwr_ioctl_data
%6 = tail call i32 @open_i2c_device(i8* %0)
%7 = bitcast %struct.i2c_msg* %2 to i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%10 = icmp slt i32 %6, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %44
13: ; preds = %1
%14 = call i32 @memset(%struct.i2c_msg* %2, i32 0, i32 4)
%15 = load i32, i32* @I2C_TIMEOUT
%16 = call i64 (i32, i32, ...) @ioctl(i32 %6, i32 %15, i64 50)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
br label %44
20: ; preds = %13
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%22 = bitcast %struct.i2c_rdwr_ioctl_data* %5 to i8*
%23 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %5, i64 0, i32 1
%24 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %5, i64 0, i32 0
br label %25
25: ; preds = %20, %37
%26 = phi i32 [ 1, %20 ], [ %38, %37 ]
%27 = call i32 @add_i2c_read_data(i32 %26, %struct.i2c_msg* %2, i32 1)
store %struct.i2c_msg* %2, %struct.i2c_msg** %23
store i32 1, i32* %24
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%29 = load i32, i32* @stdout
%30 = call i32 @fflush(i32 %29)
%31 = load i32, i32* @I2C_RDWR
%32 = call i64 (i32, i32, ...) @ioctl(i32 %6, i32 %31, %struct.i2c_rdwr_ioctl_data* %5)
%33 = icmp sgt i64 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = call i32 @snprintf(i8* %9, i32 128, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%36 = call i32 @strncat(i8* %8, i8* %9, i32 512)
br label %37
37: ; preds = %34, %25
%38 = add nuw nsw i32 %26, 1
%39 = icmp eq i32 %38, 127
br i1 %39, label %40, label %25
40: ; preds = %37
%41 = call i32 @putchar(i32 10)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.8, i64 0, i64 0))
%43 = call i32 @puts(i8* %8)
br label %44
44: ; preds = %40, %18, %11
%45 = phi i32 [ 20, %11 ], [ 30, %18 ], [ 0, %40 ]
ret i32 %45
}