#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 {
  int len;
  unsigned int addr;
  unsigned char *buf;
  int /*<<< orphan*/ flags;
};

/* Variables and functions */
int /*<<< orphan*/ I2C_M_RD;
int /*<<< orphan*/ I2C_RDWR;
int /*<<< orphan*/ I2C_RETRIES;
int /*<<< orphan*/ I2C_TIMEOUT;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(struct i2c_msg *);
int ioctl(int, int /*<<< orphan*/, unsigned long);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int i2c_read_reg(char *dev, unsigned char *buf, unsigned slave_address,
                 unsigned reg_address, int len) {
  struct i2c_rdwr_ioctl_data work_queue;
  unsigned char w_val = reg_address;
  int ret;
  int fd = open(dev, O_RDWR);
  if (!fd) {
    printf("Error on opening the device file\n");
    return 0;
  }
  work_queue.nmsgs = 2;
  work_queue.msgs =
      (struct i2c_msg *)malloc(work_queue.nmsgs * sizeof(struct i2c_msg));
  if (!work_queue.msgs) {
    printf("Memory alloc error\n");
    close(fd);
    return 0;
  }
  ioctl(fd, I2C_TIMEOUT, 2);
  ioctl(fd, I2C_RETRIES, 1);
  (work_queue.msgs[0]).len = 1;
  (work_queue.msgs[0]).addr = slave_address;
  (work_queue.msgs[0]).buf = &w_val;
  (work_queue.msgs[1]).len = len;
  (work_queue.msgs[1]).flags = I2C_M_RD;
  (work_queue.msgs[1]).addr = slave_address;
  (work_queue.msgs[1]).buf = buf;
  ret = ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);
  if (ret < 0) {
    close(fd);
    free(work_queue.msgs);
    return 0;
  } else {
    close(fd);
    free(work_queue.msgs);
    return len;
  }
}


