#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 /*<<< orphan*/ prefix;

/* Variables and functions */
int /*<<< orphan*/ Log(char *);
int /*<<< orphan*/ WriteChars(int, char const *, int);
int snprintf(char *, int, char *, char, int);
size_t strlen(char const *);
void WritePacket(int fd, char type, const char *message) {
  size_t len_s = strlen(message);
  if (len_s >= 0xFFFF) {
    Log("overlong message, cannot write (hardcoded limit)");
    return;
  }
  int len = len_s;
  if (len < 0 || (size_t)len != len_s) {
    Log("overlong message, cannot write (does not fit in int)");
    return;
  }
  char prefix[16];
  int prefixlen = snprintf(prefix, sizeof(prefix), "%c %d\n", type, len);
  if (prefixlen <= 0 || (size_t)prefixlen >= sizeof(prefix)) {
    Log("overlong prefix, cannot write");
    return;
  }
  if (!WriteChars(fd, prefix, prefixlen)) {
    return;
  }
  if (len != 0 && !WriteChars(fd, message, len)) {
    return;
  }
  if (!WriteChars(fd, "\n", 1)) {
    return;
  }
}


x86 assembly (clang, -O0):
<WritePacket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffff,-0x18(%rbp)
jb     1181 <WritePacket+0x41>
lea    0xe89(%rip),%rdi
call   1060 <Log@plt>
jmp    1262 <WritePacket+0x122>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11a0 <WritePacket+0x60>
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11b1 <WritePacket+0x71>
lea    0xe8a(%rip),%rdi
call   1060 <Log@plt>
jmp    1262 <WritePacket+0x122>
lea    -0x30(%rbp),%rdi
mov    -0x5(%rbp),%al
mov    -0x1c(%rbp),%r8d
mov    $0x10,%esi
lea    0xe9e(%rip),%rdx
movsbl %al,%ecx
call   1050 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    11eb <WritePacket+0xab>
movslq -0x34(%rbp),%rax
cmp    $0x10,%rax
jb     11fc <WritePacket+0xbc>
lea    0xe7b(%rip),%rdi
call   1060 <Log@plt>
jmp    1262 <WritePacket+0x122>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <WriteChars@plt>
cmp    $0x0,%eax
jne    1219 <WritePacket+0xd9>
jmp    1262 <WritePacket+0x122>
cmpl   $0x0,-0x1c(%rbp)
je     1240 <WritePacket+0x100>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <WriteChars@plt>
cmp    $0x0,%eax
jne    1240 <WritePacket+0x100>
jmp    1262 <WritePacket+0x122>
mov    -0x4(%rbp),%edi
lea    0xe21(%rip),%rsi
mov    $0x1,%edx
call   1030 <WriteChars@plt>
cmp    $0x0,%eax
jne    1262 <WritePacket+0x122>
jmp    1262 <WritePacket+0x122>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WritePacket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %esi,%ebx
mov    %edi,%r14d
mov    %rdx,%rdi
call   1040 <strlen@plt>
cmp    $0xffff,%rax
jb     1178 <WritePacket+0x38>
lea    0xe97(%rip),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <Log@plt>
mov    %rax,%rbp
movsbl %bl,%ecx
lea    0xeac(%rip),%rdx
mov    %rsp,%rdi
mov    $0x10,%esi
mov    %ebp,%r8d
call   1050 <snprintf@plt>
lea    -0x10(%rax),%ecx
cmp    $0xfffffff0,%ecx
ja     11ab <WritePacket+0x6b>
lea    0xe94(%rip),%rdi
call   1060 <Log@plt>
jmp    11e5 <WritePacket+0xa5>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1030 <WriteChars@plt>
test   %eax,%eax
je     11e5 <WritePacket+0xa5>
test   %ebp,%ebp
je     11d1 <WritePacket+0x91>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <WriteChars@plt>
test   %eax,%eax
je     11e5 <WritePacket+0xa5>
lea    0xe5e(%rip),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <WriteChars@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"overlong message, cannot write (hardcoded limit)\00"
@.str.1 = [53 x i8] c"overlong message, cannot write (does not fit in int)\00"
@.str.2 = [7 x i8] c"%c %d\0A\00"
@.str.3 = [30 x i8] c"overlong prefix, cannot write\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @WritePacket(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca [16 x i8]
%10 = alloca i32
store i32 %0, i32* %4
store i8 %1, i8* %5
store i8* %2, i8** %6
%11 = load i8*, i8** %6
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %7
%13 = load i64, i64* %7
%14 = icmp uge i64 %13, 65535
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @Log(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %64
17: ; preds = %3
%18 = load i64, i64* %7
%19 = trunc i64 %18 to i32
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %27, label %22
22: ; preds = %17
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = load i64, i64* %7
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %22, %17
%28 = call i32 @Log(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %64
29: ; preds = %22
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%31 = load i8, i8* %5
%32 = load i32, i32* %8
%33 = call i32 @snprintf(i8* %30, i32 16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8 signext %31, i32 %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp sle i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %29
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = icmp uge i64 %38, 16
br i1 %39, label %40, label %42
40: ; preds = %36, %29
%41 = call i32 @Log(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %64
42: ; preds = %36
%43 = load i32, i32* %4
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%45 = load i32, i32* %10
%46 = call i32 @WriteChars(i32 %43, i8* %44, i32 %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %42
br label %64
49: ; preds = %42
%50 = load i32, i32* %8
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %49
%53 = load i32, i32* %4
%54 = load i8*, i8** %6
%55 = load i32, i32* %8
%56 = call i32 @WriteChars(i32 %53, i8* %54, i32 %55)
%57 = icmp ne i32 %56, 0
br i1 %57, label %59, label %58
58: ; preds = %52
br label %64
59: ; preds = %52, %49
%60 = load i32, i32* %4
%61 = call i32 @WriteChars(i32 %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
%62 = icmp ne i32 %61, 0
br i1 %62, label %64, label %63
63: ; preds = %59
br label %64
64: ; preds = %15, %27, %40, %48, %58, %63, %59
ret void
}

LLVM IR (-O2):
@.str = [49 x i8] c"overlong message, cannot write (hardcoded limit)\00"
@.str.2 = [7 x i8] c"%c %d\0A\00"
@.str.3 = [30 x i8] c"overlong prefix, cannot write\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @WritePacket(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca [16 x i8]
%5 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%6 = icmp ugt i64 %5, 65534
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @Log(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
br label %28
9: ; preds = %3
%10 = trunc i64 %5 to i32
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%12 = call i32 @snprintf(i8* %11, i32 16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8 signext %1, i32 %10)
%13 = add i32 %12, -16
%14 = icmp ult i32 %13, -15
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @Log(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %27
17: ; preds = %9
%18 = call i32 @WriteChars(i32 %0, i8* %11, i32 %12)
%19 = icmp eq i32 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %17
%21 = icmp eq i32 %10, 0
br i1 %21, label %25, label %22
22: ; preds = %20
%23 = call i32 @WriteChars(i32 %0, i8* %2, i32 %10)
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22, %20
%26 = call i32 @WriteChars(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 1)
br label %27
27: ; preds = %25, %22, %17, %15
br label %28
28: ; preds = %27, %7
ret void
}