#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 meif_message {
  int dummy;
};

/* Variables and functions */
int bcm4751_serial_write(int, void *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ meif_message_log(struct meif_message *);
int meif_message_to_data(struct meif_message *, void **);
int /*<<< orphan*/ printf(char *, ...);
int meif_message_send(struct meif_message *meif_message, int fd) {
  void *data = NULL;
  int length = 0;
  int chunk = 160;
  int written = 0;
  int count = 0;
  int rc;
  int i;
  if (meif_message == NULL)
    return -1;
  length = meif_message_to_data(meif_message, &data);
  if (length < 0 || data == NULL) {
    return -1;
  }
  printf("Sending %d bytes!\n", length);
  meif_message_log(meif_message);
  printf("\n");
  while (written < length) {
    count = length - written < chunk ? length - written : chunk;
    rc = bcm4751_serial_write(fd, data + written, count, NULL);
    if (rc <= 0) {
      printf("Write failed, written %d/%d\n", written, length);
      free(data);
      return -1;
    }
    written += rc;
  }
  free(data);
  return 0;
}


x86 assembly (clang, -O0):
<meif_message_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0xa0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    119a <meif_message_send+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <meif_message_send+0x146>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <meif_message_to_data@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     11bf <meif_message_send+0x6f>
cmpq   $0x0,-0x20(%rbp)
jne    11cb <meif_message_send+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <meif_message_send+0x146>
mov    -0x24(%rbp),%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <meif_message_log@plt>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1286 <meif_message_send+0x136>
mov    -0x24(%rbp),%eax
sub    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    121c <meif_message_send+0xcc>
mov    -0x24(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    1222 <meif_message_send+0xd2>
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x30(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <bcm4751_serial_write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     1278 <meif_message_send+0x128>
mov    -0x2c(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1296 <meif_message_send+0x146>
mov    -0x34(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11f3 <meif_message_send+0xa3>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<meif_message_send>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%rsp)
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     122f <meif_message_send+0xcf>
mov    %esi,%r15d
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1070 <meif_message_to_data@plt>
test   %eax,%eax
js     122f <meif_message_send+0xcf>
cmpq   $0x0,(%rsp)
je     122f <meif_message_send+0xcf>
mov    %eax,%ebp
lea    0xe58(%rip),%rdi
xor    %r14d,%r14d
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <meif_message_log@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
test   %ebp,%ebp
je     1226 <meif_message_send+0xc6>
xor    %ebx,%ebx
mov    $0xa0,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edx
sub    %ebx,%edx
cmp    $0xa0,%edx
cmovge %r14d,%edx
movslq %ebx,%rsi
add    (%rsp),%rsi
mov    %r15d,%edi
xor    %ecx,%ecx
call   1080 <bcm4751_serial_write@plt>
test   %eax,%eax
jle    120e <meif_message_send+0xae>
add    %eax,%ebx
cmp    %ebx,%ebp
jg     11e0 <meif_message_send+0x80>
xor    %r14d,%r14d
jmp    1226 <meif_message_send+0xc6>
lea    0xdfe(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%r14d
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.meif_message = type { i32 }
@.str = [19 x i8] c"Sending %d bytes!\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [29 x i8] c"Write failed, written %d/%d\0A\00"
define i32 @meif_message_send(%struct.meif_message* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.meif_message*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.meif_message* %0, %struct.meif_message** %4
store i32 %1, i32* %5
store i8* null, i8** %6
store i32 0, i32* %7
store i32 160, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%13 = load %struct.meif_message*, %struct.meif_message** %4
%14 = icmp eq %struct.meif_message* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %71
16: ; preds = %2
%17 = load %struct.meif_message*, %struct.meif_message** %4
%18 = call i32 @meif_message_to_data(%struct.meif_message* %17, i8** %6)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %21, %16
store i32 -1, i32* %3
br label %71
25: ; preds = %21
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = load %struct.meif_message*, %struct.meif_message** %4
%29 = call i32 @meif_message_log(%struct.meif_message* %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %64, %25
%32 = load i32, i32* %9
%33 = load i32, i32* %7
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %68
35: ; preds = %31
%36 = load i32, i32* %7
%37 = load i32, i32* %9
%38 = sub nsw i32 %36, %37
%39 = load i32, i32* %8
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = load i32, i32* %7
%43 = load i32, i32* %9
%44 = sub nsw i32 %42, %43
br label %47
45: ; preds = %35
%46 = load i32, i32* %8
br label %47
47: ; preds = %45, %41
%48 = phi i32 [ %44, %41 ], [ %46, %45 ]
store i32 %48, i32* %10
%49 = load i32, i32* %5
%50 = load i8*, i8** %6
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = getelementptr i8, i8* %50, i64 %52
%54 = load i32, i32* %10
%55 = call i32 @bcm4751_serial_write(i32 %49, i8* %53, i32 %54, i32* null)
store i32 %55, i32* %11
%56 = load i32, i32* %11
%57 = icmp sle i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %47
%59 = load i32, i32* %9
%60 = load i32, i32* %7
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %59, i32 %60)
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
store i32 -1, i32* %3
br label %71
64: ; preds = %47
%65 = load i32, i32* %11
%66 = load i32, i32* %9
%67 = add nsw i32 %66, %65
store i32 %67, i32* %9
br label %31
68: ; preds = %31
%69 = load i8*, i8** %6
%70 = call i32 @free(i8* %69)
store i32 0, i32* %3
br label %71
71: ; preds = %68, %58, %24, %15
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.meif_message = type { i32 }
@.str = [19 x i8] c"Sending %d bytes!\0A\00"
@.str.2 = [29 x i8] c"Write failed, written %d/%d\0A\00"
define i32 @meif_message_send(%struct.meif_message* %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = icmp eq %struct.meif_message* %0, null
br i1 %5, label %36, label %6
6: ; preds = %2
%7 = call i32 @meif_message_to_data(%struct.meif_message* %0, i8** %3)
%8 = icmp slt i32 %7, 0
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %36, label %12
12: ; preds = %6
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%14 = call i32 @meif_message_log(%struct.meif_message* %0)
%15 = call i32 @putchar(i32 10)
%16 = icmp eq i32 %7, 0
br i1 %16, label %32, label %19
17: ; preds = %19
%18 = icmp sgt i32 %7, %29
br i1 %18, label %19, label %32
19: ; preds = %12, %17
%20 = phi i32 [ %29, %17 ], [ 0, %12 ]
%21 = sub nsw i32 %7, %20
%22 = icmp slt i32 %21, 160
%23 = select i1 %22, i32 %21, i32 160
%24 = load i8*, i8** %3
%25 = sext i32 %20 to i64
%26 = getelementptr i8, i8* %24, i64 %25
%27 = call i32 @bcm4751_serial_write(i32 %1, i8* %26, i32 %23, i32* null)
%28 = icmp slt i32 %27, 1
%29 = add nsw i32 %27, %20
br i1 %28, label %30, label %17
30: ; preds = %19
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %7)
br label %32
32: ; preds = %17, %12, %30
%33 = phi i32 [ -1, %30 ], [ 0, %12 ], [ 0, %17 ]
%34 = load i8*, i8** %3
%35 = call i32 @free(i8* %34)
br label %36
36: ; preds = %32, %6, %2
%37 = phi i32 [ -1, %2 ], [ -1, %6 ], [ %33, %32 ]
ret i32 %37
}