#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 */
union i2c_smbus_data {
  int *block;
};
typedef int __u8;
typedef int __s32;

/* Variables and functions */
int I2C_SMBUS_BLOCK_MAX;
int /*<<< orphan*/ I2C_SMBUS_I2C_BLOCK_DATA;
int /*<<< orphan*/ I2C_SMBUS_READ;
int i2c_smbus_access(int, int /*<<< orphan*/, int, int /*<<< orphan*/,
                     union i2c_smbus_data *);
__s32 i2c_smbus_read_i2c_block_data(int file, __u8 command, __u8 length,
                                    __u8 *values) {
  union i2c_smbus_data data;
  int i, err;
  if (length > I2C_SMBUS_BLOCK_MAX)
    length = I2C_SMBUS_BLOCK_MAX;
  data.block[0] = length;
  err = i2c_smbus_access(file, I2C_SMBUS_READ, command,
                         length == 32 ? I2C_SMBUS_I2C_BLOCK_DATA
                                      : I2C_SMBUS_I2C_BLOCK_DATA,
                         &data);
  if (err < 0)
    return err;
  for (i = 1; i <= data.block[0]; i++)
    values[i - 1] = data.block[i];
  return data.block[0];
}


x86 assembly (clang, -O0):
<i2c_smbus_read_i2c_block_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2ea9(%rip),%rcx
cmp    (%rcx),%eax
jle    1143 <i2c_smbus_read_i2c_block_data+0x33>
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x20,-0x10(%rbp)
jne    117f <i2c_smbus_read_i2c_block_data+0x6f>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    118b <i2c_smbus_read_i2c_block_data+0x7b>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%esi
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%ecx
lea    -0x20(%rbp),%r8
call   1030 <i2c_smbus_access@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11b8 <i2c_smbus_read_i2c_block_data+0xa8>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1200 <i2c_smbus_read_i2c_block_data+0xf0>
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jg     11f7 <i2c_smbus_read_i2c_block_data+0xe7>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11bf <i2c_smbus_read_i2c_block_data+0xaf>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
LLVM IR (-O0):
%union.i2c_smbus_data = type { i32* }
@I2C_SMBUS_BLOCK_MAX = global i32 0
@I2C_SMBUS_READ = global i32 0
@I2C_SMBUS_I2C_BLOCK_DATA = global i32 0
define i32 @i2c_smbus_read_i2c_block_data(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %union.i2c_smbus_data
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%13 = load i32, i32* %8
%14 = load i32, i32* @I2C_SMBUS_BLOCK_MAX
%15 = icmp sgt i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = load i32, i32* @I2C_SMBUS_BLOCK_MAX
store i32 %17, i32* %8
br label %18
18: ; preds = %16, %4
%19 = load i32, i32* %8
%20 = bitcast %union.i2c_smbus_data* %10 to i32**
%21 = load i32*, i32** %20
%22 = getelementptr inbounds i32, i32* %21, i64 0
store i32 %19, i32* %22
%23 = load i32, i32* %6
%24 = load i32, i32* @I2C_SMBUS_READ
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, 32
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = load i32, i32* @I2C_SMBUS_I2C_BLOCK_DATA
br label %32
30: ; preds = %18
%31 = load i32, i32* @I2C_SMBUS_I2C_BLOCK_DATA
br label %32
32: ; preds = %30, %28
%33 = phi i32 [ %29, %28 ], [ %31, %30 ]
%34 = call i32 @i2c_smbus_access(i32 %23, i32 %24, i32 %25, i32 %33, %union.i2c_smbus_data* %10)
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = load i32, i32* %12
store i32 %38, i32* %5
br label %67
39: ; preds = %32
store i32 1, i32* %11
br label %40
40: ; preds = %59, %39
%41 = load i32, i32* %11
%42 = bitcast %union.i2c_smbus_data* %10 to i32**
%43 = load i32*, i32** %42
%44 = getelementptr inbounds i32, i32* %43, i64 0
%45 = load i32, i32* %44
%46 = icmp sle i32 %41, %45
br i1 %46, label %47, label %62
47: ; preds = %40
%48 = bitcast %union.i2c_smbus_data* %10 to i32**
%49 = load i32*, i32** %48
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = load i32*, i32** %9
%55 = load i32, i32* %11
%56 = sub nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %54, i64 %57
store i32 %53, i32* %58
br label %59
59: ; preds = %47
%60 = load i32, i32* %11
%61 = add nsw i32 %60, 1
store i32 %61, i32* %11
br label %40
62: ; preds = %40
%63 = bitcast %union.i2c_smbus_data* %10 to i32**
%64 = load i32*, i32** %63
%65 = getelementptr inbounds i32, i32* %64, i64 0
%66 = load i32, i32* %65
store i32 %66, i32* %5
br label %67
67: ; preds = %62, %37
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):