#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 EOF;
int get_positive_integer();
int get_string(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
char *strncat(char *, char *, int);
scalar_t__ strncmp(char *, char *, int);
char *strncpy(char *, char *, int);
int main(void) {
  char string1[100], string2[100];
  int length, demo;
  for (;;) {
    printf("Enter string1: ");
    if (get_string(string1) == EOF)
      break;
    printf("Enter string2: ");
    if (get_string(string2) == EOF)
      break;
    printf("Enter length: ");
    if ((length = get_positive_integer()) == EOF)
      break;
    printf("(1) strncpy()\n(2) strncat()\n(3) strncmp()\nChoose which function "
           "to demo: ");
    if ((demo = get_positive_integer()) == EOF)
      break;
    else if (demo == 1)
      printf("strncpy(string1, string2, length)"
             " = [%s]\n",
             (strncpy(string1, string2, length)));
    else if (demo == 2)
      printf("strncat(string1, string2, length)"
             " = [%s]\n",
             (strncat(string1, string2, length)));
    else if (demo == 3) {
      if (strncmp(string1, string2, length))
        printf("The first %d characters of the strings match!\n", length);
      else
        printf("The first %d characters of the strings *do not* match...\n",
               length);
    } else
      printf("Input error\n");
  }
  putchar('\n');
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x70(%rbp),%rdi
call   1060 <get_string@plt>
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%eax
jne    11ad <main+0x3d>
jmp    132c <main+0x1bc>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0xe0(%rbp),%rdi
call   1060 <get_string@plt>
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%eax
jne    11db <main+0x6b>
jmp    132c <main+0x1bc>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1090 <get_positive_integer@plt>
mov    %eax,-0xe4(%rbp)
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%eax
jne    120a <main+0x9a>
jmp    132c <main+0x1bc>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1090 <get_positive_integer@plt>
mov    %eax,-0xe8(%rbp)
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%eax
jne    1239 <main+0xc9>
jmp    132c <main+0x1bc>
cmpl   $0x1,-0xe8(%rbp)
jne    1272 <main+0x102>
lea    -0x70(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
mov    -0xe4(%rbp),%edx
call   1040 <strncpy@plt>
mov    %rax,%rsi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1322 <main+0x1b2>
cmpl   $0x2,-0xe8(%rbp)
jne    12ab <main+0x13b>
lea    -0x70(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
mov    -0xe4(%rbp),%edx
call   1080 <strncat@plt>
mov    %rax,%rsi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    131d <main+0x1ad>
cmpl   $0x3,-0xe8(%rbp)
jne    130a <main+0x19a>
lea    -0x70(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
mov    -0xe4(%rbp),%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
je     12f1 <main+0x181>
mov    -0xe4(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1305 <main+0x195>
mov    -0xe4(%rbp),%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1318 <main+0x1a8>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    131d <main+0x1ad>
jmp    1322 <main+0x1b2>
jmp    1327 <main+0x1b7>
jmp    1182 <main+0x12>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
lea    0xe68(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rsp,%rdi
call   1070 <get_string@plt>
mov    0x2e3a(%rip),%r12
cmp    (%r12),%eax
jne    11d2 <main+0x52>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe37(%rip),%r14
lea    0x70(%rsp),%rbx
lea    0xe3b(%rip),%r15
mov    %rsp,%r13
jmp    1218 <main+0x98>
nopw   0x0(%rax,%rax,1)
lea    0xf3f(%rip),%rdi
call   1060 <puts@plt>
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r13,%rdi
call   1070 <get_string@plt>
cmp    (%r12),%eax
je     11b4 <main+0x34>
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1070 <get_string@plt>
cmp    (%r12),%eax
je     11b4 <main+0x34>
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   10a0 <get_positive_integer@plt>
cmp    (%r12),%eax
je     11b4 <main+0x34>
mov    %eax,%ebp
lea    0xddb(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   10a0 <get_positive_integer@plt>
cmp    (%r12),%eax
je     11b4 <main+0x34>
cmp    $0x3,%eax
je     12a0 <main+0x120>
cmp    $0x2,%eax
je     12c0 <main+0x140>
cmp    $0x1,%eax
jne    11f0 <main+0x70>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
lea    0xde6(%rip),%rdi
jmp    12d4 <main+0x154>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     12e3 <main+0x163>
lea    0xe14(%rip),%rdi
jmp    12ea <main+0x16a>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1090 <strncat@plt>
lea    0xdcf(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    11fc <main+0x7c>
lea    0xe12(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    11fc <main+0x7c>

LLVM IR (-O0):
@.str = [16 x i8] c"Enter string1: \00"
@EOF = global i32 0
@.str.1 = [16 x i8] c"Enter string2: \00"
@.str.2 = [15 x i8] c"Enter length: \00"
@.str.3 = [74 x i8] c"(1) strncpy()\0A(2) strncat()\0A(3) strncmp()\0AChoose which function to demo: \00"
@.str.4 = [42 x i8] c"strncpy(string1, string2, length) = [%s]\0A\00"
@.str.5 = [42 x i8] c"strncat(string1, string2, length) = [%s]\0A\00"
@.str.6 = [47 x i8] c"The first %d characters of the strings match!\0A\00"
@.str.7 = [58 x i8] c"The first %d characters of the strings *do not* match...\0A\00"
@.str.8 = [13 x i8] c"Input error\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %71, %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%9 = call i32 @get_string(i8* %8)
%10 = load i32, i32* @EOF
%11 = icmp eq i32 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %6
br label %72
13: ; preds = %6
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%16 = call i32 @get_string(i8* %15)
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %13
br label %72
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 (...) @get_positive_integer()
store i32 %22, i32* %4
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %20
br label %72
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 (...) @get_positive_integer()
store i32 %28, i32* %5
%29 = load i32, i32* @EOF
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %26
br label %72
32: ; preds = %26
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%38 = load i32, i32* %4
%39 = call i8* @strncpy(i8* %36, i8* %37, i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %39)
br label %70
41: ; preds = %32
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, 2
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%47 = load i32, i32* %4
%48 = call i8* @strncat(i8* %45, i8* %46, i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %48)
br label %69
50: ; preds = %41
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, 3
br i1 %52, label %53, label %66
53: ; preds = %50
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%55 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%56 = load i32, i32* %4
%57 = call i64 @strncmp(i8* %54, i8* %55, i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = load i32, i32* %4
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0), i32 %60)
br label %65
62: ; preds = %53
%63 = load i32, i32* %4
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.7, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %62, %59
br label %68
66: ; preds = %50
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
br label %68
68: ; preds = %66, %65
br label %69
69: ; preds = %68, %44
br label %70
70: ; preds = %69, %35
br label %71
71: ; preds = %70
br label %6
72: ; preds = %31, %25, %19, %12
%73 = call i32 @putchar(i8 signext 10)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"Enter string1: \00"
@EOF = global i32 0
@.str.1 = [16 x i8] c"Enter string2: \00"
@.str.2 = [15 x i8] c"Enter length: \00"
@.str.3 = [74 x i8] c"(1) strncpy()\0A(2) strncat()\0A(3) strncmp()\0AChoose which function to demo: \00"
@.str.4 = [42 x i8] c"strncpy(string1, string2, length) = [%s]\0A\00"
@.str.5 = [42 x i8] c"strncat(string1, string2, length) = [%s]\0A\00"
@.str.6 = [47 x i8] c"The first %d characters of the strings match!\0A\00"
@.str.7 = [58 x i8] c"The first %d characters of the strings *do not* match...\0A\00"
@str = [12 x i8] c"Input error\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @get_string(i8* %3)
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %6, %7
br i1 %8, label %45, label %9
9: ; preds = %0, %40
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%11 = call i32 @get_string(i8* %4)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %45, label %14
14: ; preds = %9
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 (...) @get_positive_integer()
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %45, label %19
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([74 x i8], [74 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 (...) @get_positive_integer()
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %45, label %24
24: ; preds = %19
switch i32 %21, label %38 [
i32 1, label %25
i32 2, label %28
i32 3, label %31
]
25: ; preds = %24
%26 = call i8* @strncpy(i8* %3, i8* %4, i32 %16)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %26)
br label %40
28: ; preds = %24
%29 = call i8* @strncat(i8* %3, i8* %4, i32 %16)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i8* %29)
br label %40
31: ; preds = %24
%32 = call i64 @strncmp(i8* %3, i8* %4, i32 %16)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0), i32 %16)
br label %40
36: ; preds = %31
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.7, i64 0, i64 0), i32 %16)
br label %40
38: ; preds = %24
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %25, %38, %36, %34, %28
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%42 = call i32 @get_string(i8* %3)
%43 = load i32, i32* @EOF
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %9
45: ; preds = %40, %9, %14, %19, %0
%46 = call i32 @putchar(i8 signext 10)
ret i32 0
}