#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 atoi(char *);
int /*<<< orphan*/ server_prompt(int, char *, int);
int /*<<< orphan*/ server_write(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ simple_write(int, char *);
int /*<<< orphan*/ sleep(int);
void test_text(int fd, char *args) {
  switch (atoi(args)) {
  case 1:
    simple_write(fd, "First line\r\n");
    server_prompt(fd, "> ", 2);
    sleep(1);
    server_write(fd, "\r", 2, 0);
    simple_write(fd, "Second line\r\nThere should no longer be a > character "
                     "between the first and second line.\r\n");
    break;
  case 2:
    simple_write(
        fd,
        "\e%@"
        "(iso-8859-1 charset): A single y character, with \" above it: "
        "\377\377\r\n"
        "Word wrapping test. The next line contains non-breaking spaces:\r\n"
        "In\240this\240long\240line\240of\240text,"
        "\240the\240only\240place\240where "
        "space\240is\240used\240is\240before\240the\240first\240space\240word."
        "\r\n");
    break;
  case 3:
    simple_write(
        fd,
        "\e%G"
        "(utf-8 charset): A single a character with \" above it: \xC3\xA4 and "
        "again: a\xCC\x88\r\n"
        "Word wrapping test. The next line contains non-breaking spaces:\r\n"
        "In\302\240this\302\240long\302\240line\302\240of\302\240text,"
        "\302\240the\302\240only\302\240place\302\240where "
        "space\302\240is\302\240used\302\240is\302\240before\302\240the\302\240"
        "first\302\240space\302\240word.\r\n");
    break;
  case 4:
    simple_write(fd, "This test assumes the screen is 80 characters wide.\r\n"
                     "This 80 character line should not be wrapped. The line "
                     "should properly end here.\r\n"
                     "This 81 character line should be wrapped. Xyzzy hocus "
                     "pocus plugh shazam alakazam\r\n"
                     "This 81 character line should also be wrapped. "
                     "Abracadabra plugh plover alakazam.\r\n"
                     "This 81 character line should be wrapped too.  Klaatu "
                     "barada nikto!  Hocus-pocus.\r\n"
                     "This 81 character line should be wrapped as well. Klaatu "
                     "barada nikto hocus-pocus\r\n");
    break;
  case 5:
    simple_write(
        fd,
        "Backspace is destructive NOT!\010\010\010\010\r\n"
        "\r\n"
        "\0103\r\n"
        "\r\n"
        "\r\n"
        "\"3\" should be in the first column and there should be a blank\r\n"
        "line between the \"Backspace is...\" text and the line with \"3\".\r\n"
        "The first line should end with \"NOT!\"\r\n");
    simple_write(fd, "Backspace is destructive NOT!\010\010\010\010\r\n");
    break;
  default:
    simple_write(fd, "Test processing tests:\r\n"
                     "testtext 1 - tests carriage return handling.\r\n"
                     "testtext 2 - tests ISO-8859-1 text handling.\r\n"
                     "testtext 3 - tests UTF-8 text handling.\r\n"
                     "testtext 4 - more word wrapping tests\r\n"
                     "testtext 5 - Backspace testing.\r\n");
  }
}


x86 assembly (clang, -O0):
<test_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <atoi@plt>
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x18(%rbp)
sub    $0x4,%eax
ja     1244 <test_text+0xf4>
mov    -0x18(%rbp),%rax
lea    0xe7b(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x4(%rbp),%edi
lea    0xe7c(%rip),%rsi
call   1030 <simple_write@plt>
mov    -0x4(%rbp),%edi
lea    0xe7a(%rip),%rsi
mov    $0x2,%edx
call   1050 <server_prompt@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    0xe5f(%rip),%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1060 <server_write@plt>
mov    -0x4(%rbp),%edi
lea    0xe4b(%rip),%rsi
call   1030 <simple_write@plt>
jmp    1253 <test_text+0x103>
mov    -0x4(%rbp),%edi
lea    0xe91(%rip),%rsi
call   1030 <simple_write@plt>
jmp    1253 <test_text+0x103>
mov    -0x4(%rbp),%edi
lea    0xf62(%rip),%rsi
call   1030 <simple_write@plt>
jmp    1253 <test_text+0x103>
mov    -0x4(%rbp),%edi
lea    0x104d(%rip),%rsi
call   1030 <simple_write@plt>
jmp    1253 <test_text+0x103>
mov    -0x4(%rbp),%edi
lea    0x120d(%rip),%rsi
call   1030 <simple_write@plt>
mov    -0x4(%rbp),%edi
lea    0x12d2(%rip),%rsi
call   1030 <simple_write@plt>
jmp    1253 <test_text+0x103>
mov    -0x4(%rbp),%edi
lea    0x12e2(%rip),%rsi
call   1030 <simple_write@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_text>:
push   %rbx
mov    %edi,%ebx
mov    %rsi,%rdi
call   1040 <atoi@plt>
add    $0xffffffff,%eax
cmp    $0x4,%eax
ja     11c3 <test_text+0x73>
lea    0xf16(%rip),%rsi
lea    0xe8f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe93(%rip),%rsi
mov    %ebx,%edi
call   1030 <simple_write@plt>
lea    0xe92(%rip),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
call   1050 <server_prompt@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe78(%rip),%rsi
mov    %ebx,%edi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1060 <server_write@plt>
lea    0xe65(%rip),%rsi
jmp    11f3 <test_text+0xa3>
lea    0x1366(%rip),%rsi
jmp    11f3 <test_text+0xa3>
lea    0xf92(%rip),%rsi
jmp    11f3 <test_text+0xa3>
lea    0x1088(%rip),%rsi
jmp    11f3 <test_text+0xa3>
lea    0x1253(%rip),%rsi
mov    %ebx,%edi
call   1030 <simple_write@plt>
lea    0x1319(%rip),%rsi
mov    %ebx,%edi
pop    %rbx
jmp    1030 <simple_write@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"First line\0D\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [2 x i8] c"\0D\00"
@.str.3 = [90 x i8] c"Second line\0D\0AThere should no longer be a > character between the first and second line.\0D\0A\00"
@.str.4 = [229 x i8] c"\1B%@(iso-8859-1 charset): A single y character, with \22 above it: \FF\FF\0D\0AWord wrapping test. The next line contains non-breaking spaces:\0D\0AIn\A0this\A0long\A0line\A0of\A0text,\A0the\A0only\A0place\A0where space\A0is\A0used\A0is\A0before\A0the\A0first\A0space\A0word.\0D\0A\00"
@.str.5 = [255 x i8] c"\1B%G(utf-8 charset): A single a character with \22 above it: \C3\A4 and again: a\CC\88\0D\0AWord wrapping test. The next line contains non-breaking spaces:\0D\0AIn\C2\A0this\C2\A0long\C2\A0line\C2\A0of\C2\A0text,\C2\A0the\C2\A0only\C2\A0place\C2\A0where space\C2\A0is\C2\A0used\C2\A0is\C2\A0before\C2\A0the\C2\A0first\C2\A0space\C2\A0word.\0D\0A\00"
@.str.6 = [468 x i8] c"This test assumes the screen is 80 characters wide.\0D\0AThis 80 character line should not be wrapped. The line should properly end here.\0D\0AThis 81 character line should be wrapped. Xyzzy hocus pocus plugh shazam alakazam\0D\0AThis 81 character line should also be wrapped. Abracadabra plugh plover alakazam.\0D\0AThis 81 character line should be wrapped too. Klaatu barada nikto! Hocus-pocus.\0D\0AThis 81 character line should be wrapped as well. Klaatu barada nikto hocus-pocus\0D\0A\00"
@.str.7 = [212 x i8] c"Backspace is destructive NOT!\08\08\08\08\0D\0A\0D\0A\083\0D\0A\0D\0A\0D\0A\223\22 should be in the first column and there should be a blank\0D\0Aline between the \22Backspace is...\22 text and the line with \223\22.\0D\0AThe first line should end with \22NOT!\22\0D\0A\00"
@.str.8 = [36 x i8] c"Backspace is destructive NOT!\08\08\08\08\0D\0A\00"
@.str.9 = [230 x i8] c"Test processing tests:\0D\0Atesttext 1 - tests carriage return handling.\0D\0Atesttext 2 - tests ISO-8859-1 text handling.\0D\0Atesttext 3 - tests UTF-8 text handling.\0D\0Atesttext 4 - more word wrapping tests\0D\0Atesttext 5 - Backspace testing.\0D\0A\00"
define void @test_text(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = load i8*, i8** %4
%6 = call i32 @atoi(i8* %5)
switch i32 %6, label %31 [
i32 1, label %7
i32 2, label %17
i32 3, label %20
i32 4, label %23
i32 5, label %26
]
7: ; preds = %2
%8 = load i32, i32* %3
%9 = call i32 @simple_write(i32 %8, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %3
%11 = call i32 @server_prompt(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%12 = call i32 @sleep(i32 1)
%13 = load i32, i32* %3
%14 = call i32 @server_write(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 0)
%15 = load i32, i32* %3
%16 = call i32 @simple_write(i32 %15, i8* getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0))
br label %34
17: ; preds = %2
%18 = load i32, i32* %3
%19 = call i32 @simple_write(i32 %18, i8* getelementptr inbounds ([229 x i8], [229 x i8]* @.str.4, i64 0, i64 0))
br label %34
20: ; preds = %2
%21 = load i32, i32* %3
%22 = call i32 @simple_write(i32 %21, i8* getelementptr inbounds ([255 x i8], [255 x i8]* @.str.5, i64 0, i64 0))
br label %34
23: ; preds = %2
%24 = load i32, i32* %3
%25 = call i32 @simple_write(i32 %24, i8* getelementptr inbounds ([468 x i8], [468 x i8]* @.str.6, i64 0, i64 0))
br label %34
26: ; preds = %2
%27 = load i32, i32* %3
%28 = call i32 @simple_write(i32 %27, i8* getelementptr inbounds ([212 x i8], [212 x i8]* @.str.7, i64 0, i64 0))
%29 = load i32, i32* %3
%30 = call i32 @simple_write(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0))
br label %34
31: ; preds = %2
%32 = load i32, i32* %3
%33 = call i32 @simple_write(i32 %32, i8* getelementptr inbounds ([230 x i8], [230 x i8]* @.str.9, i64 0, i64 0))
br label %34
34: ; preds = %31, %26, %23, %20, %17, %7
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"First line\0D\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [2 x i8] c"\0D\00"
@.str.3 = [90 x i8] c"Second line\0D\0AThere should no longer be a > character between the first and second line.\0D\0A\00"
@.str.4 = [229 x i8] c"\1B%@(iso-8859-1 charset): A single y character, with \22 above it: \FF\FF\0D\0AWord wrapping test. The next line contains non-breaking spaces:\0D\0AIn\A0this\A0long\A0line\A0of\A0text,\A0the\A0only\A0place\A0where space\A0is\A0used\A0is\A0before\A0the\A0first\A0space\A0word.\0D\0A\00"
@.str.5 = [255 x i8] c"\1B%G(utf-8 charset): A single a character with \22 above it: \C3\A4 and again: a\CC\88\0D\0AWord wrapping test. The next line contains non-breaking spaces:\0D\0AIn\C2\A0this\C2\A0long\C2\A0line\C2\A0of\C2\A0text,\C2\A0the\C2\A0only\C2\A0place\C2\A0where space\C2\A0is\C2\A0used\C2\A0is\C2\A0before\C2\A0the\C2\A0first\C2\A0space\C2\A0word.\0D\0A\00"
@.str.6 = [468 x i8] c"This test assumes the screen is 80 characters wide.\0D\0AThis 80 character line should not be wrapped. The line should properly end here.\0D\0AThis 81 character line should be wrapped. Xyzzy hocus pocus plugh shazam alakazam\0D\0AThis 81 character line should also be wrapped. Abracadabra plugh plover alakazam.\0D\0AThis 81 character line should be wrapped too. Klaatu barada nikto! Hocus-pocus.\0D\0AThis 81 character line should be wrapped as well. Klaatu barada nikto hocus-pocus\0D\0A\00"
@.str.7 = [212 x i8] c"Backspace is destructive NOT!\08\08\08\08\0D\0A\0D\0A\083\0D\0A\0D\0A\0D\0A\223\22 should be in the first column and there should be a blank\0D\0Aline between the \22Backspace is...\22 text and the line with \223\22.\0D\0AThe first line should end with \22NOT!\22\0D\0A\00"
@.str.8 = [36 x i8] c"Backspace is destructive NOT!\08\08\08\08\0D\0A\00"
@.str.9 = [230 x i8] c"Test processing tests:\0D\0Atesttext 1 - tests carriage return handling.\0D\0Atesttext 2 - tests ISO-8859-1 text handling.\0D\0Atesttext 3 - tests UTF-8 text handling.\0D\0Atesttext 4 - more word wrapping tests\0D\0Atesttext 5 - Backspace testing.\0D\0A\00"
define void @test_text(i32 %0, i8* nocapture %1) {
%3 = tail call i32 @atoi(i8* %1)
switch i32 %3, label %13 [
i32 1, label %4
i32 2, label %14
i32 3, label %9
i32 4, label %10
i32 5, label %11
]
4: ; preds = %2
%5 = tail call i32 @simple_write(i32 %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @server_prompt(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2)
%7 = tail call i32 @sleep(i32 1)
%8 = tail call i32 @server_write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 0)
br label %14
9: ; preds = %2
br label %14
10: ; preds = %2
br label %14
11: ; preds = %2
%12 = tail call i32 @simple_write(i32 %0, i8* getelementptr inbounds ([212 x i8], [212 x i8]* @.str.7, i64 0, i64 0))
br label %14
13: ; preds = %2
br label %14
14: ; preds = %2, %13, %11, %10, %9, %4
%15 = phi i8* [ getelementptr inbounds ([230 x i8], [230 x i8]* @.str.9, i64 0, i64 0), %13 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0), %11 ], [ getelementptr inbounds ([468 x i8], [468 x i8]* @.str.6, i64 0, i64 0), %10 ], [ getelementptr inbounds ([255 x i8], [255 x i8]* @.str.5, i64 0, i64 0), %9 ], [ getelementptr inbounds ([90 x i8], [90 x i8]* @.str.3, i64 0, i64 0), %4 ], [ getelementptr inbounds ([229 x i8], [229 x i8]* @.str.4, i64 0, i64 0), %2 ]
%16 = tail call i32 @simple_write(i32 %0, i8* %15)
ret void
}