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

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char *);
char *first_n_letters(char *s, int n) {
  char *ss = malloc(n + 1);
  memcpy(ss, s, n);
  ss[n] = 0;
  if (strlen(s) > n && n > 3) {
    ss[n - 1] = ss[n - 2] = ss[n - 3] = '.';
  }
  return ss;
}


x86 assembly (clang, -O0):
<first_n_letters>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0xc(%rbp),%eax
jle    11b9 <first_n_letters+0x89>
cmpl   $0x3,-0xc(%rbp)
jle    11b9 <first_n_letters+0x89>
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x3,%ecx
movslq %ecx,%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<first_n_letters>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r15
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
movslq %ebx,%r12
movb   $0x0,(%r14,%r12,1)
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    $0x4,%r12d
jl     1187 <first_n_letters+0x57>
cmp    %ebx,%eax
jle    1187 <first_n_letters+0x57>
lea    -0x3(%rbx),%eax
movb   $0x2e,(%r14,%rax,1)
lea    -0x2(%rbx),%eax
movb   $0x2e,(%r14,%rax,1)
add    $0xffffffff,%ebx
movb   $0x2e,(%r14,%rbx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @first_n_letters(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = add nsw i32 %6, 1
%8 = call i8* @malloc(i32 %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = call i32 @memcpy(i8* %9, i8* %10, i32 %11)
%13 = load i8*, i8** %5
%14 = load i32, i32* %4
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
store i8 0, i8* %16
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = load i32, i32* %4
%20 = icmp sgt i32 %18, %19
br i1 %20, label %21, label %40
21: ; preds = %2
%22 = load i32, i32* %4
%23 = icmp sgt i32 %22, 3
br i1 %23, label %24, label %40
24: ; preds = %21
%25 = load i8*, i8** %5
%26 = load i32, i32* %4
%27 = sub nsw i32 %26, 3
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 46, i8* %29
%30 = load i8*, i8** %5
%31 = load i32, i32* %4
%32 = sub nsw i32 %31, 2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 46, i8* %34
%35 = load i8*, i8** %5
%36 = load i32, i32* %4
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8 46, i8* %39
br label %40
40: ; preds = %24, %21, %2
%41 = load i8*, i8** %5
ret i8* %41
}

LLVM IR (-O2):
define i8* @first_n_letters(i8* %0, i32 %1) {
%3 = add nsw i32 %1, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = tail call i32 @memcpy(i8* %4, i8* %0, i32 %1)
%6 = sext i32 %1 to i64
%7 = getelementptr inbounds i8, i8* %4, i64 %6
store i8 0, i8* %7
%8 = tail call i32 @strlen(i8* %0)
%9 = icmp sgt i32 %8, %1
%10 = icmp sgt i32 %1, 3
%11 = and i1 %10, %9
br i1 %11, label %12, label %22
12: ; preds = %2
%13 = add nsw i32 %1, -3
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %4, i64 %14
store i8 46, i8* %15
%16 = add nsw i32 %1, -2
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %4, i64 %17
store i8 46, i8* %18
%19 = add nsw i32 %1, -1
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %4, i64 %20
store i8 46, i8* %21
br label %22
22: ; preds = %12, %2
ret i8* %4
}