#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 */
int /*<<< orphan*/ A_BOLD;
int /*<<< orphan*/ attroff(int /*<<< orphan*/);
int /*<<< orphan*/ attron(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ mvprintw(int, int, char *, ...);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
void print_up_to(char *buf, int y, int x, size_t max) {
  size_t copy_this_much;
  size_t src_len = strlen(buf);
  if (src_len <= max) {
    copy_this_much = max;
  } else {
    copy_this_much = max - 3;
  }
  char *print_buf = malloc(max + 1);
  memset(print_buf, 0, max + 1);
  strncpy(print_buf, buf, copy_this_much);
  mvprintw(y, x, "%s", print_buf);
  if (src_len > max) {
    attron(A_BOLD);
    mvprintw(y, x + copy_this_much, "...");
    attroff(A_BOLD);
  }
  free(print_buf);
}


x86 assembly (clang, -O0):
<print_up_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11be <print_up_to+0x3e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ca <print_up_to+0x4a>
mov    -0x18(%rbp),%rax
sub    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <strncpy@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x30(%rbp),%rcx
lea    0xdf0(%rip),%rdx
mov    $0x0,%al
call   10a0 <mvprintw@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    125c <print_up_to+0xdc>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1080 <attron@plt>
mov    -0xc(%rbp),%edi
movslq -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xdbc(%rip),%rdx
mov    $0x0,%al
call   10a0 <mvprintw@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1030 <attroff@plt>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_up_to>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %edx,(%rsp)
mov    %esi,0x4(%rsp)
mov    %rdi,%r14
call   1060 <strlen@plt>
mov    %rax,%r13
lea    -0x3(%rbx),%rbp
cmp    %rbx,%rax
cmovbe %rbx,%rbp
lea    0x1(%rbx),%r15
mov    %r15,%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
mov    (%rsp),%r15d
call   1070 <memset@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    0x4(%rsp),%r14d
mov    %rbp,%rdx
call   1050 <strncpy@plt>
lea    0xe1b(%rip),%rdx
mov    %r14d,%edi
mov    %r15d,%esi
mov    %r12,%rcx
xor    %eax,%eax
call   10a0 <mvprintw@plt>
cmp    %rbx,%r13
jbe    1225 <print_up_to+0xa5>
mov    0x2ddf(%rip),%rbx
mov    (%rbx),%edi
call   1080 <attron@plt>
add    %r15d,%ebp
lea    0xdf1(%rip),%rdx
mov    %r14d,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   10a0 <mvprintw@plt>
mov    (%rbx),%edi
call   1030 <attroff@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@A_BOLD = global i32 0
@.str.1 = [4 x i8] c"...\00"
define void @print_up_to(i8* %0, i32 %1, i32 %2, i64 %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = icmp ugt i64 %5, %3
%7 = add i64 %3, -3
%8 = select i1 %6, i64 %7, i64 %3
%9 = add i64 %3, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = tail call i32 @memset(i8* %10, i32 0, i64 %9)
%12 = tail call i32 @strncpy(i8* %10, i8* %0, i64 %8)
%13 = tail call i32 (i32, i32, i8*, ...) @mvprintw(i32 %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %10)
br i1 %6, label %14, label %22
14: ; preds = %4
%15 = load i32, i32* @A_BOLD
%16 = tail call i32 @attron(i32 %15)
%17 = trunc i64 %8 to i32
%18 = add i32 %17, %2
%19 = tail call i32 (i32, i32, i8*, ...) @mvprintw(i32 %1, i32 %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @A_BOLD
%21 = tail call i32 @attroff(i32 %20)
br label %22
22: ; preds = %14, %4
%23 = tail call i32 @free(i8* %10)
ret void
}