#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, void const *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned char);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, unsigned char *, size_t);
void write_msg(int tty_fd, const void *msg_body, size_t msg_body_size) {
  static unsigned char buf[1024];
  static unsigned char seq = 0;
  ssize_t ret;
  size_t i;
  if (msg_body_size > 255) {
    fputs("Message too large\n", stderr);
    return;
  }
  buf[0] = 0x02;
  buf[1] = seq++;
  buf[2] = 0x00;
  buf[3] = (unsigned char)msg_body_size;
  memcpy(buf + 4, msg_body, msg_body_size);
  buf[msg_body_size + 4] = 0x03;
  printf("Sending %zu bytes:", msg_body_size + 5);
  for (i = 0; i < msg_body_size + 5; ++i) {
    printf(" %02x", buf[i]);
  }
  putchar('\n');
  ret = write(tty_fd, buf, msg_body_size + 5);
  if (ret != (ssize_t)msg_body_size + 5) {
    perror("write: /dev/ttyS0");
    exit(233);
  }
}


x86 assembly (clang, -O0):
<write_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0xff,-0x18(%rbp)
jbe    11ab <write_msg+0x3b>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1060 <fputs@plt>
jmp    12ae <write_msg+0x13e>
movb   $0x2,0x2ebe(%rip)
mov    0x32b8(%rip),%al
mov    %al,%cl
add    $0x1,%cl
mov    %cl,0x32ad(%rip)
mov    %al,0x2ea8(%rip)
movb   $0x0,0x2ea2(%rip)
mov    -0x18(%rbp),%rax
mov    %al,0x2e99(%rip)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0x2e8b(%rip),%rdi
call   1070 <memcpy@plt>
mov    -0x18(%rbp),%rcx
lea    0x2e77(%rip),%rax
movb   $0x3,0x4(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x5,%rax
lea    0xe06(%rip),%rdi
movzbl %al,%esi
call   1050 <printf@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    $0x5,%rcx
cmp    %rcx,%rax
jae    125e <write_msg+0xee>
mov    -0x28(%rbp),%rcx
lea    0xde9(%rip),%rdi
lea    0x2e2c(%rip),%rax
movzbl (%rax,%rcx,1),%esi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    121d <write_msg+0xad>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rdx
add    $0x5,%rdx
lea    0x2df6(%rip),%rsi
call   1040 <write@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    $0x5,%rcx
cmp    %rcx,%rax
je     12ae <write_msg+0x13e>
lea    0xd8d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xe9,%edi
call   1090 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_msg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x100,%rdx
jb     119e <write_msg+0x2e>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fputs@plt>
mov    %rdx,%rbx
mov    %edi,%r14d
movb   $0x2,0x2ec5(%rip)
lea    0x2ebe(%rip),%r15
mov    0x32b8(%rip),%al
lea    0x1(%rax),%ecx
mov    %cl,0x32af(%rip)
mov    %al,0x2eaa(%rip)
movb   $0x0,0x2ea4(%rip)
mov    %bl,0x2e9f(%rip)
lea    0x2e99(%rip),%rdi
call   1070 <memcpy@plt>
movb   $0x3,0x4(%rbx,%r15,1)
add    $0x5,%rbx
movzbl %bl,%esi
lea    0xe1f(%rip),%rdi
call   1050 <printf@plt>
xor    %ebp,%ebp
lea    0xe24(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl 0x0(%rbp,%r15,1),%esi
mov    %r12,%rdi
call   1050 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1210 <write_msg+0xa0>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x2e38(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1040 <write@plt>
cmp    %rbx,%rax
jne    1251 <write_msg+0xe1>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd4(%rip),%rdi
call   1080 <perror@plt>
mov    $0xe9,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@write_msg.buf = internal global [1024 x i8] zeroinitializer
@write_msg.seq = internal global i8 0
@.str = [19 x i8] c"Message too large\0A\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Sending %zu bytes:\00"
@.str.2 = [6 x i8] c" %02x\00"
@.str.3 = [18 x i8] c"write: /dev/ttyS0\00"
define void @write_msg(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
%10 = icmp ugt i64 %9, 255
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %55
14: ; preds = %3
store i8 2, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 0)
%15 = load i8, i8* @write_msg.seq
%16 = add i8 %15, 1
store i8 %16, i8* @write_msg.seq
store i8 %15, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 1)
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 2)
%17 = load i64, i64* %6
%18 = trunc i64 %17 to i8
store i8 %18, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 3)
%19 = load i8*, i8** %5
%20 = load i64, i64* %6
%21 = call i32 @memcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 4), i8* %19, i64 %20)
%22 = load i64, i64* %6
%23 = add i64 %22, 4
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 %23
store i8 3, i8* %24
%25 = load i64, i64* %6
%26 = add i64 %25, 5
%27 = trunc i64 %26 to i8
%28 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %27)
store i64 0, i64* %8
br label %29
29: ; preds = %39, %14
%30 = load i64, i64* %8
%31 = load i64, i64* %6
%32 = add i64 %31, 5
%33 = icmp ult i64 %30, %32
br i1 %33, label %34, label %42
34: ; preds = %29
%35 = load i64, i64* %8
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %37)
br label %39
39: ; preds = %34
%40 = load i64, i64* %8
%41 = add i64 %40, 1
store i64 %41, i64* %8
br label %29
42: ; preds = %29
%43 = call i32 @putchar(i8 signext 10)
%44 = load i32, i32* %4
%45 = load i64, i64* %6
%46 = add i64 %45, 5
%47 = call i64 @write(i32 %44, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 0), i64 %46)
store i64 %47, i64* %7
%48 = load i64, i64* %7
%49 = load i64, i64* %6
%50 = add nsw i64 %49, 5
%51 = icmp ne i64 %48, %50
br i1 %51, label %52, label %55
52: ; preds = %42
%53 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 @exit(i32 233)
unreachable
55: ; preds = %11, %42
ret void
}