x86 assembly (clang, -O0):
<i2c_read_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %al,-0x39(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    11b7 <i2c_read_reg+0x57>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ce <i2c_read_reg+0x16e>
movl   $0x2,-0x38(%rbp)
movslq -0x38(%rbp),%rax
imul   $0x18,%rax,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11fc <i2c_read_reg+0x9c>
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ce <i2c_read_reg+0x16e>
mov    -0x44(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edx
call   1050 <ioctl@plt>
mov    -0x44(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1050 <ioctl@plt>
mov    -0x30(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%rax
lea    -0x39(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    0x2d6f(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x28(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x1c(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x20(%rax)
mov    -0x44(%rbp),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    12b7 <i2c_read_reg+0x157>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ce <i2c_read_reg+0x16e>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<i2c_read_reg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %r8d,%r14d
mov    %edx,%r12d
mov    %rsi,%r15
mov    %cl,0xf(%rsp)
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
je     123b <i2c_read_reg+0xdb>
mov    %eax,%ebp
movl   $0x2,0x10(%rsp)
mov    $0x30,%edi
call   1070 <malloc@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     1249 <i2c_read_reg+0xe9>
mov    %rax,%rbx
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
mov    %ebp,%edi
call   1050 <ioctl@plt>
movl   $0x1,(%rbx)
mov    %r12d,0x4(%rbx)
lea    0xf(%rsp),%rax
mov    %rax,0x8(%rbx)
mov    %r14d,0x18(%rbx)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rbx)
mov    %r12d,0x1c(%rbx)
mov    %r15,0x20(%rbx)
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
test   %ebx,%ebx
cmovns %r14d,%eax
jmp    125e <i2c_read_reg+0xfe>
lea    0xdbe(%rip),%rdi
call   1040 <puts@plt>
jmp    125c <i2c_read_reg+0xfc>
lea    0xdd1(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, i32 }
@O_RDWR = global i32 0
@.str = [34 x i8] c"Error on opening the device file\0A\00"
@.str.1 = [20 x i8] c"Memory alloc error\0A\00"
@I2C_TIMEOUT = global i32 0
@I2C_RETRIES = global i32 0
@I2C_M_RD = global i32 0
@I2C_RDWR = global i32 0
define i32 @i2c_read_reg(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.i2c_rdwr_ioctl_data
%13 = alloca i8
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%16 = load i32, i32* %10
%17 = trunc i32 %16 to i8
store i8 %17, i8* %13
%18 = load i8*, i8** %7
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %15
%21 = load i32, i32* %15
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %5
%24 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %101
25: ; preds = %5
%26 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 0
store i32 2, i32* %26
%27 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 0
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 24
%31 = trunc i64 %30 to i32
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to %struct.i2c_msg*
%34 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
store %struct.i2c_msg* %33, %struct.i2c_msg** %34
%35 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%36 = load %struct.i2c_msg*, %struct.i2c_msg** %35
%37 = icmp ne %struct.i2c_msg* %36, null
br i1 %37, label %42, label %38
38: ; preds = %25
%39 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* %15
%41 = call i32 @close(i32 %40)
store i32 0, i32* %6
br label %101
42: ; preds = %25
%43 = load i32, i32* %15
%44 = load i32, i32* @I2C_TIMEOUT
%45 = call i32 @ioctl(i32 %43, i32 %44, i64 2)
%46 = load i32, i32* %15
%47 = load i32, i32* @I2C_RETRIES
%48 = call i32 @ioctl(i32 %46, i32 %47, i64 1)
%49 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%50 = load %struct.i2c_msg*, %struct.i2c_msg** %49
%51 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %50, i64 0
%52 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %51, i32 0, i32 0
store i32 1, i32* %52
%53 = load i32, i32* %9
%54 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%55 = load %struct.i2c_msg*, %struct.i2c_msg** %54
%56 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %55, i64 0
%57 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %56, i32 0, i32 1
store i32 %53, i32* %57
%58 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%59 = load %struct.i2c_msg*, %struct.i2c_msg** %58
%60 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %59, i64 0
%61 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %60, i32 0, i32 2
store i8* %13, i8** %61
%62 = load i32, i32* %11
%63 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%64 = load %struct.i2c_msg*, %struct.i2c_msg** %63
%65 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %64, i64 1
%66 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %65, i32 0, i32 0
store i32 %62, i32* %66
%67 = load i32, i32* @I2C_M_RD
%68 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%69 = load %struct.i2c_msg*, %struct.i2c_msg** %68
%70 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %69, i64 1
%71 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %70, i32 0, i32 3
store i32 %67, i32* %71
%72 = load i32, i32* %9
%73 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%74 = load %struct.i2c_msg*, %struct.i2c_msg** %73
%75 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %74, i64 1
%76 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %75, i32 0, i32 1
store i32 %72, i32* %76
%77 = load i8*, i8** %8
%78 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%79 = load %struct.i2c_msg*, %struct.i2c_msg** %78
%80 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %79, i64 1
%81 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %80, i32 0, i32 2
store i8* %77, i8** %81
%82 = load i32, i32* %15
%83 = load i32, i32* @I2C_RDWR
%84 = ptrtoint %struct.i2c_rdwr_ioctl_data* %12 to i64
%85 = call i32 @ioctl(i32 %82, i32 %83, i64 %84)
store i32 %85, i32* %14
%86 = load i32, i32* %14
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %94
88: ; preds = %42
%89 = load i32, i32* %15
%90 = call i32 @close(i32 %89)
%91 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%92 = load %struct.i2c_msg*, %struct.i2c_msg** %91
%93 = call i32 @free(%struct.i2c_msg* %92)
store i32 0, i32* %6
br label %101
94: ; preds = %42
%95 = load i32, i32* %15
%96 = call i32 @close(i32 %95)
%97 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %12, i32 0, i32 1
%98 = load %struct.i2c_msg*, %struct.i2c_msg** %97
%99 = call i32 @free(%struct.i2c_msg* %98)
%100 = load i32, i32* %11
store i32 %100, i32* %6
br label %101
101: ; preds = %94, %88, %38, %23
%102 = load i32, i32* %6
ret i32 %102
}

LLVM IR (-O2):
%struct.i2c_rdwr_ioctl_data = type { i32, %struct.i2c_msg* }
%struct.i2c_msg = type { i32, i32, i8*, i32 }
@O_RDWR = global i32 0
@I2C_TIMEOUT = global i32 0
@I2C_RETRIES = global i32 0
@I2C_M_RD = global i32 0
@I2C_RDWR = global i32 0
@str = [33 x i8] c"Error on opening the device file\00"
@str.2 = [19 x i8] c"Memory alloc error\00"
define i32 @i2c_read_reg(i8* nocapture %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.i2c_rdwr_ioctl_data
%7 = alloca i8
%8 = bitcast %struct.i2c_rdwr_ioctl_data* %6 to i8*
%9 = trunc i32 %3 to i8
store i8 %9, i8* %7
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* %0, i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %5
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %45
15: ; preds = %5
%16 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 0
store i32 2, i32* %16
%17 = tail call i64 @malloc(i32 48)
%18 = inttoptr i64 %17 to %struct.i2c_msg*
%19 = getelementptr inbounds %struct.i2c_rdwr_ioctl_data, %struct.i2c_rdwr_ioctl_data* %6, i64 0, i32 1
store %struct.i2c_msg* %18, %struct.i2c_msg** %19
%20 = icmp eq i64 %17, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.2, i64 0, i64 0))
%23 = tail call i32 @close(i32 %11)
br label %45
24: ; preds = %15
%25 = load i32, i32* @I2C_TIMEOUT
%26 = tail call i32 @ioctl(i32 %11, i32 %25, i64 2)
%27 = load i32, i32* @I2C_RETRIES
%28 = tail call i32 @ioctl(i32 %11, i32 %27, i64 1)
%29 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i64 0, i32 0
store i32 1, i32* %29
%30 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i64 0, i32 1
store i32 %2, i32* %30
%31 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i64 0, i32 2
store i8* %7, i8** %31
%32 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i64 1, i32 0
store i32 %4, i32* %32
%33 = load i32, i32* @I2C_M_RD
%34 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i64 1, i32 3
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i64 1, i32 1
store i32 %2, i32* %35
%36 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %18, i64 1, i32 2
store i8* %1, i8** %36
%37 = load i32, i32* @I2C_RDWR
%38 = ptrtoint %struct.i2c_rdwr_ioctl_data* %6 to i64
%39 = call i32 @ioctl(i32 %11, i32 %37, i64 %38)
%40 = icmp slt i32 %39, 0
%41 = call i32 @close(i32 %11)
%42 = load %struct.i2c_msg*, %struct.i2c_msg** %19
%43 = call i32 @free(%struct.i2c_msg* %42)
%44 = select i1 %40, i32 0, i32 %4
br label %45
45: ; preds = %24, %21, %13
%46 = phi i32 [ 0, %21 ], [ 0, %13 ], [ %44, %24 ]
ret i32 %46
}