#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 int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int *, char const *, int);
int strlen(char const *);
int writeErrorPacket(uint8_t *buffer, int limit, const char *message) {
  int length = 0;
  buffer[length++] = 2;
  int string_length = strlen(message);
  if (string_length > 255) {
    string_length = 255;
  }
  buffer[length++] = string_length;
  memcpy(buffer + length, message, string_length);
  length += string_length;
  return length;
}


x86 assembly (clang, -O0):
<writeErrorPacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x1c(%rbp)
movslq %ecx,%rcx
movl   $0x2,(%rax,%rcx,4)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xff,-0x20(%rbp)
jle    1173 <writeErrorPacket+0x53>
movl   $0xff,-0x20(%rbp)
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeErrorPacket>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rdi,%rbx
movl   $0x2,(%rdi)
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
cmp    $0xff,%eax
mov    $0xff,%eax
cmovge %eax,%ebp
mov    %ebp,0x4(%rbx)
add    $0x8,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
add    $0x2,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @writeErrorPacket(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %7
%9 = load i32*, i32** %4
%10 = load i32, i32* %7
%11 = add nsw i32 %10, 1
store i32 %11, i32* %7
%12 = sext i32 %10 to i64
%13 = getelementptr inbounds i32, i32* %9, i64 %12
store i32 2, i32* %13
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp sgt i32 %16, 255
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 255, i32* %8
br label %19
19: ; preds = %18, %3
%20 = load i32, i32* %8
%21 = load i32*, i32** %4
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
store i32 %23, i32* %7
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i32, i32* %21, i64 %24
store i32 %20, i32* %25
%26 = load i32*, i32** %4
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = call i32 @memcpy(i32* %29, i8* %30, i32 %31)
%33 = load i32, i32* %8
%34 = load i32, i32* %7
%35 = add nsw i32 %34, %33
store i32 %35, i32* %7
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
define i32 @writeErrorPacket(i32* %0, i32 %1, i8* %2) {
store i32 2, i32* %0
%4 = tail call i32 @strlen(i8* %2)
%5 = icmp slt i32 %4, 255
%6 = select i1 %5, i32 %4, i32 255
%7 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %6, i32* %7
%8 = getelementptr inbounds i32, i32* %0, i64 2
%9 = tail call i32 @memcpy(i32* %8, i8* %2, i32 %6)
%10 = add nsw i32 %6, 2
ret i32 %10
}