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

/* Variables and functions */
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ add_i2c_read_data(int, struct i2c_msg *, int);
scalar_t__ add_i2c_write_data(int, struct i2c_msg *, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(scalar_t__);
int getopt(int, char **, char *);
int i2c_detect(char *);
int ioctl(int, int /*<<< orphan*/, struct i2c_rdwr_ioctl_data *);
int /*<<< orphan*/ memset(struct i2c_msg *, int /*<<< orphan*/, int);
int open_i2c_device(char *);
char *optarg;
scalar_t__ opterr;
int /*<<< orphan*/ print_received_data(int, struct i2c_msg *);
int /*<<< orphan*/ printf(char *, ...);
int strtol(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  int i2c_address = -1;
  int number_of_data_to_read = 0;
  char *data_to_write = 0;
  char *i2c_character_device = 0;
  int detect = 0;
  int c;
  opterr = 0;
  while ((c = getopt(argc, argv, "a:d:w:r:ch")) != -1) {
    switch (c) {
    case 'a':
      i2c_address = strtol(optarg, 0, 0);
      break;
    case 'd':
      i2c_character_device = optarg;
      break;
    case 'w':
      data_to_write = optarg;
      break;
    case 'r':
      number_of_data_to_read = strtol(optarg, 0, 0);
      break;
    case 'c':
      detect = 1;
      break;
    case 'h':
      usage();
      return 1;
    default:
      break;
    }
  }
  if (i2c_character_device == 0) {
    printf("Please specify a device\n");
    usage();
    return 3;
  }
  if (detect) {
    return i2c_detect(i2c_character_device);
  }
  if (i2c_address == -1) {
    printf("Please set a device address\n");
    usage();
    return 4;
  }
  if (number_of_data_to_read == 0 && data_to_write == 0) {
    printf("Please specify at least one transmission\n");
    usage();
    return 5;
  }
  int i2c_character_device_fd = open_i2c_device(i2c_character_device);
  if (i2c_character_device_fd <= 0) {
    printf("Can not open %s", i2c_character_device);
    return 6;
  }
  struct i2c_msg rdwr_msg[2];
  int msg_count = 0;
  memset(rdwr_msg, 0, sizeof(rdwr_msg));
  if (data_to_write != 0) {
    if (add_i2c_write_data(i2c_address, &rdwr_msg[msg_count], data_to_write) !=
        0) {
      goto cleanup;
    }
    msg_count++;
  }
  if (number_of_data_to_read != 0) {
    add_i2c_read_data(i2c_address, &rdwr_msg[msg_count],
                      number_of_data_to_read);
    msg_count++;
  }
  struct i2c_rdwr_ioctl_data rdwr_set;
  rdwr_set.msgs = rdwr_msg;
  rdwr_set.nmsgs = msg_count;
  if (ioctl(i2c_character_device_fd, I2C_RDWR, &rdwr_set) == -1) {
    printf("Read write transaction failed (bus %s, addr 0x%X)\n",
           i2c_character_device, i2c_address);
    goto cleanup;
  }
  if (number_of_data_to_read != 0) {
    print_received_data(i2c_address, &rdwr_msg[msg_count - 1]);
  }
cleanup:
  for (int i = 0; i < msg_count; i++) {
    free(rdwr_msg[i].buf);
    rdwr_msg[i].buf = 0;
  }
  close(i2c_character_device_fd);
  i2c_character_device_fd = -1;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2dc3(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe32(%rip),%rdx
call   10e0 <getopt@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
je     12e9 <main+0x119>
mov    -0x30(%rbp),%eax
add    $0xffffff9f,%eax
mov    %eax,%ecx
mov    %rcx,-0x78(%rbp)
sub    $0x16,%eax
ja     12df <main+0x10f>
mov    -0x78(%rbp),%rax
lea    0xda5(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2d8d(%rip),%rax
mov    (%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10b0 <strtol@plt>
mov    %eax,-0x14(%rbp)
jmp    12e4 <main+0x114>
mov    0x2d72(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    12e4 <main+0x114>
mov    0x2d5f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    12e4 <main+0x114>
mov    0x2d4c(%rip),%rax
mov    (%rax),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   10b0 <strtol@plt>
mov    %eax,-0x18(%rbp)
jmp    12e4 <main+0x114>
movl   $0x1,-0x2c(%rbp)
jmp    12e4 <main+0x114>
mov    $0x0,%al
call   10d0 <usage@plt>
movl   $0x1,-0x4(%rbp)
jmp    1509 <main+0x339>
jmp    12e4 <main+0x114>
jmp    121c <main+0x4c>
cmpq   $0x0,-0x28(%rbp)
jne    1315 <main+0x145>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10d0 <usage@plt>
movl   $0x3,-0x4(%rbp)
jmp    1509 <main+0x339>
cmpl   $0x0,-0x2c(%rbp)
je     1330 <main+0x160>
mov    -0x28(%rbp),%rdi
call   10c0 <i2c_detect@plt>
mov    %eax,-0x4(%rbp)
jmp    1509 <main+0x339>
cmpl   $0xffffffff,-0x14(%rbp)
jne    135b <main+0x18b>
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10d0 <usage@plt>
movl   $0x4,-0x4(%rbp)
jmp    1509 <main+0x339>
cmpl   $0x0,-0x18(%rbp)
jne    1391 <main+0x1c1>
cmpq   $0x0,-0x20(%rbp)
jne    1391 <main+0x1c1>
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10d0 <usage@plt>
movl   $0x5,-0x4(%rbp)
jmp    1509 <main+0x339>
mov    -0x28(%rbp),%rdi
call   1060 <open_i2c_device@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     13c5 <main+0x1f5>
mov    -0x28(%rbp),%rsi
lea    0xd15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x6,-0x4(%rbp)
jmp    1509 <main+0x339>
movl   $0x0,-0x54(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
je     141a <main+0x24a>
mov    -0x14(%rbp),%edi
movslq -0x54(%rbp),%rax
lea    -0x50(%rbp),%rsi
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <add_i2c_write_data@plt>
cmp    $0x0,%rax
je     1411 <main+0x241>
jmp    14b7 <main+0x2e7>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1447 <main+0x277>
mov    -0x14(%rbp),%edi
movslq -0x54(%rbp),%rax
lea    -0x50(%rbp),%rsi
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
call   10f0 <add_i2c_read_data@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2b69(%rip),%rax
mov    (%rax),%esi
lea    -0x68(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    148d <main+0x2bd>
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xc56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    14b7 <main+0x2e7>
cmpl   $0x0,-0x18(%rbp)
je     14b2 <main+0x2e2>
mov    -0x14(%rbp),%edi
mov    -0x54(%rbp),%eax
sub    $0x1,%eax
cltq
lea    -0x50(%rbp),%rsi
shl    $0x3,%rax
add    %rax,%rsi
call   1040 <print_received_data@plt>
jmp    14b7 <main+0x2e7>
movl   $0x0,-0x6c(%rbp)
mov    -0x6c(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    14f3 <main+0x323>
movslq -0x6c(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
call   1030 <free@plt>
movslq -0x6c(%rbp),%rax
movq   $0x0,-0x50(%rbp,%rax,8)
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
jmp    14be <main+0x2ee>
mov    -0x34(%rbp),%edi
call   1090 <close@plt>
movl   $0xffffffff,-0x34(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2dde(%rip),%rax
movq   $0x0,(%rax)
xor    %eax,%eax
mov    %rax,0x18(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe45(%rip),%r14
lea    0xde2(%rip),%r15
xor    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %r13d,%r13d
xor    %r12d,%r12d
jmp    1239 <main+0x59>
nopl   (%rax)
cmp    $0xffffffff,%eax
je     12ca <main+0xea>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10f0 <getopt@plt>
lea    -0x61(%rax),%ecx
cmp    $0x16,%ecx
ja     1230 <main+0x50>
movslq (%r15,%rcx,4),%rax
add    %r15,%rax
jmp    *%rax
mov    0x2d9a(%rip),%rax
mov    (%rax),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtol@plt>
mov    %eax,0xc(%rsp)
jmp    1239 <main+0x59>
mov    $0x1,%r12d
jmp    1239 <main+0x59>
mov    0x2d79(%rip),%rax
mov    (%rax),%r13
jmp    1239 <main+0x59>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <strtol@plt>
mov    %rax,0x18(%rsp)
jmp    1239 <main+0x59>
mov    0x2d53(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
jmp    1239 <main+0x59>
xor    %eax,%eax
call   10e0 <usage@plt>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r13,%r13
je     12ed <main+0x10d>
mov    %r13,%rbp
test   %r12d,%r12d
je     1307 <main+0x127>
mov    %rbp,%rdi
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <i2c_detect@plt>
lea    0xdfb(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   10e0 <usage@plt>
mov    $0x3,%eax
jmp    12bb <main+0xdb>
mov    0xc(%rsp),%r13d
cmp    $0xffffffff,%r13d
je     1342 <main+0x162>
mov    0x18(%rsp),%rbx
test   %ebx,%ebx
jne    135f <main+0x17f>
mov    0x10(%rsp),%r14
test   %r14,%r14
jne    1364 <main+0x184>
lea    0xd7e(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   10e0 <usage@plt>
mov    $0x5,%eax
jmp    12bb <main+0xdb>
lea    0xd8a(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   10e0 <usage@plt>
mov    $0x4,%eax
jmp    12bb <main+0xdb>
mov    0x10(%rsp),%r14
mov    %rbp,%rdi
call   1070 <open_i2c_device@plt>
test   %eax,%eax
jle    1412 <main+0x232>
mov    %eax,%r15d
lea    0x20(%rsp),%rdi
xor    %r12d,%r12d
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
test   %r14,%r14
je     13af <main+0x1cf>
lea    0x20(%rsp),%rsi
mov    %r13d,%edi
mov    %r14,%rdx
call   10b0 <add_i2c_write_data@plt>
test   %rax,%rax
jne    1464 <main+0x284>
mov    $0x1,%r12d
test   %ebx,%ebx
je     13cc <main+0x1ec>
mov    %r12d,%eax
lea    (%rsp,%rax,8),%rsi
add    $0x20,%rsi
mov    %r13d,%edi
mov    %ebx,%edx
call   1100 <add_i2c_read_data@plt>
add    $0x1,%r12d
lea    0x20(%rsp),%rax
mov    %rax,0x40(%rsp)
mov    %r12d,0x38(%rsp)
mov    0x2be6(%rip),%rax
mov    (%rax),%esi
lea    0x38(%rsp),%rdx
mov    %r15d,%edi
call   1090 <ioctl@plt>
mov    %r12d,%ebx
cmp    $0xffffffff,%eax
je     142d <main+0x24d>
cmpl   $0x0,0x18(%rsp)
je     1441 <main+0x261>
lea    (%rsp,%rbx,8),%rsi
add    $0x18,%rsi
mov    %r13d,%edi
call   1040 <print_received_data@plt>
jmp    1441 <main+0x261>
lea    0xc4e(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x6,%eax
jmp    12bb <main+0xdb>
lea    0xc43(%rip),%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
test   %r12d,%r12d
je     1464 <main+0x284>
xor    %ebp,%ebp
mov    0x20(%rsp,%rbp,8),%rdi
call   1030 <free@plt>
movq   $0x0,0x20(%rsp,%rbp,8)
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1448 <main+0x268>
mov    %r15d,%edi
call   10a0 <close@plt>
xor    %eax,%eax
jmp    12bb <main+0xdb>

LLVM IR (-O0):
%struct.i2c_msg = type { i64 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@opterr = global i64 0
@.str = [11 x i8] c"a:d:w:r:ch\00"
@optarg = global i8* null
@.str.1 = [25 x i8] c"Please specify a device\0A\00"
@.str.2 = [29 x i8] c"Please set a device address\0A\00"
@.str.3 = [42 x i8] c"Please specify at least one transmission\0A\00"
@.str.4 = [16 x i8] c"Can not open %s\00"
@I2C_RDWR = global i32 0
@.str.5 = [51 x i8] c"Read write transaction failed (bus %s, addr 0x%X)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [2 x %struct.i2c_msg]
%14 = alloca i32
%15 = alloca %struct.i2c_rdwr_ioctl_data
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 0, i32* %7
store i8* null, i8** %8
store i8* null, i8** %9
store i32 0, i32* %10
store i64 0, i64* @opterr
br label %17
17: ; preds = %38, %2
%18 = load i32, i32* %4
%19 = load i8**, i8*** %5
%20 = call i32 @getopt(i32 %18, i8** %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 %20, i32* %11
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %39
22: ; preds = %17
%23 = load i32, i32* %11
switch i32 %23, label %37 [
i32 97, label %24
i32 100, label %27
i32 119, label %29
i32 114, label %31
i32 99, label %34
i32 104, label %35
]
24: ; preds = %22
%25 = load i8*, i8** @optarg
%26 = call i32 @strtol(i8* %25, i32 0, i32 0)
store i32 %26, i32* %6
br label %38
27: ; preds = %22
%28 = load i8*, i8** @optarg
store i8* %28, i8** %9
br label %38
29: ; preds = %22
%30 = load i8*, i8** @optarg
store i8* %30, i8** %8
br label %38
31: ; preds = %22
%32 = load i8*, i8** @optarg
%33 = call i32 @strtol(i8* %32, i32 0, i32 0)
store i32 %33, i32* %7
br label %38
34: ; preds = %22
store i32 1, i32* %10
br label %38
35: ; preds = %22
%36 = call i32 (...) @usage()
store i32 1, i32* %3
br label %149
37: ; preds = %22
br label %38
38: ; preds = %37, %34, %31, %29, %27, %24
br label %17
39: ; preds = %17
%40 = load i8*, i8** %9
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 (...) @usage()
store i32 3, i32* %3
br label %149
45: ; preds = %39
%46 = load i32, i32* %10
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i8*, i8** %9
%50 = call i32 @i2c_detect(i8* %49)
store i32 %50, i32* %3
br label %149
51: ; preds = %45
%52 = load i32, i32* %6
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 (...) @usage()
store i32 4, i32* %3
br label %149
57: ; preds = %51
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %57
%61 = load i8*, i8** %8
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%65 = call i32 (...) @usage()
store i32 5, i32* %3
br label %149
66: ; preds = %60, %57
%67 = load i8*, i8** %9
%68 = call i32 @open_i2c_device(i8* %67)
store i32 %68, i32* %12
%69 = load i32, i32* %12
%70 = icmp sle i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %66
%72 = load i8*, i8** %9
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %72)
store i32 6, i32* %3
br label %149
74: ; preds = %66
store i32 0, i32* %14
%75 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 0
%76 = call i32 @memset(%struct.i2c_msg* %75, i32 0, i32 16)
%77 = load i8*, i8** %8
%78 = icmp ne i8* %77, null
br i1 %78, label %79, label %91
79: ; preds = %74
%80 = load i32, i32* %6
%81 = load i32, i32* %14
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 %82
%84 = load i8*, i8** %8
%85 = call i64 @add_i2c_write_data(i32 %80, %struct.i2c_msg* %83, i8* %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %79
br label %127
88: ; preds = %79
%89 = load i32, i32* %14
%90 = add nsw i32 %89, 1
store i32 %90, i32* %14
br label %91
91: ; preds = %88, %74
%92 = load i32, i32* %7
%93 = icmp ne i32 %92, 0
br i1 %93, label %94, label %103
94: ; preds = %91
%95 = load i32, i32* %6
%96 = load i32, i32* %14
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 %97
%99 = load i32, i32* %7
%100 = call i32 @add_i2c_read_data(i32 %95, %struct.i2c_msg* %98, i32 %99)
%101 = load i32, i32* %14
%102 = add nsw i32 %101, 1
store i32 %102, i32* %14
br label %103
103: ; preds = %94, %91
%104 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 0
%105 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %15, i32 0, i32 1
store %struct.i2c_msg* %104, %struct.i2c_msg** %105
%106 = load i32, i32* %14
%107 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %15, i32 0, i32 0
store i32 %106, i32* %107
%108 = load i32, i32* %12
%109 = load i32, i32* @I2C_RDWR
%110 = call i32 @ioctl(i32 %108, i32 %109, %struct.i2c_rdwr_ioctl_data* %15)
%111 = icmp eq i32 %110, -1
br i1 %111, label %112, label %116
112: ; preds = %103
%113 = load i8*, i8** %9
%114 = load i32, i32* %6
%115 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %113, i32 %114)
br label %127
116: ; preds = %103
%117 = load i32, i32* %7
%118 = icmp ne i32 %117, 0
br i1 %118, label %119, label %126
119: ; preds = %116
%120 = load i32, i32* %6
%121 = load i32, i32* %14
%122 = sub nsw i32 %121, 1
%123 = sext i32 %122 to i64
%124 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 %123
%125 = call i32 @print_received_data(i32 %120, %struct.i2c_msg* %124)
br label %126
126: ; preds = %119, %116
br label %127
127: ; preds = %126, %112, %87
store i32 0, i32* %16
br label %128
128: ; preds = %143, %127
%129 = load i32, i32* %16
%130 = load i32, i32* %14
%131 = icmp slt i32 %129, %130
br i1 %131, label %132, label %146
132: ; preds = %128
%133 = load i32, i32* %16
%134 = sext i32 %133 to i64
%135 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 %134
%136 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %135, i32 0, i32 0
%137 = load i64, i64* %136
%138 = call i32 @free(i64 %137)
%139 = load i32, i32* %16
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %13, i64 0, i64 %140
%142 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %141, i32 0, i32 0
store i64 0, i64* %142
br label %143
143: ; preds = %132
%144 = load i32, i32* %16
%145 = add nsw i32 %144, 1
store i32 %145, i32* %16
br label %128
146: ; preds = %128
%147 = load i32, i32* %12
%148 = call i32 @close(i32 %147)
store i32 -1, i32* %12
store i32 0, i32* %3
br label %149
149: ; preds = %146, %71, %63, %54, %48, %42, %35
%150 = load i32, i32* %3
ret i32 %150
}

LLVM IR (-O2):
%struct.i2c_msg = type { i64 }
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
@opterr = global i64 0
@.str = [11 x i8] c"a:d:w:r:ch\00"
@optarg = global i8* null
@.str.4 = [16 x i8] c"Can not open %s\00"
@I2C_RDWR = global i32 0
@.str.5 = [51 x i8] c"Read write transaction failed (bus %s, addr 0x%X)\0A\00"
@str = [41 x i8] c"Please specify at least one transmission\00"
@str.6 = [28 x i8] c"Please set a device address\00"
@str.7 = [24 x i8] c"Please specify a device\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [2 x %struct.i2c_msg]
%4 = alloca %struct.i2c_rdwr_ioctl_data
store i64 0, i64* @opterr
br label %5
5: ; preds = %27, %2
%6 = phi i32 [ %29, %27 ], [ -1, %2 ]
%7 = phi i32 [ %12, %27 ], [ 0, %2 ]
%8 = phi i8* [ %17, %27 ], [ null, %2 ]
%9 = phi i8* [ %21, %27 ], [ null, %2 ]
%10 = phi i32 [ %24, %27 ], [ 0, %2 ]
br label %11
11: ; preds = %5, %34
%12 = phi i32 [ %7, %5 ], [ %36, %34 ]
%13 = phi i8* [ %8, %5 ], [ %17, %34 ]
%14 = phi i8* [ %9, %5 ], [ %21, %34 ]
%15 = phi i32 [ %10, %5 ], [ %24, %34 ]
br label %16
16: ; preds = %11, %32
%17 = phi i8* [ %13, %11 ], [ %33, %32 ]
%18 = phi i8* [ %14, %11 ], [ %21, %32 ]
%19 = phi i32 [ %15, %11 ], [ %24, %32 ]
br label %20
20: ; preds = %16, %30
%21 = phi i8* [ %18, %16 ], [ %31, %30 ]
%22 = phi i32 [ %19, %16 ], [ %24, %30 ]
br label %23
23: ; preds = %25, %20
%24 = phi i32 [ %22, %20 ], [ 1, %25 ]
br label %25
25: ; preds = %23, %25
%26 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
switch i32 %26, label %25 [
i32 -1, label %39
i32 97, label %27
i32 100, label %30
i32 119, label %32
i32 114, label %34
i32 99, label %23
i32 104, label %37
]
27: ; preds = %25
%28 = load i8*, i8** @optarg
%29 = tail call i32 @strtol(i8* %28, i32 0, i32 0)
br label %5
30: ; preds = %25
%31 = load i8*, i8** @optarg
br label %20
32: ; preds = %25
%33 = load i8*, i8** @optarg
br label %16
34: ; preds = %25
%35 = load i8*, i8** @optarg
%36 = tail call i32 @strtol(i8* %35, i32 0, i32 0)
br label %11
37: ; preds = %25
%38 = tail call i32 (...) @usage()
br label %107
39: ; preds = %25
%40 = icmp eq i8* %21, null
br i1 %40, label %41, label %44
41: ; preds = %39
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
%43 = tail call i32 (...) @usage()
br label %107
44: ; preds = %39
%45 = icmp eq i32 %24, 0
br i1 %45, label %48, label %46
46: ; preds = %44
%47 = tail call i32 @i2c_detect(i8* %21)
br label %107
48: ; preds = %44
%49 = icmp eq i32 %6, -1
br i1 %49, label %50, label %53
50: ; preds = %48
%51 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
%52 = tail call i32 (...) @usage()
br label %107
53: ; preds = %48
%54 = icmp eq i32 %12, 0
%55 = icmp eq i8* %17, null
%56 = select i1 %54, i1 %55, i1 false
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%59 = tail call i32 (...) @usage()
br label %107
60: ; preds = %53
%61 = tail call i32 @open_i2c_device(i8* %21)
%62 = icmp slt i32 %61, 1
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %21)
br label %107
65: ; preds = %60
%66 = bitcast [2 x %struct.i2c_msg]* %3 to i8*
%67 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %3, i64 0, i64 0
%68 = call i32 @memset(%struct.i2c_msg* %67, i32 0, i32 16)
br i1 %55, label %72, label %69
69: ; preds = %65
%70 = call i64 @add_i2c_write_data(i32 %6, %struct.i2c_msg* %67, i8* %17)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %98
72: ; preds = %69, %65
%73 = phi i32 [ 0, %65 ], [ 1, %69 ]
br i1 %54, label %79, label %74
74: ; preds = %72
%75 = zext i32 %73 to i64
%76 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %3, i64 0, i64 %75
%77 = call i32 @add_i2c_read_data(i32 %6, %struct.i2c_msg* %76, i32 %12)
%78 = add nuw nsw i32 %73, 1
br label %79
79: ; preds = %74, %72
%80 = phi i32 [ %78, %74 ], [ %73, %72 ]
%81 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %4, i64 0, i32 1
store %struct.i2c_msg* %67, %struct.i2c_msg** %81
%82 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %4, i64 0, i32 0
store i32 %80, i32* %82
%83 = load i32, i32* @I2C_RDWR
%84 = call i32 @ioctl(i32 %61, i32 %83, %struct.i2c_rdwr_ioctl_data* %4)
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %88
86: ; preds = %79
%87 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %21, i32 %6)
br label %94
88: ; preds = %79
br i1 %54, label %94, label %89
89: ; preds = %88
%90 = add nsw i32 %80, -1
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %3, i64 0, i64 %91
%93 = call i32 @print_received_data(i32 %6, %struct.i2c_msg* %92)
br label %94
94: ; preds = %88, %89, %86
%95 = icmp eq i32 %80, 0
br i1 %95, label %98, label %96
96: ; preds = %94
%97 = zext i32 %80 to i64
br label %100
98: ; preds = %100, %69, %94
%99 = call i32 @close(i32 %61)
br label %107
100: ; preds = %96, %100
%101 = phi i64 [ 0, %96 ], [ %105, %100 ]
%102 = getelementptr inbounds [2 x %struct.i2c_msg], [2 x %struct.i2c_msg]* %3, i64 0, i64 %101, i32 0
%103 = load i64, i64* %102
%104 = call i32 @free(i64 %103)
store i64 0, i64* %102
%105 = add nuw nsw i64 %101, 1
%106 = icmp eq i64 %105, %97
br i1 %106, label %98, label %100
107: ; preds = %63, %98, %57, %50, %46, %41, %37
%108 = phi i32 [ 1, %37 ], [ 3, %41 ], [ %47, %46 ], [ 4, %50 ], [ 5, %57 ], [ 6, %63 ], [ 0, %98 ]
ret i32 %108
}