LLVM IR (-O2):
@write_msg.buf = internal global [1024 x i8] zeroinitializer
@write_msg.seq = internal unnamed_addr global i8 0
@.str = [19 x i8] c"Message too large\0A\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Sending %zu bytes:\00"
@.str.2 = [6 x i8] c" %02x\00"
@.str.3 = [18 x i8] c"write: /dev/ttyS0\00"
define void @write_msg(i32 %0, i8* %1, i64 %2) {
%4 = icmp ugt i64 %2, 255
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @stderr
%7 = tail call i32 @fputs(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %32
8: ; preds = %3
store i8 2, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 0)
%9 = load i8, i8* @write_msg.seq
%10 = add i8 %9, 1
store i8 %10, i8* @write_msg.seq
store i8 %9, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 1)
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 2)
%11 = trunc i64 %2 to i8
store i8 %11, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 3)
%12 = tail call i32 @memcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 4), i8* %1, i64 %2)
%13 = add nuw nsw i64 %2, 4
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 %13
store i8 3, i8* %14
%15 = add nuw nsw i64 %2, 5
%16 = trunc i64 %15 to i8
%17 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %16)
br label %18
18: ; preds = %8, %18
%19 = phi i64 [ 0, %8 ], [ %23, %18 ]
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %21)
%23 = add nuw i64 %19, 1
%24 = icmp eq i64 %23, %15
br i1 %24, label %25, label %18
25: ; preds = %18
%26 = tail call i32 @putchar(i8 signext 10)
%27 = tail call i64 @write(i32 %0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @write_msg.buf, i64 0, i64 0), i64 %15)
%28 = icmp eq i64 %27, %15
br i1 %28, label %32, label %29
29: ; preds = %25
%30 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%31 = tail call i32 @exit(i32 233)
unreachable
32: ; preds = %25, %5
ret void
}