#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*/ Tui_Gotoxy(int, int);
int /*<<< orphan*/ Tui_Textbackground(int);
int /*<<< orphan*/ Tui_Textcolor(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int Tui_Entry(int x, int y, char *buffer, int size, int forecolor,
              int backcolor) {
  char *entry;
  int length;
  int i;
  entry = (char *)malloc(sizeof(char) * 255);
  memset(entry, '\0', 255);
  Tui_Textcolor(forecolor);
  Tui_Textbackground(backcolor);
  Tui_Gotoxy(x, y);
  for (i = 0; i < size; i++) {
    putchar(' ');
  }
  Tui_Gotoxy(x, y);
  fgets(entry, 255, stdin);
  if (strlen(entry) > 0) {
    entry[strlen(entry) - 1] = '\0';
  }
  length = strlen(entry);
  if (length <= size) {
    strcpy(buffer, entry);
  } else {
    strncpy(buffer, entry, size);
    length = size;
  }
  free(entry);
  return (length);
}


x86 assembly (clang, -O0):
<Tui_Entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
mov    $0xff,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1090 <memset@plt>
mov    -0x18(%rbp),%edi
call   1080 <Tui_Textcolor@plt>
mov    -0x1c(%rbp),%edi
call   10d0 <Tui_Textbackground@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10b0 <Tui_Gotoxy@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1231 <Tui_Entry+0x81>
mov    $0x20,%edi
call   1040 <putchar@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    120d <Tui_Entry+0x5d>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   10b0 <Tui_Gotoxy@plt>
mov    -0x28(%rbp),%rdi
mov    0x2da1(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   10a0 <fgets@plt>
mov    -0x28(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%eax
jle    1286 <Tui_Entry+0xd6>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     12b0 <Tui_Entry+0x100>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <strcpy@plt>
jmp    12c6 <Tui_Entry+0x116>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Tui_Entry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%ebp
mov    %r8d,%r14d
mov    %ecx,%r13d
mov    %rdx,(%rsp)
mov    %esi,%r15d
mov    %edi,%r12d
mov    $0xff,%edi
call   10c0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1090 <memset@plt>
mov    %r14d,%edi
call   1080 <Tui_Textcolor@plt>
mov    %ebp,%edi
call   10d0 <Tui_Textbackground@plt>
mov    %r12d,%edi
mov    %r15d,%esi
call   10b0 <Tui_Gotoxy@plt>
test   %r13d,%r13d
jle    121f <Tui_Entry+0x6f>
mov    %r13d,%ebp
nopl   0x0(%rax)
mov    $0x20,%edi
call   1040 <putchar@plt>
add    $0xffffffff,%ebp
jne    1210 <Tui_Entry+0x60>
mov    %r12d,%edi
mov    %r15d,%esi
call   10b0 <Tui_Gotoxy@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xff,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
jle    125b <Tui_Entry+0xab>
mov    %rbx,%rdi
call   1070 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1070 <strlen@plt>
cmp    %r13d,%eax
jle    1279 <Tui_Entry+0xc9>
mov    (%rsp),%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <strncpy@plt>
jmp    128a <Tui_Entry+0xda>
mov    %eax,%ebp
mov    (%rsp),%rdi
mov    %rbx,%rsi
call   1060 <strcpy@plt>
mov    %ebp,%r13d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
define i32 @Tui_Entry(i32 %0, i32 %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%16 = call i64 @malloc(i32 255)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %13
%18 = load i8*, i8** %13
%19 = call i32 @memset(i8* %18, i8 signext 0, i32 255)
%20 = load i32, i32* %11
%21 = call i32 @Tui_Textcolor(i32 %20)
%22 = load i32, i32* %12
%23 = call i32 @Tui_Textbackground(i32 %22)
%24 = load i32, i32* %7
%25 = load i32, i32* %8
%26 = call i32 @Tui_Gotoxy(i32 %24, i32 %25)
store i32 0, i32* %15
br label %27
27: ; preds = %33, %6
%28 = load i32, i32* %15
%29 = load i32, i32* %10
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = call i32 @putchar(i8 signext 32)
br label %33
33: ; preds = %31
%34 = load i32, i32* %15
%35 = add nsw i32 %34, 1
store i32 %35, i32* %15
br label %27
36: ; preds = %27
%37 = load i32, i32* %7
%38 = load i32, i32* %8
%39 = call i32 @Tui_Gotoxy(i32 %37, i32 %38)
%40 = load i8*, i8** %13
%41 = load i32, i32* @stdin
%42 = call i32 @fgets(i8* %40, i32 255, i32 %41)
%43 = load i8*, i8** %13
%44 = call i32 @strlen(i8* %43)
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %36
%47 = load i8*, i8** %13
%48 = load i8*, i8** %13
%49 = call i32 @strlen(i8* %48)
%50 = sub nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %47, i64 %51
store i8 0, i8* %52
br label %53
53: ; preds = %46, %36
%54 = load i8*, i8** %13
%55 = call i32 @strlen(i8* %54)
store i32 %55, i32* %14
%56 = load i32, i32* %14
%57 = load i32, i32* %10
%58 = icmp sle i32 %56, %57
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = load i8*, i8** %9
%61 = load i8*, i8** %13
%62 = call i32 @strcpy(i8* %60, i8* %61)
br label %69
63: ; preds = %53
%64 = load i8*, i8** %9
%65 = load i8*, i8** %13
%66 = load i32, i32* %10
%67 = call i32 @strncpy(i8* %64, i8* %65, i32 %66)
%68 = load i32, i32* %10
store i32 %68, i32* %14
br label %69
69: ; preds = %63, %59
%70 = load i8*, i8** %13
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* %14
ret i32 %72
}

LLVM IR (-O2):
@stdin = global i32 0
define i32 @Tui_Entry(i32 %0, i32 %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i64 @malloc(i32 255)
%8 = inttoptr i64 %7 to i8*
%9 = tail call i32 @memset(i8* %8, i8 signext 0, i32 255)
%10 = tail call i32 @Tui_Textcolor(i32 %4)
%11 = tail call i32 @Tui_Textbackground(i32 %5)
%12 = tail call i32 @Tui_Gotoxy(i32 %0, i32 %1)
%13 = icmp sgt i32 %3, 0
br i1 %13, label %14, label %19
14: ; preds = %6, %14
%15 = phi i32 [ %17, %14 ], [ 0, %6 ]
%16 = tail call i32 @putchar(i8 signext 32)
%17 = add nuw nsw i32 %15, 1
%18 = icmp eq i32 %17, %3
br i1 %18, label %19, label %14
19: ; preds = %14, %6
%20 = tail call i32 @Tui_Gotoxy(i32 %0, i32 %1)
%21 = load i32, i32* @stdin
%22 = tail call i32 @fgets(i8* %8, i32 255, i32 %21)
%23 = tail call i32 @strlen(i8* %8)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = tail call i32 @strlen(i8* %8)
%27 = add nsw i32 %26, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %8, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %25, %19
%31 = tail call i32 @strlen(i8* %8)
%32 = icmp sgt i32 %31, %3
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 @strcpy(i8* %2, i8* %8)
br label %37
35: ; preds = %30
%36 = tail call i32 @strncpy(i8* %2, i8* %8, i32 %3)
br label %37
37: ; preds = %35, %33
%38 = phi i32 [ %31, %33 ], [ %3, %35 ]
%39 = tail call i32 @free(i8* %8)
ret i32 %38
}