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

/* Variables and functions */
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *);
unsigned char *realloc(unsigned char *, int);
scalar_t__ strtol(char const *, char **, int /*<<< orphan*/);
int add_i2c_write_data(int i2c_address, struct i2c_msg *wr_msg,
                       const char *data_to_write) {
  wr_msg->addr = i2c_address;
  wr_msg->flags = 0;
  int i = 0;
  wr_msg->buf = malloc(sizeof(char) * 64);
  const char *parse_head = data_to_write;
  char *parse_tail;
  while (1) {
    if (i >= 64) {
      wr_msg->buf = realloc(wr_msg->buf, (i / 64 + 1) * 64 * sizeof(char));
    }
    if (wr_msg->buf == 0) {
      printf("Run out of memory\n");
      return -1;
    }
    wr_msg->buf[i] = (unsigned char)strtol(parse_head, &parse_tail, 0);
    i++;
    if (parse_tail == 0 || *parse_tail == 0) {
      break;
    }
    parse_head = parse_tail + 1;
  }
  wr_msg->len = i;
  return 0;
}


x86 assembly (clang, -O0):
<add_i2c_write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x18(%rax)
movl   $0x0,-0x1c(%rbp)
mov    $0x40,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x40,-0x1c(%rbp)
jl     11c7 <add_i2c_write_data+0x87>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1c(%rbp),%eax
mov    $0x40,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
shl    $0x6,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11ee <add_i2c_write_data+0xae>
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1259 <add_i2c_write_data+0x119>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1232 <add_i2c_write_data+0xf2>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1237 <add_i2c_write_data+0xf7>
jmp    1248 <add_i2c_write_data+0x108>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    118c <add_i2c_write_data+0x4c>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x10(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_i2c_write_data>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %edi,(%rsi)
movq   $0x0,0x18(%rsi)
mov    $0x40,%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%r15)
xor    %eax,%eax
mov    %rsp,%r14
nopl   0x0(%rax,%rax,1)
mov    %rax,%rbx
mov    0x8(%r15),%rax
cmp    $0x40,%rbx
jb     1194 <add_i2c_write_data+0x54>
mov    %ebx,%esi
and    $0x7fffffc0,%esi
add    $0x40,%esi
mov    %rax,%rdi
call   1060 <realloc@plt>
mov    %rax,0x8(%r15)
test   %rax,%rax
je     11ce <add_i2c_write_data+0x8e>
mov    %r12,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    0x8(%r15),%rcx
mov    %al,(%rcx,%rbx,1)
mov    (%rsp),%rcx
test   %rcx,%rcx
je     11c3 <add_i2c_write_data+0x83>
lea    0x1(%rcx),%r12
lea    0x1(%rbx),%rax
cmpb   $0x0,(%rcx)
jne    1170 <add_i2c_write_data+0x30>
add    $0x1,%ebx
mov    %ebx,0x10(%r15)
xor    %eax,%eax
jmp    11df <add_i2c_write_data+0x9f>
lea    0xe2b(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.i2c_msg = type { i32, i8*, i32, i64 }
@.str = [19 x i8] c"Run out of memory\0A\00"
define i32 @add_i2c_write_data(i32 %0, %struct.i2c_msg* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.i2c_msg*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i32 %0, i32* %5
store %struct.i2c_msg* %1, %struct.i2c_msg** %6
store i8* %2, i8** %7
%11 = load i32, i32* %5
%12 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%13 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%15 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %14, i32 0, i32 3
store i64 0, i64* %15
store i32 0, i32* %8
%16 = call i8* @malloc(i32 64)
%17 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%18 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %17, i32 0, i32 1
store i8* %16, i8** %18
%19 = load i8*, i8** %7
store i8* %19, i8** %9
br label %20
20: ; preds = %3, %64
%21 = load i32, i32* %8
%22 = icmp sge i32 %21, 64
br i1 %22, label %23, label %37
23: ; preds = %20
%24 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%25 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %24, i32 0, i32 1
%26 = load i8*, i8** %25
%27 = load i32, i32* %8
%28 = sdiv i32 %27, 64
%29 = add nsw i32 %28, 1
%30 = mul nsw i32 %29, 64
%31 = sext i32 %30 to i64
%32 = mul i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = call i8* @realloc(i8* %26, i32 %33)
%35 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%36 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %35, i32 0, i32 1
store i8* %34, i8** %36
br label %37
37: ; preds = %23, %20
%38 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%39 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %38, i32 0, i32 1
%40 = load i8*, i8** %39
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %71
44: ; preds = %37
%45 = load i8*, i8** %9
%46 = call i64 @strtol(i8* %45, i8** %10, i32 0)
%47 = trunc i64 %46 to i8
%48 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%49 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %48, i32 0, i32 1
%50 = load i8*, i8** %49
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 %47, i8* %53
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
%56 = load i8*, i8** %10
%57 = icmp eq i8* %56, null
br i1 %57, label %63, label %58
58: ; preds = %44
%59 = load i8*, i8** %10
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %58, %44
br label %67
64: ; preds = %58
%65 = load i8*, i8** %10
%66 = getelementptr inbounds i8, i8* %65, i64 1
store i8* %66, i8** %9
br label %20
67: ; preds = %63
%68 = load i32, i32* %8
%69 = load %struct.i2c_msg*, %struct.i2c_msg** %6
%70 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %69, i32 0, i32 2
store i32 %68, i32* %70
store i32 0, i32* %4
br label %71
71: ; preds = %67, %42
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.i2c_msg = type { i32, i8*, i32, i64 }
@str = [18 x i8] c"Run out of memory\00"
define i32 @add_i2c_write_data(i32 %0, %struct.i2c_msg* nocapture %1, i8* %2) {
%4 = alloca i8*
%5 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %1, i64 0, i32 0
store i32 %0, i32* %5
%6 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %1, i64 0, i32 3
store i64 0, i64* %6
%7 = tail call dereferenceable_or_null(64) i8* @malloc(i32 64)
%8 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %1, i64 0, i32 1
store i8* %7, i8** %8
%9 = bitcast i8** %4 to i8*
br label %10
10: ; preds = %33, %3
%11 = phi i64 [ %30, %33 ], [ 0, %3 ]
%12 = phi i8* [ %36, %33 ], [ %2, %3 ]
%13 = icmp ugt i64 %11, 63
%14 = load i8*, i8** %8
br i1 %13, label %15, label %20
15: ; preds = %10
%16 = trunc i64 %11 to i32
%17 = and i32 %16, 2147483584
%18 = add nuw i32 %17, 64
%19 = tail call i8* @realloc(i8* %14, i32 %18)
store i8* %19, i8** %8
br label %20
20: ; preds = %10, %15
%21 = phi i8* [ %19, %15 ], [ %14, %10 ]
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %40
25: ; preds = %20
%26 = call i64 @strtol(i8* %12, i8** %4, i32 0)
%27 = trunc i64 %26 to i8
%28 = load i8*, i8** %8
%29 = getelementptr inbounds i8, i8* %28, i64 %11
store i8 %27, i8* %29
%30 = add nuw i64 %11, 1
%31 = load i8*, i8** %4
%32 = icmp eq i8* %31, null
br i1 %32, label %37, label %33
33: ; preds = %25
%34 = load i8, i8* %31
%35 = icmp eq i8 %34, 0
%36 = getelementptr inbounds i8, i8* %31, i64 1
br i1 %35, label %37, label %10
37: ; preds = %25, %33
%38 = trunc i64 %30 to i32
%39 = getelementptr inbounds %struct.i2c_msg, %struct.i2c_msg* %1, i64 0, i32 2
store i32 %38, i32* %39
br label %40
40: ; preds = %37, %23
%41 = phi i32 [ -1, %23 ], [ 0, %37 ]
ret i32 %41
}