#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int, char);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strlen(char *);
char *countAndSay(int n) {
  char *ret = (char *)calloc(sizeof(char), 8192);
  char *tmp = (char *)calloc(sizeof(char), 8192);
  char buff[8];
  ret[0] = '1';
  if (n == 1)
    return ret;
  n--;
  while (n--) {
    char *p = ret;
    char *move;
    char *pp = tmp;
    while (*p) {
      char say = p[0];
      int count = 0;
      move = p;
      while (move[count] == *p) {
        count++;
      }
      snprintf(buff, 8, "%d%c", count, say);
      strcat(tmp, buff);
      p += count;
    }
    memcpy(ret, tmp, strlen(tmp));
    tmp[0] = 0;
  }
  free(tmp);
  return ret;
}


x86 assembly (clang, -O0):
<countAndSay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x1,%edi
mov    $0x2000,%esi
call   1060 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x1,%edi
mov    $0x2000,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x31,(%rax)
cmpl   $0x1,-0xc(%rbp)
jne    11af <countAndSay+0x4f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12af <countAndSay+0x14f>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     129e <countAndSay+0x13e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     126a <countAndSay+0x10a>
mov    -0x30(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x41(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movslq -0x48(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x30(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    122a <countAndSay+0xca>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1201 <countAndSay+0xa1>
lea    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%ecx
mov    $0x8,%esi
lea    0xdc3(%rip),%rdx
movsbl -0x41(%rbp),%r8d
call   1050 <snprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x48(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11dc <countAndSay+0x7c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    11b8 <countAndSay+0x58>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countAndSay>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    $0x1,%edi
mov    $0x2000,%esi
call   1060 <calloc@plt>
mov    %rax,%rbp
mov    $0x1,%edi
mov    $0x2000,%esi
call   1060 <calloc@plt>
movb   $0x31,0x0(%rbp)
cmp    $0x1,%r14d
je     126f <countAndSay+0x10f>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
add    $0xffffffff,%r14d
mov    %r14d,0x4(%rsp)
mov    $0x31,%al
lea    0xe46(%rip),%r12
lea    0x10(%rsp),%r13
test   %al,%al
je     122f <countAndSay+0xcf>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rcx,%rdx
add    $0x1,%rcx
cmp    %al,0x1(%rbp,%rdx,1)
je     11f0 <countAndSay+0x90>
mov    %ecx,%r14d
lea    (%r14,%rbp,1),%r15
movsbl %al,%r8d
mov    %r13,%rdi
mov    $0x8,%esi
mov    %r12,%rdx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <strcat@plt>
mov    0x0(%rbp,%r14,1),%al
mov    %r15,%rbp
test   %al,%al
jne    11e0 <countAndSay+0x80>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <memcpy@plt>
movb   $0x0,(%rbx)
mov    0x4(%rsp),%eax
add    $0xffffffff,%eax
je     1267 <countAndSay+0x107>
mov    %eax,0x4(%rsp)
movzbl 0x0(%rbp),%eax
test   %al,%al
jne    11d0 <countAndSay+0x70>
jmp    122f <countAndSay+0xcf>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%d%c\00"
define i8* @countAndSay(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [8 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i32
store i32 %0, i32* %3
%12 = call i64 @calloc(i32 1, i32 8192)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %4
%14 = call i64 @calloc(i32 1, i32 8192)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %5
%16 = load i8*, i8** %4
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 49, i8* %17
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = load i8*, i8** %4
store i8* %21, i8** %2
br label %79
22: ; preds = %1
%23 = load i32, i32* %3
%24 = add nsw i32 %23, -1
store i32 %24, i32* %3
br label %25
25: ; preds = %67, %22
%26 = load i32, i32* %3
%27 = add nsw i32 %26, -1
store i32 %27, i32* %3
%28 = icmp ne i32 %26, 0
br i1 %28, label %29, label %75
29: ; preds = %25
%30 = load i8*, i8** %4
store i8* %30, i8** %7
%31 = load i8*, i8** %5
store i8* %31, i8** %9
br label %32
32: ; preds = %55, %29
%33 = load i8*, i8** %7
%34 = load i8, i8* %33
%35 = icmp ne i8 %34, 0
br i1 %35, label %36, label %67
36: ; preds = %32
%37 = load i8*, i8** %7
%38 = getelementptr inbounds i8, i8* %37, i64 0
%39 = load i8, i8* %38
store i8 %39, i8* %10
store i32 0, i32* %11
%40 = load i8*, i8** %7
store i8* %40, i8** %8
br label %41
41: ; preds = %52, %36
%42 = load i8*, i8** %8
%43 = load i32, i32* %11
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = load i8*, i8** %7
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %47, %50
br i1 %51, label %52, label %55
52: ; preds = %41
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
br label %41
55: ; preds = %41
%56 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%57 = load i32, i32* %11
%58 = load i8, i8* %10
%59 = call i32 @snprintf(i8* %56, i32 8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %57, i8 signext %58)
%60 = load i8*, i8** %5
%61 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%62 = call i32 @strcat(i8* %60, i8* %61)
%63 = load i32, i32* %11
%64 = load i8*, i8** %7
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i8, i8* %64, i64 %65
store i8* %66, i8** %7
br label %32
67: ; preds = %32
%68 = load i8*, i8** %4
%69 = load i8*, i8** %5
%70 = load i8*, i8** %5
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @memcpy(i8* %68, i8* %69, i32 %71)
%73 = load i8*, i8** %5
%74 = getelementptr inbounds i8, i8* %73, i64 0
store i8 0, i8* %74
br label %25
75: ; preds = %25
%76 = load i8*, i8** %5
%77 = call i32 @free(i8* %76)
%78 = load i8*, i8** %4
store i8* %78, i8** %2
br label %79
79: ; preds = %75, %20
%80 = load i8*, i8** %2
ret i8* %80
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d%c\00"
define i8* @countAndSay(i32 %0) {
%2 = alloca [8 x i8]
%3 = tail call i64 @calloc(i32 1, i32 8192)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @calloc(i32 1, i32 8192)
%6 = inttoptr i64 %5 to i8*
%7 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
store i8 49, i8* %4
%8 = icmp eq i32 %0, 1
br i1 %8, label %41, label %9
9: ; preds = %1
%10 = add nsw i32 %0, -1
br label %11
11: ; preds = %37, %9
%12 = phi i8 [ %38, %37 ], [ 49, %9 ]
%13 = phi i32 [ %35, %37 ], [ %10, %9 ]
%14 = icmp eq i8 %12, 0
br i1 %14, label %32, label %15
15: ; preds = %11, %24
%16 = phi i8 [ %30, %24 ], [ %12, %11 ]
%17 = phi i8* [ %27, %24 ], [ %4, %11 ]
br label %18
18: ; preds = %15, %18
%19 = phi i64 [ 1, %15 ], [ %23, %18 ]
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, %16
%23 = add nuw i64 %19, 1
br i1 %22, label %18, label %24
24: ; preds = %18
%25 = trunc i64 %19 to i32
%26 = and i64 %19, 4294967295
%27 = getelementptr inbounds i8, i8* %17, i64 %26
%28 = call i32 @snprintf(i8* %7, i32 8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %25, i8 signext %16)
%29 = call i32 @strcat(i8* %6, i8* %7)
%30 = load i8, i8* %27
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %15
32: ; preds = %24, %11
%33 = call i32 @strlen(i8* %6)
%34 = call i32 @memcpy(i8* %4, i8* %6, i32 %33)
store i8 0, i8* %6
%35 = add nsw i32 %13, -1
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %32
%38 = load i8, i8* %4
br label %11
39: ; preds = %32
%40 = call i32 @free(i8* %6)
br label %41
41: ; preds = %1, %39
ret i8* %4
}