#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*/ special;
typedef int /*<<< orphan*/ position;
typedef int /*<<< orphan*/ modedivider;
typedef int /*<<< orphan*/ mode;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ cmdcode;

/* Variables and functions */
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ config_error(char *, int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char *);
int packet_buildtext(char **outputptr, char filename, char mode, char special,
                     char *text) {
  const char cmdcode = 'A', modedivider = 0x1b, position = 0x30;
  size_t textlen = 0;
  if (text != NULL) {
    textlen = strlen(text);
  }
  if (textlen > 4096) {
    config_error("Warning: shrank a text frame to fit %d bytes.", 4096);
    textlen = 4096;
  }
  size_t pktsize = sizeof(cmdcode) + sizeof(filename) + sizeof(modedivider) +
                   sizeof(position) + sizeof(mode) + textlen;
  if (special != 0) {
    pktsize += sizeof(special);
  }
  char *data = (char *)calloc(pktsize, sizeof(char));
  size_t offset = 0;
  memcpy(&data[offset], &cmdcode, sizeof(cmdcode));
  offset = sizeof(cmdcode);
  memcpy(&data[offset], &filename, sizeof(filename));
  offset += sizeof(filename);
  memcpy(&data[offset], &modedivider, sizeof(modedivider));
  offset += sizeof(modedivider);
  memcpy(&data[offset], &position, sizeof(position));
  offset += sizeof(position);
  memcpy(&data[offset], &mode, sizeof(mode));
  offset += sizeof(mode);
  if (special != 0) {
    memcpy(&data[offset], &special, sizeof(special));
    offset += sizeof(special);
  }
  if (textlen > 0) {
    memcpy(&data[offset], text, textlen);
  }
  *outputptr = data;
  return pktsize;
}


