#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 */
char generate_char(size_t, char *);
int /*<<< orphan*/ prepare_stream(size_t, int *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int scanf(char *, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(void) {
  printf("Enter how many chars you want to generate, the size of P, the values "
         "in P (0-100),\n"
         "and the chars in C\n");
  printf("> ");
  srand(time(NULL));
  size_t to_generate;
  size_t n;
  while (scanf("%zu%zu", &to_generate, &n) == 2) {
    int distr[n];
    char chars_distr[n];
    size_t i;
    for (i = 0; i < n; i++) {
      scanf("%d", &distr[i]);
    }
    for (i = 0; i < n; i++) {
      scanf(" %c", &chars_distr[i]);
    }
    prepare_stream(n, distr);
    for (i = 0; i < to_generate; i++) {
      putchar(generate_char(n, chars_distr));
      putchar('\n');
    }
    printf("> ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
lea    0xed5(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
lea    0xebc(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1080 <scanf@plt>
cmp    $0x2,%eax
jne    12fa <main+0x18a>
mov    -0x18(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1252 <main+0xe2>
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1216 <main+0xa6>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    128f <main+0x11f>
mov    -0x40(%rbp),%rsi
add    -0x38(%rbp),%rsi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    125a <main+0xea>
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%rdi
call   1090 <prepare_stream@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    12e2 <main+0x172>
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdi
call   1050 <generate_char@plt>
movsbl %al,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    12a4 <main+0x134>
lea    0xd7e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
jmp    11a7 <main+0x37>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe79(%rip),%rdi
call   1040 <puts@plt>
lea    0xe5c(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
lea    0xe45(%rip),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
call   1090 <scanf@plt>
cmp    $0x2,%eax
jne    12ed <main+0x16d>
lea    0xe2d(%rip),%r14
jmp    120f <main+0x8f>
nop
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
mov    -0x40(%rbp),%rsp
lea    0xe0c(%rip),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
call   1090 <scanf@plt>
cmp    $0x2,%eax
jne    12ed <main+0x16d>
mov    %rsp,%rdx
mov    -0x30(%rbp),%rax
mov    %rsp,%r13
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    -0x30(%rbp),%rax
mov    %rsp,%r15
lea    0xf(%rax),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
test   %rax,%rax
mov    %rdx,-0x40(%rbp)
je     12a0 <main+0x120>
mov    %r13,%r12
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1090 <scanf@plt>
add    $0x1,%rbx
mov    -0x30(%rbp),%rax
add    $0x4,%r12
cmp    %rax,%rbx
jb     1250 <main+0xd0>
test   %rax,%rax
lea    0xd95(%rip),%r12
je     12a0 <main+0x120>
xor    %ebx,%ebx
nopl   0x0(%rax)
lea    (%r15,%rbx,1),%rsi
mov    %r12,%rdi
xor    %eax,%eax
call   1090 <scanf@plt>
add    $0x1,%rbx
mov    -0x30(%rbp),%rdi
cmp    %rdi,%rbx
jb     1280 <main+0x100>
jmp    12a2 <main+0x122>
nopl   (%rax)
xor    %edi,%edi
mov    %r13,%rsi
call   10a0 <prepare_stream@plt>
cmpq   $0x0,-0x38(%rbp)
je     11e0 <main+0x60>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    -0x30(%rbp),%rdi
mov    %r15,%rsi
call   1060 <generate_char@plt>
movsbl %al,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    -0x38(%rbp),%rbx
jb     12c0 <main+0x140>
jmp    11e0 <main+0x60>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [103 x i8] c"Enter how many chars you want to generate, the size of P, the values in P (0-100),\0Aand the chars in C\0A\00"
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [7 x i8] c"%zu%zu\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [4 x i8] c" %c\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %1
%8 = call i32 @printf(i8* getelementptr inbounds ([103 x i8], [103 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @time(i32* null)
%11 = call i32 @srand(i32 %10)
br label %12
12: ; preds = %59, %0
%13 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %2, i64* %3)
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %62
15: ; preds = %12
%16 = load i64, i64* %3
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %4
%18 = alloca i32, i64 %16
store i64 %16, i64* %5
%19 = load i64, i64* %3
%20 = alloca i8, i64 %19
store i64 %19, i64* %6
store i64 0, i64* %7
br label %21
21: ; preds = %29, %15
%22 = load i64, i64* %7
%23 = load i64, i64* %3
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i64, i64* %7
%27 = getelementptr inbounds i32, i32* %18, i64 %26
%28 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %27)
br label %29
29: ; preds = %25
%30 = load i64, i64* %7
%31 = add i64 %30, 1
store i64 %31, i64* %7
br label %21
32: ; preds = %21
store i64 0, i64* %7
br label %33
33: ; preds = %41, %32
%34 = load i64, i64* %7
%35 = load i64, i64* %3
%36 = icmp ult i64 %34, %35
br i1 %36, label %37, label %44
37: ; preds = %33
%38 = load i64, i64* %7
%39 = getelementptr inbounds i8, i8* %20, i64 %38
%40 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %37
%42 = load i64, i64* %7
%43 = add i64 %42, 1
store i64 %43, i64* %7
br label %33
44: ; preds = %33
%45 = load i64, i64* %3
%46 = call i32 @prepare_stream(i64 %45, i32* %18)
store i64 0, i64* %7
br label %47
47: ; preds = %56, %44
%48 = load i64, i64* %7
%49 = load i64, i64* %2
%50 = icmp ult i64 %48, %49
br i1 %50, label %51, label %59
51: ; preds = %47
%52 = load i64, i64* %3
%53 = call signext i8 @generate_char(i64 %52, i8* %20)
%54 = call i32 @putchar(i8 signext %53)
%55 = call i32 @putchar(i8 signext 10)
br label %56
56: ; preds = %51
%57 = load i64, i64* %7
%58 = add i64 %57, 1
store i64 %58, i64* %7
br label %47
59: ; preds = %47
%60 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%61 = load i8*, i8** %4
br label %12
62: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"> \00"
@.str.2 = [7 x i8] c"%zu%zu\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [4 x i8] c" %c\00"
@str = [102 x i8] c"Enter how many chars you want to generate, the size of P, the values in P (0-100),\0Aand the chars in C\00"
define i32 @main() {
%1 = alloca i64
%2 = alloca i64
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([102 x i8], [102 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @time(i32* null)
%6 = tail call i32 @srand(i32 %5)
%7 = bitcast i64* %1 to i8*
%8 = bitcast i64* %2 to i8*
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %1, i64* %2)
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %52
11: ; preds = %0, %48
%12 = load i64, i64* %2
%13 = call i8* @llvm.stacksave()
%14 = alloca i32, i64 %12
%15 = load i64, i64* %2
%16 = alloca i8, i64 %15
%17 = icmp eq i64 %15, 0
br i1 %17, label %34, label %20
18: ; preds = %20
%19 = icmp eq i64 %25, 0
br i1 %19, label %34, label %27
20: ; preds = %11, %20
%21 = phi i64 [ %24, %20 ], [ 0, %11 ]
%22 = getelementptr inbounds i32, i32* %14, i64 %21
%23 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %22)
%24 = add nuw i64 %21, 1
%25 = load i64, i64* %2
%26 = icmp ult i64 %24, %25
br i1 %26, label %20, label %18
27: ; preds = %18, %27
%28 = phi i64 [ %31, %27 ], [ 0, %18 ]
%29 = getelementptr inbounds i8, i8* %16, i64 %28
%30 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %29)
%31 = add nuw i64 %28, 1
%32 = load i64, i64* %2
%33 = icmp ult i64 %31, %32
br i1 %33, label %27, label %34
34: ; preds = %27, %11, %18
%35 = phi i64 [ 0, %18 ], [ 0, %11 ], [ %32, %27 ]
%36 = call i32 @prepare_stream(i64 %35, i32* %14)
%37 = load i64, i64* %1
%38 = icmp eq i64 %37, 0
br i1 %38, label %48, label %39
39: ; preds = %34, %39
%40 = phi i64 [ %45, %39 ], [ 0, %34 ]
%41 = load i64, i64* %2
%42 = call signext i8 @generate_char(i64 %41, i8* %16)
%43 = call i32 @putchar(i8 signext %42)
%44 = call i32 @putchar(i8 signext 10)
%45 = add nuw i64 %40, 1
%46 = load i64, i64* %1
%47 = icmp ult i64 %45, %46
br i1 %47, label %39, label %48
48: ; preds = %39, %34
%49 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i64* %1, i64* %2)
%51 = icmp eq i32 %50, 2
br i1 %51, label %11, label %52
52: ; preds = %48, %0
ret i32 0
}