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

/* Variables and functions */
char *malloc(size_t);
int strlen(char const *);
int /*<<< orphan*/ strncat(char *, char const *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *msg_format(const unsigned sender, const time_t timestamp,
                 const char *msg) {
  (void)timestamp;
  char msg_sender[7];
  strncpy(msg_sender, (sender == 0) ? "You:  " : "Peer: ", 7);
  size_t msglen = strlen(msg_sender) + strlen(msg) + 1;
  char *message = malloc(msglen + 1);
  if (message == NULL)
    return NULL;
  message[0] = '\0';
  strncpy(message, msg_sender, sizeof msg_sender);
  strncat(message, msg, strlen(msg) + 1);
  message[msglen - 1] = '\n';
  message[msglen] = '\0';
  return message;
}


x86 assembly (clang, -O0):
<msg_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x1f(%rbp),%rdi
mov    -0xc(%rbp),%ecx
lea    0xea7(%rip),%rsi
lea    0xe99(%rip),%rax
cmp    $0x0,%ecx
cmove  %rax,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
lea    -0x1f(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c6 <msg_format+0x86>
movq   $0x0,-0x8(%rbp)
jmp    122e <msg_format+0xee>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
lea    -0x1f(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <strncat@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msg_format>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
test   %edi,%edi
lea    0xeab(%rip),%rax
lea    0xeab(%rip),%rsi
cmove  %rax,%rsi
lea    0x9(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0x1,%eax
movslq %eax,%r15
lea    0x1(%r15),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11d0 <msg_format+0x90>
movb   $0x0,(%rbx)
lea    0x9(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strncat@plt>
movw   $0xa,-0x1(%r15,%rbx,1)
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [7 x i8] c"You: \00"
@.str.1 = [7 x i8] c"Peer: \00"
define i8* @msg_format(i32 %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [7 x i8]
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i32, i32* %6
%12 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i64
%16 = select i1 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%17 = call i32 @strncpy(i8* %12, i8* %16, i32 7)
%18 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %19, %21
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
store i64 %24, i64* %9
%25 = load i64, i64* %9
%26 = add i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %10
%28 = load i8*, i8** %10
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %3
store i8* null, i8** %4
br label %51
31: ; preds = %3
%32 = load i8*, i8** %10
%33 = getelementptr inbounds i8, i8* %32, i64 0
store i8 0, i8* %33
%34 = load i8*, i8** %10
%35 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%36 = call i32 @strncpy(i8* %34, i8* %35, i32 7)
%37 = load i8*, i8** %10
%38 = load i8*, i8** %7
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %40, 1
%42 = call i32 @strncat(i8* %37, i8* %38, i32 %41)
%43 = load i8*, i8** %10
%44 = load i64, i64* %9
%45 = sub i64 %44, 1
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 10, i8* %46
%47 = load i8*, i8** %10
%48 = load i64, i64* %9
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %10
store i8* %50, i8** %4
br label %51
51: ; preds = %31, %30
%52 = load i8*, i8** %4
ret i8* %52
}

LLVM IR (-O2):
@.str = [7 x i8] c"You: \00"
@.str.1 = [7 x i8] c"Peer: \00"
define i8* @msg_format(i32 %0, i32 %1, i8* %2) {
%4 = alloca [7 x i8]
%5 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%6 = icmp eq i32 %0, 0
%7 = select i1 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0)
%8 = call i32 @strncpy(i8* %5, i8* %7, i32 7)
%9 = call i32 @strlen(i8* %5)
%10 = call i32 @strlen(i8* %2)
%11 = add i32 %9, 1
%12 = add i32 %11, %10
%13 = sext i32 %12 to i64
%14 = add nsw i64 %13, 1
%15 = call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %25, label %17
17: ; preds = %3
store i8 0, i8* %15
%18 = call i32 @strncpy(i8* %15, i8* %5, i32 7)
%19 = call i32 @strlen(i8* %2)
%20 = add nsw i32 %19, 1
%21 = call i32 @strncat(i8* %15, i8* %2, i32 %20)
%22 = add nsw i64 %13, -1
%23 = getelementptr inbounds i8, i8* %15, i64 %22
store i8 10, i8* %23
%24 = getelementptr inbounds i8, i8* %15, i64 %13
store i8 0, i8* %24
br label %25
25: ; preds = %3, %17
ret i8* %15
}