x86 assembly (clang, -O0):
<packet_buildtext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %sil,%dl
mov    %rdi,-0x8(%rbp)
mov    %dl,-0x9(%rbp)
mov    %cl,-0xa(%rbp)
mov    %al,-0xb(%rbp)
mov    %r8,-0x18(%rbp)
movb   $0x41,-0x19(%rbp)
movb   $0x1b,-0x1a(%rbp)
movb   $0x30,-0x1b(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118c <packet_buildtext+0x4c>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x1000,-0x28(%rbp)
jbe    11b3 <packet_buildtext+0x73>
lea    0xe5f(%rip),%rdi
mov    $0x1000,%esi
call   1060 <config_error@plt>
movq   $0x1000,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x30(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
je     11d8 <packet_buildtext+0x98>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
movq   $0x1,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0x1b(%rbp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0xa(%rbp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
movsbl -0xb(%rbp),%eax
cmp    $0x0,%eax
je     12c7 <packet_buildtext+0x187>
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
lea    -0xb(%rbp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    12e7 <packet_buildtext+0x1a7>
mov    -0x38(%rbp),%rdi
add    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<packet_buildtext>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r15
mov    %ecx,%r12d
mov    %rdi,%r14
mov    %sil,0x7(%rsp)
mov    %dl,0x6(%rsp)
mov    %r12b,0x5(%rsp)
movb   $0x41,0x4(%rsp)
movb   $0x1b,0x3(%rsp)
movb   $0x30,0x2(%rsp)
test   %r8,%r8
je     11a2 <packet_buildtext+0x62>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
cmp    $0x1001,%rax
jb     11a5 <packet_buildtext+0x65>
lea    0xe70(%rip),%rdi
mov    $0x1000,%esi
call   1060 <config_error@plt>
mov    $0x1000,%r13d
jmp    11a5 <packet_buildtext+0x65>
xor    %r13d,%r13d
xor    %eax,%eax
test   %r12b,%r12b
sete   %al
mov    %r13,%rbx
sub    %rax,%rbx
add    $0x6,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbp
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
mov    %rax,%rdi
call   1050 <memcpy@plt>
lea    0x1(%rbp),%rdi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
lea    0x2(%rbp),%rdi
lea    0x3(%rsp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
lea    0x3(%rbp),%rdi
lea    0x2(%rsp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
lea    0x4(%rbp),%rdi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
test   %r12b,%r12b
je     126c <packet_buildtext+0x12c>
lea    0x5(%rbp),%rdi
lea    0x5(%rsp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    $0x6,%eax
test   %r13,%r13
je     1258 <packet_buildtext+0x118>
mov    %rbp,%rdi
add    %rax,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    %rbp,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x5,%eax
test   %r13,%r13
jne    1247 <packet_buildtext+0x107>
jmp    1258 <packet_buildtext+0x118>

LLVM IR (-O0):
@.str = [46 x i8] c"Warning: shrank a text frame to fit %d bytes.\00"
define i32 @packet_buildtext(i8** %0, i8 signext %1, i8 signext %2, i8 signext %3, i8* %4) {
%6 = alloca i8**
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i8
%12 = alloca i8
%13 = alloca i8
%14 = alloca i64
%15 = alloca i64
%16 = alloca i8*
%17 = alloca i64
store i8** %0, i8*** %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
store i8* %4, i8** %10
store i8 65, i8* %11
store i8 27, i8* %12
store i8 48, i8* %13
store i64 0, i64* %14
%18 = load i8*, i8** %10
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %5
%21 = load i8*, i8** %10
%22 = call i64 @strlen(i8* %21)
store i64 %22, i64* %14
br label %23
23: ; preds = %20, %5
%24 = load i64, i64* %14
%25 = icmp ugt i64 %24, 4096
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @config_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 4096)
store i64 4096, i64* %14
br label %28
28: ; preds = %26, %23
%29 = load i64, i64* %14
%30 = add i64 5, %29
store i64 %30, i64* %15
%31 = load i8, i8* %9
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i64, i64* %15
%36 = add i64 %35, 1
store i64 %36, i64* %15
br label %37
37: ; preds = %34, %28
%38 = load i64, i64* %15
%39 = call i64 @calloc(i64 %38, i32 1)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %16
store i64 0, i64* %17
%41 = load i8*, i8** %16
%42 = load i64, i64* %17
%43 = getelementptr inbounds i8, i8* %41, i64 %42
%44 = call i32 @memcpy(i8* %43, i8* %11, i64 1)
store i64 1, i64* %17
%45 = load i8*, i8** %16
%46 = load i64, i64* %17
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = call i32 @memcpy(i8* %47, i8* %7, i64 1)
%49 = load i64, i64* %17
%50 = add i64 %49, 1
store i64 %50, i64* %17
%51 = load i8*, i8** %16
%52 = load i64, i64* %17
%53 = getelementptr inbounds i8, i8* %51, i64 %52
%54 = call i32 @memcpy(i8* %53, i8* %12, i64 1)
%55 = load i64, i64* %17
%56 = add i64 %55, 1
store i64 %56, i64* %17
%57 = load i8*, i8** %16
%58 = load i64, i64* %17
%59 = getelementptr inbounds i8, i8* %57, i64 %58
%60 = call i32 @memcpy(i8* %59, i8* %13, i64 1)
%61 = load i64, i64* %17
%62 = add i64 %61, 1
store i64 %62, i64* %17
%63 = load i8*, i8** %16
%64 = load i64, i64* %17
%65 = getelementptr inbounds i8, i8* %63, i64 %64
%66 = call i32 @memcpy(i8* %65, i8* %8, i64 1)
%67 = load i64, i64* %17
%68 = add i64 %67, 1
store i64 %68, i64* %17
%69 = load i8, i8* %9
%70 = sext i8 %69 to i32
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %79
72: ; preds = %37
%73 = load i8*, i8** %16
%74 = load i64, i64* %17
%75 = getelementptr inbounds i8, i8* %73, i64 %74
%76 = call i32 @memcpy(i8* %75, i8* %9, i64 1)
%77 = load i64, i64* %17
%78 = add i64 %77, 1
store i64 %78, i64* %17
br label %79
79: ; preds = %72, %37
%80 = load i64, i64* %14
%81 = icmp ugt i64 %80, 0
br i1 %81, label %82, label %89
82: ; preds = %79
%83 = load i8*, i8** %16
%84 = load i64, i64* %17
%85 = getelementptr inbounds i8, i8* %83, i64 %84
%86 = load i8*, i8** %10
%87 = load i64, i64* %14
%88 = call i32 @memcpy(i8* %85, i8* %86, i64 %87)
br label %89
89: ; preds = %82, %79
%90 = load i8*, i8** %16
%91 = load i8**, i8*** %6
store i8* %90, i8** %91
%92 = load i64, i64* %15
%93 = trunc i64 %92 to i32
ret i32 %93
}

LLVM IR (-O2):
@.str = [46 x i8] c"Warning: shrank a text frame to fit %d bytes.\00"
define i32 @packet_buildtext(i8** nocapture writeonly %0, i8 signext %1, i8 signext %2, i8 signext %3, i8* %4) {
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
store i8 %1, i8* %6
store i8 %2, i8* %7
store i8 %3, i8* %8
store i8 65, i8* %9
store i8 27, i8* %10
store i8 48, i8* %11
%12 = icmp eq i8* %4, null
br i1 %12, label %18, label %13
13: ; preds = %5
%14 = tail call i64 @strlen(i8* %4)
%15 = icmp ugt i64 %14, 4096
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @config_error(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 4096)
br label %18
18: ; preds = %5, %16, %13
%19 = phi i64 [ 4096, %16 ], [ %14, %13 ], [ 0, %5 ]
%20 = icmp eq i8 %3, 0
%21 = select i1 %20, i64 5, i64 6
%22 = add nuw nsw i64 %21, %19
%23 = tail call i64 @calloc(i64 %22, i32 1)
%24 = inttoptr i64 %23 to i8*
%25 = call i32 @memcpy(i8* %24, i8* %9, i64 1)
%26 = getelementptr inbounds i8, i8* %24, i64 1
%27 = call i32 @memcpy(i8* %26, i8* %6, i64 1)
%28 = getelementptr inbounds i8, i8* %24, i64 2
%29 = call i32 @memcpy(i8* %28, i8* %10, i64 1)
%30 = getelementptr inbounds i8, i8* %24, i64 3
%31 = call i32 @memcpy(i8* %30, i8* %11, i64 1)
%32 = getelementptr inbounds i8, i8* %24, i64 4
%33 = call i32 @memcpy(i8* %32, i8* %7, i64 1)
br i1 %20, label %37, label %34
34: ; preds = %18
%35 = getelementptr inbounds i8, i8* %24, i64 5
%36 = call i32 @memcpy(i8* %35, i8* %8, i64 1)
br label %37
37: ; preds = %34, %18
%38 = phi i64 [ 6, %34 ], [ 5, %18 ]
%39 = icmp eq i64 %19, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = getelementptr inbounds i8, i8* %24, i64 %38
%42 = call i32 @memcpy(i8* %41, i8* %4, i64 %19)
br label %43
43: ; preds = %40, %37
store i8* %24, i8** %0
%44 = trunc i64 %22 to i32
ret i32 %44
}