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

/* Variables and functions */
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char **, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, char const *,
                            char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int FormSendMsg(char *output, char *msg, const char *nick) {
  char buf[512];
  char hostname[200];
  int shift = 0, index;
  int nick_len = strlen(nick), host_len = 0, msg_len = 0;
  memset(&buf, 0, 512);
  memset(&hostname, 0, 200);
  gethostname(hostname, sizeof(hostname));
  host_len = strlen(hostname);
  if (host_len + 2 * nick_len + 3 >= 200)
    return -1;
  snprintf(buf, 512, ":%s!%s@%s ", nick, nick, hostname);
  shift = strlen(buf);
  msg_len = strlen(msg);
  if (shift + msg_len + 2 < 512) {
    strncpy(buf + shift, msg, msg_len);
    shift += msg_len;
    strncpy(buf + shift, "\r\n", 2);
    shift += 2;
    memcpy(output, buf, shift);
    output[shift] = '\0';
    return 0;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<FormSendMsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x310,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2f4(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2fc(%rbp)
movl   $0x0,-0x300(%rbp)
movl   $0x0,-0x304(%rbp)
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    -0x2f0(%rbp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1060 <memset@plt>
lea    -0x2f0(%rbp),%rdi
mov    $0xc8,%esi
call   1080 <gethostname@plt>
lea    -0x2f0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x300(%rbp)
mov    -0x300(%rbp),%eax
mov    -0x2fc(%rbp),%ecx
shl    $0x1,%ecx
add    %ecx,%eax
add    $0x3,%eax
cmp    $0xc8,%eax
jl     1218 <FormSendMsg+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1315 <FormSendMsg+0x1b5>
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    -0x2f0(%rbp),%r9
mov    $0x200,%esi
lea    0xdc6(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2f4(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x304(%rbp)
mov    -0x2f4(%rbp),%eax
add    -0x304(%rbp),%eax
add    $0x2,%eax
cmp    $0x200,%eax
jge    130e <FormSendMsg+0x1ae>
lea    -0x220(%rbp),%rdi
movslq -0x2f4(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x304(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x304(%rbp),%eax
add    -0x2f4(%rbp),%eax
mov    %eax,-0x2f4(%rbp)
lea    -0x220(%rbp),%rdi
movslq -0x2f4(%rbp),%rax
add    %rax,%rdi
lea    0xd47(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
mov    -0x2f4(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x2f4(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    -0x2f4(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x10(%rbp),%rax
movslq -0x2f4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    1315 <FormSendMsg+0x1b5>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x310,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FormSendMsg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2d0,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
lea    0xd0(%rsp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1060 <memset@plt>
mov    %rbp,%rdi
mov    $0xc8,%esi
call   1080 <gethostname@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    (%rax,%r12,2),%eax
mov    $0xffffffff,%ebp
cmp    $0xc4,%eax
jg     1269 <FormSendMsg+0x109>
lea    0xe27(%rip),%rdx
lea    0xd0(%rsp),%r12
mov    %rsp,%r9
mov    %r12,%rdi
mov    $0x200,%esi
mov    %rbx,%rcx
mov    %rbx,%r8
call   1050 <snprintf@plt>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbx,1),%ecx
cmp    $0x1fd,%ecx
jg     1269 <FormSendMsg+0x109>
movslq %ecx,%rbp
lea    0x2(%rbp),%r12
movslq %ebx,%rcx
lea    (%rsp,%rcx,1),%rdi
add    $0xd0,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    (%rsp,%rbp,1),%rdi
add    $0xd0,%rdi
lea    0xdc6(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncpy@plt>
lea    0xd0(%rsp),%rsi
mov    %r14,%rdi
mov    %r12d,%edx
call   1070 <memcpy@plt>
movb   $0x0,(%r14,%r12,1)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x2d0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c":%s!%s@%s \00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i32 @FormSendMsg(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [512 x i8]
%9 = alloca [200 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %10
%15 = load i8*, i8** %7
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%17 = bitcast [512 x i8]* %8 to i8**
%18 = call i32 @memset(i8** %17, i32 0, i32 512)
%19 = bitcast [200 x i8]* %9 to i8**
%20 = call i32 @memset(i8** %19, i32 0, i32 200)
%21 = getelementptr inbounds [200 x i8], [200 x i8]* %9, i64 0, i64 0
%22 = call i32 @gethostname(i8* %21, i32 200)
%23 = getelementptr inbounds [200 x i8], [200 x i8]* %9, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
store i32 %24, i32* %13
%25 = load i32, i32* %13
%26 = load i32, i32* %12
%27 = mul nsw i32 2, %26
%28 = add nsw i32 %25, %27
%29 = add nsw i32 %28, 3
%30 = icmp sge i32 %29, 200
br i1 %30, label %31, label %32
31: ; preds = %3
store i32 -1, i32* %4
br label %74
32: ; preds = %3
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%34 = load i8*, i8** %7
%35 = load i8*, i8** %7
%36 = getelementptr inbounds [200 x i8], [200 x i8]* %9, i64 0, i64 0
%37 = call i32 @snprintf(i8* %33, i32 512, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %35, i8* %36)
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%39 = call i32 @strlen(i8* %38)
store i32 %39, i32* %10
%40 = load i8*, i8** %6
%41 = call i32 @strlen(i8* %40)
store i32 %41, i32* %14
%42 = load i32, i32* %10
%43 = load i32, i32* %14
%44 = add nsw i32 %42, %43
%45 = add nsw i32 %44, 2
%46 = icmp slt i32 %45, 512
br i1 %46, label %47, label %73
47: ; preds = %32
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8*, i8** %6
%53 = load i32, i32* %14
%54 = call i32 @strncpy(i8* %51, i8* %52, i32 %53)
%55 = load i32, i32* %14
%56 = load i32, i32* %10
%57 = add nsw i32 %56, %55
store i32 %57, i32* %10
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = call i32 @strncpy(i8* %61, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 2
store i32 %64, i32* %10
%65 = load i8*, i8** %5
%66 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%67 = load i32, i32* %10
%68 = call i32 @memcpy(i8* %65, i8* %66, i32 %67)
%69 = load i8*, i8** %5
%70 = load i32, i32* %10
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 0, i8* %72
store i32 0, i32* %4
br label %74
73: ; preds = %32
store i32 -1, i32* %4
br label %74
74: ; preds = %73, %47, %31
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@.str = [11 x i8] c":%s!%s@%s \00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i32 @FormSendMsg(i8* %0, i8* %1, i8* %2) {
%4 = alloca [512 x i8]
%5 = alloca [200 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%8 = tail call i32 @strlen(i8* %2)
%9 = bitcast [512 x i8]* %4 to i8**
%10 = call i32 @memset(i8** %9, i32 0, i32 512)
%11 = bitcast [200 x i8]* %5 to i8**
%12 = call i32 @memset(i8** %11, i32 0, i32 200)
%13 = call i32 @gethostname(i8* %7, i32 200)
%14 = call i32 @strlen(i8* %7)
%15 = shl nsw i32 %8, 1
%16 = add nsw i32 %14, %15
%17 = icmp sgt i32 %16, 196
br i1 %17, label %35, label %18
18: ; preds = %3
%19 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %2, i8* %7)
%20 = call i32 @strlen(i8* %6)
%21 = call i32 @strlen(i8* %1)
%22 = add nsw i32 %21, %20
%23 = add nsw i32 %22, 2
%24 = icmp slt i32 %22, 510
br i1 %24, label %25, label %35
25: ; preds = %18
%26 = sext i32 %20 to i64
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %26
%28 = call i32 @strncpy(i8* %27, i8* %1, i32 %21)
%29 = sext i32 %22 to i64
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %29
%31 = call i32 @strncpy(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%32 = call i32 @memcpy(i8* %0, i8* %6, i32 %23)
%33 = sext i32 %23 to i64
%34 = getelementptr inbounds i8, i8* %0, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %18, %3, %25
%36 = phi i32 [ 0, %25 ], [ -1, %3 ], [ -1, %18 ]
ret i32 %36
}