#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*/ 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 *, size_t);
size_t strlen(char *);
int packet_buildstring(char **outputptr, char filename, char *text) {
  char cmdcode = 'G';
  size_t textlen = 0;
  if (text != NULL) {
    textlen = strlen(text);
  }
  if (textlen > 125) {
    config_error("Warning: shrank a string frame to fit %d bytes.", 125);
    textlen = 125;
  }
  size_t pktsize = sizeof(cmdcode) + sizeof(filename) + textlen;
  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);
  if (textlen > 0) {
    memcpy(&data[offset], text, textlen);
  }
  *outputptr = data;
  return pktsize;
}


x86 assembly (clang, -O0):
<packet_buildstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x47,-0x19(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117a <packet_buildstring+0x3a>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x7d,-0x28(%rbp)
jbe    119e <packet_buildstring+0x5e>
lea    0xe74(%rip),%rdi
mov    $0x7d,%esi
call   1060 <config_error@plt>
movq   $0x7d,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x2,%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)
cmpq   $0x0,-0x28(%rbp)
jbe    1224 <packet_buildstring+0xe4>
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_buildstring>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rdi,%r14
mov    %sil,0xf(%rsp)
movb   $0x47,0xe(%rsp)
test   %rdx,%rdx
je     118c <packet_buildstring+0x4c>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%r13
cmp    $0x7e,%rax
jb     118f <packet_buildstring+0x4f>
lea    0xe86(%rip),%rdi
mov    $0x7d,%esi
call   1060 <config_error@plt>
mov    $0x7d,%r13d
jmp    118f <packet_buildstring+0x4f>
xor    %r13d,%r13d
lea    0x2(%r13),%r12
mov    %r12,%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
lea    0xe(%rsp),%rsi
mov    $0x1,%edx
mov    %rax,%rdi
call   1050 <memcpy@plt>
lea    0x1(%rbx),%rdi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
test   %r13,%r13
je     11df <packet_buildstring+0x9f>
mov    %rbx,%rdi
add    $0x2,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
mov    %rbx,(%r14)
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [48 x i8] c"Warning: shrank a string frame to fit %d bytes.\00"
define i32 @packet_buildstring(i8** nocapture writeonly %0, i8 signext %1, i8* %2) {
%4 = alloca i8
%5 = alloca i8
store i8 %1, i8* %4
store i8 71, i8* %5
%6 = icmp eq i8* %2, null
br i1 %6, label %12, label %7
7: ; preds = %3
%8 = tail call i64 @strlen(i8* %2)
%9 = icmp ugt i64 %8, 125
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @config_error(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i32 125)
br label %12
12: ; preds = %3, %10, %7
%13 = phi i64 [ 125, %10 ], [ %8, %7 ], [ 0, %3 ]
%14 = add nuw nsw i64 %13, 2
%15 = tail call i64 @calloc(i64 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
%17 = call i32 @memcpy(i8* %16, i8* %5, i64 1)
%18 = getelementptr inbounds i8, i8* %16, i64 1
%19 = call i32 @memcpy(i8* %18, i8* %4, i64 1)
%20 = icmp eq i64 %13, 0
br i1 %20, label %24, label %21
21: ; preds = %12
%22 = getelementptr inbounds i8, i8* %16, i64 2
%23 = call i32 @memcpy(i8* %22, i8* %2, i64 %13)
br label %24
24: ; preds = %21, %12
store i8* %16, i8** %0
%25 = trunc i64 %14 to i32
ret i32 %25
}