#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int isValidArg(char *);
scalar_t__ isalpha(char);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ replace(char *, char *, char *);
int /*<<< orphan*/ sortArray(char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int main(int argc, char *argv[]) {
  if (argc < 3 || argc > 4) {
    fprintf(stderr,
            "%s Expected either 2 or 3 arguments but instead received %d \n",
            argv[0], argc - 1);
    exit(EXIT_FAILURE);
  } else {
    int display = 1;
    for (int i = 1; i < argc; i++) {
      int length = strlen(argv[i]);
      for (int j = 0; j < length; j++) {
        if (isalpha(argv[i][j]) && display == 1) {
          printf("Expected only digits for each argument \n");
          display = 0;
        }
      }
    }
    bool is1Valid = isValidArg(argv[1]);
    bool is2Valid = isValidArg(argv[2]);
    if (is1Valid && is2Valid) {
      sortArray(argv[1], argv[2]);
    }
    printf("\n\n");
    printf("STRING REPLACE \n");
    printf("[Argv1] replaces [Argv2] in string [Argv3] \n\n");
    if (argc == 3) {
      printf("Requires 3 arguments but was given only %d arguments \n",
             argc - 1);
    } else {
      replace(argv[1], argv[2], argv[3]);
    }
    printf("\n");
    exit(EXIT_SUCCESS);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11aa <main+0x2a>
cmpl   $0x4,-0x8(%rbp)
jle    11da <main+0x5a>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movl   $0x1,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1283 <main+0x103>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1270 <main+0xf0>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1070 <isalpha@plt>
cmp    $0x0,%rax
je     125d <main+0xdd>
cmpl   $0x1,-0x14(%rbp)
jne    125d <main+0xdd>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
jmp    1262 <main+0xe2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    120f <main+0x8f>
jmp    1275 <main+0xf5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11e8 <main+0x68>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <isValidArg@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <isValidArg@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12cc <main+0x14c>
cmpl   $0x0,-0x28(%rbp)
je     12cc <main+0x14c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1050 <sortArray@plt>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x3,-0x8(%rbp)
jne    1319 <main+0x199>
mov    -0x8(%rbp),%esi
sub    $0x1,%esi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1336 <main+0x1b6>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
call   1080 <replace@plt>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %edi,%ecx
lea    -0x5(%rcx),%eax
cmp    $0xfffffffe,%eax
jae    11e5 <main+0x45>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%rdx
add    $0xffffffff,%ecx
lea    0xe2e(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %rcx,0x8(%rsp)
mov    %ecx,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%ebx
mov    $0x1,%ebp
lea    0xeb0(%rip),%r15
jmp    121b <main+0x7b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbp
cmp    0x10(%rsp),%rbp
je     126e <main+0xce>
mov    0x0(%r13,%rbp,8),%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    1210 <main+0x70>
mov    %eax,%r14d
xor    %r12d,%r12d
jmp    1249 <main+0xa9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r12
cmp    %r12,%r14
je     1210 <main+0x70>
mov    0x0(%r13,%rbp,8),%rax
movsbl (%rax,%r12,1),%edi
call   1090 <isalpha@plt>
test   %rax,%rax
je     1240 <main+0xa0>
cmp    $0x1,%ebx
jne    1240 <main+0xa0>
mov    %r15,%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
jmp    1240 <main+0xa0>
mov    0x8(%r13),%rdi
call   10c0 <isValidArg@plt>
mov    %eax,%ebp
mov    0x10(%r13),%rdi
call   10c0 <isValidArg@plt>
test   %ebp,%ebp
je     1297 <main+0xf7>
test   %eax,%eax
je     1297 <main+0xf7>
mov    0x8(%r13),%rdi
mov    0x10(%r13),%rsi
call   1070 <sortArray@plt>
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
lea    0xdcb(%rip),%rdi
call   1040 <puts@plt>
lea    0xdcf(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x3,0x8(%rsp)
jne    12d7 <main+0x137>
lea    0xd75(%rip),%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12e8 <main+0x148>
mov    0x8(%r13),%rdi
mov    0x10(%r13),%rsi
mov    0x18(%r13),%rdx
call   10a0 <replace@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [62 x i8] c"%s Expected either 2 or 3 arguments but instead received %d \0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [41 x i8] c"Expected only digits for each argument \0A\00"
@.str.2 = [3 x i8] c"\0A\0A\00"
@.str.3 = [17 x i8] c"STRING REPLACE \0A\00"
@.str.4 = [46 x i8] c"[Argv1] replaces [Argv2] in string [Argv3] \0A\0A\00"
@.str.5 = [55 x i8] c"Requires 3 arguments but was given only %d arguments \0A\00"
@.str.6 = [2 x i8] c"\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 4
br i1 %16, label %17, label %27
17: ; preds = %14, %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = load i32, i32* %4
%23 = sub nsw i32 %22, 1
%24 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %14
store i32 1, i32* %6
store i32 1, i32* %7
br label %28
28: ; preds = %65, %27
%29 = load i32, i32* %7
%30 = load i32, i32* %4
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %68
32: ; preds = %28
%33 = load i8**, i8*** %5
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8*, i8** %33, i64 %35
%37 = load i8*, i8** %36
%38 = call i32 @strlen(i8* %37)
store i32 %38, i32* %8
store i32 0, i32* %9
br label %39
39: ; preds = %61, %32
%40 = load i32, i32* %9
%41 = load i32, i32* %8
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %64
43: ; preds = %39
%44 = load i8**, i8*** %5
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %44, i64 %46
%48 = load i8*, i8** %47
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
%52 = load i8, i8* %51
%53 = call i64 @isalpha(i8 signext %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %43
%56 = load i32, i32* %6
%57 = icmp eq i32 %56, 1
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
br label %60
60: ; preds = %58, %55, %43
br label %61
61: ; preds = %60
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %39
64: ; preds = %39
br label %65
65: ; preds = %64
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
br label %28
68: ; preds = %28
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 1
%71 = load i8*, i8** %70
%72 = call i32 @isValidArg(i8* %71)
store i32 %72, i32* %10
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 2
%75 = load i8*, i8** %74
%76 = call i32 @isValidArg(i8* %75)
store i32 %76, i32* %11
%77 = load i32, i32* %10
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %90
79: ; preds = %68
%80 = load i32, i32* %11
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %90
82: ; preds = %79
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 1
%85 = load i8*, i8** %84
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 2
%88 = load i8*, i8** %87
%89 = call i32 @sortArray(i8* %85, i8* %88)
br label %90
90: ; preds = %82, %79, %68
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
%94 = load i32, i32* %4
%95 = icmp eq i32 %94, 3
br i1 %95, label %96, label %100
96: ; preds = %90
%97 = load i32, i32* %4
%98 = sub nsw i32 %97, 1
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 %98)
br label %111
100: ; preds = %90
%101 = load i8**, i8*** %5
%102 = getelementptr inbounds i8*, i8** %101, i64 1
%103 = load i8*, i8** %102
%104 = load i8**, i8*** %5
%105 = getelementptr inbounds i8*, i8** %104, i64 2
%106 = load i8*, i8** %105
%107 = load i8**, i8*** %5
%108 = getelementptr inbounds i8*, i8** %107, i64 3
%109 = load i8*, i8** %108
%110 = call i32 @replace(i8* %103, i8* %106, i8* %109)
br label %111
111: ; preds = %100, %96
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%113 = load i32, i32* @EXIT_SUCCESS
%114 = call i32 @exit(i32 %113)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [62 x i8] c"%s Expected either 2 or 3 arguments but instead received %d \0A\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [55 x i8] c"Requires 3 arguments but was given only %d arguments \0A\00"
@EXIT_SUCCESS = global i32 0
@str = [2 x i8] c"\0A\00"
@str.7 = [16 x i8] c"STRING REPLACE \00"
@str.8 = [45 x i8] c"[Argv1] replaces [Argv2] in string [Argv3] \0A\00"
@str.9 = [40 x i8] c"Expected only digits for each argument \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -2
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = zext i32 %0 to i64
br label %24
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = add nsw i32 %0, -1
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %33
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i32 @isValidArg(i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i32 @isValidArg(i8* %19)
%21 = icmp ne i32 %17, 0
%22 = icmp ne i32 %20, 0
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %53, label %57
24: ; preds = %5, %33
%25 = phi i64 [ 1, %5 ], [ %35, %33 ]
%26 = phi i32 [ 1, %5 ], [ %34, %33 ]
%27 = getelementptr inbounds i8*, i8** %1, i64 %25
%28 = load i8*, i8** %27
%29 = tail call i32 @strlen(i8* %28)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = zext i32 %29 to i64
br label %37
33: ; preds = %49, %24
%34 = phi i32 [ %26, %24 ], [ %50, %49 ]
%35 = add nuw nsw i64 %25, 1
%36 = icmp eq i64 %35, %6
br i1 %36, label %14, label %24
37: ; preds = %31, %49
%38 = phi i64 [ 0, %31 ], [ %51, %49 ]
%39 = phi i32 [ %26, %31 ], [ %50, %49 ]
%40 = load i8*, i8** %27
%41 = getelementptr inbounds i8, i8* %40, i64 %38
%42 = load i8, i8* %41
%43 = tail call i64 @isalpha(i8 signext %42)
%44 = icmp ne i64 %43, 0
%45 = icmp eq i32 %39, 1
%46 = select i1 %44, i1 %45, i1 false
br i1 %46, label %47, label %49
47: ; preds = %37
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.9, i64 0, i64 0))
br label %49
49: ; preds = %37, %47
%50 = phi i32 [ 0, %47 ], [ %39, %37 ]
%51 = add nuw nsw i64 %38, 1
%52 = icmp eq i64 %51, %32
br i1 %52, label %33, label %37
53: ; preds = %14
%54 = load i8*, i8** %15
%55 = load i8*, i8** %18
%56 = tail call i32 @sortArray(i8* %54, i8* %55)
br label %57
57: ; preds = %53, %14
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.8, i64 0, i64 0))
%61 = icmp eq i32 %0, 3
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.5, i64 0, i64 0), i32 2)
br label %70
64: ; preds = %57
%65 = load i8*, i8** %15
%66 = load i8*, i8** %18
%67 = getelementptr inbounds i8*, i8** %1, i64 3
%68 = load i8*, i8** %67
%69 = tail call i32 @replace(i8* %65, i8* %66, i8* %68)
br label %70
70: ; preds = %64, %62
%71 = tail call i32 @putchar(i32 10)
%72 = load i32, i32* @EXIT_SUCCESS
%73 = tail call i32 @exit(i32 %72)
unreachable
}