#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*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int main(int argc, char *argv[]) {
  int j, len, n = 500000;
  char *s = NULL, *p;
  if (argc > 1)
    n = atoi(argv[1]);
  for (j = 0; j < 10; j++) {
    free(s);
    s = strdup("abcdef");
    while ((len = strlen(s)) <= n) {
      p = (char *)malloc(2 * len + 10);
      sprintf(p, "123%s456%s789", s, s);
      free(s);
      s = p;
      len = strlen(s);
      p = (char *)malloc(len + 2);
      strcpy(p, s + len / 2);
      strncat(p + len / 2, s, len / 2 + 1);
      free(s);
      s = p;
    }
  }
  printf("%d\n", strlen(s));
  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)
movl   $0x7a120,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11cf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    12d7 <main+0x147>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
lea    0xe10(%rip),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmp    -0x1c(%rbp),%eax
jg     12c4 <main+0x134>
mov    -0x18(%rbp),%edi
shl    $0x1,%edi
add    $0xa,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdd4(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
add    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
add    $0x1,%edx
call   1070 <strncat@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11f9 <main+0x69>
jmp    12c9 <main+0x139>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d6 <main+0x46>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
lea    0xd2c(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x7a120,%r12d
cmp    $0x2,%edi
jl     11b2 <main+0x22>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%r12d
xor    %r14d,%r14d
lea    0xe44(%rip),%rbx
lea    0xe44(%rip),%r15
xor    %r13d,%r13d
jmp    11de <main+0x4e>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14d
cmp    $0xa,%r14d
je     1286 <main+0xf6>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   10b0 <strdup@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    %r12d,%eax
jg     11d0 <main+0x40>
xchg   %ax,%ax
lea    (%rax,%rax,1),%edi
add    $0xa,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %r13,%rcx
call   10a0 <sprintf@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0x2(%rbx),%edi
call   1080 <malloc@plt>
mov    %rax,%r13
mov    %ebx,%eax
shr    $0x1f,%eax
add    %ebx,%eax
sar    %eax
movslq %eax,%rbx
lea    (%rbx,%rbp,1),%rsi
mov    %r13,%rdi
call   1040 <strcpy@plt>
lea    (%rbx,%r13,1),%rdi
add    $0x1,%ebx
mov    %rbp,%rsi
mov    %ebx,%edx
call   1070 <strncat@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
cmp    %r12d,%eax
jle    1200 <main+0x70>
lea    0xd7f(%rip),%rbx
jmp    11d0 <main+0x40>
mov    %r13,%rdi
call   1050 <strlen@plt>
lea    0xd80(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"abcdef\00"
@.str.1 = [14 x i8] c"123%s456%s789\00"
@.str.2 = [4 x i8] c"%d\0A\00"
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 i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 500000, i32* %8
store i8* null, i8** %9
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %8
br label %18
18: ; preds = %13, %2
store i32 0, i32* %6
br label %19
19: ; preds = %71, %18
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 10
br i1 %21, label %22, label %74
22: ; preds = %19
%23 = load i8*, i8** %9
%24 = call i32 @free(i8* %23)
%25 = call i8* @strdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %9
br label %26
26: ; preds = %31, %22
%27 = load i8*, i8** %9
%28 = call i32 @strlen(i8* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %8
%30 = icmp sle i32 %28, %29
br i1 %30, label %31, label %70
31: ; preds = %26
%32 = load i32, i32* %7
%33 = mul nsw i32 2, %32
%34 = add nsw i32 %33, 10
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %10
%37 = load i8*, i8** %10
%38 = load i8*, i8** %9
%39 = load i8*, i8** %9
%40 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %39)
%41 = load i8*, i8** %9
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %10
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = call i32 @strlen(i8* %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 2
%48 = call i64 @malloc(i32 %47)
%49 = inttoptr i64 %48 to i8*
store i8* %49, i8** %10
%50 = load i8*, i8** %10
%51 = load i8*, i8** %9
%52 = load i32, i32* %7
%53 = sdiv i32 %52, 2
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
%56 = call i32 @strcpy(i8* %50, i8* %55)
%57 = load i8*, i8** %10
%58 = load i32, i32* %7
%59 = sdiv i32 %58, 2
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
%62 = load i8*, i8** %9
%63 = load i32, i32* %7
%64 = sdiv i32 %63, 2
%65 = add nsw i32 %64, 1
%66 = call i32 @strncat(i8* %61, i8* %62, i32 %65)
%67 = load i8*, i8** %9
%68 = call i32 @free(i8* %67)
%69 = load i8*, i8** %10
store i8* %69, i8** %9
br label %26
70: ; preds = %26
br label %71
71: ; preds = %70
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %19
74: ; preds = %19
%75 = load i8*, i8** %9
%76 = call i32 @strlen(i8* %75)
%77 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %76)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"abcdef\00"
@.str.1 = [14 x i8] c"123%s456%s789\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
br label %8
8: ; preds = %4, %2
%9 = phi i32 [ %7, %4 ], [ 500000, %2 ]
br label %10
10: ; preds = %8, %42
%11 = phi i32 [ 0, %8 ], [ %44, %42 ]
%12 = phi i8* [ null, %8 ], [ %43, %42 ]
%13 = tail call i32 @free(i8* %12)
%14 = tail call dereferenceable_or_null(7) i8* @strdup(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @strlen(i8* %14)
%16 = icmp sgt i32 %15, %9
br i1 %16, label %42, label %17
17: ; preds = %10, %17
%18 = phi i32 [ %38, %17 ], [ %15, %10 ]
%19 = phi i8* [ %29, %17 ], [ %14, %10 ]
%20 = shl nsw i32 %18, 1
%21 = add nsw i32 %20, 10
%22 = tail call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = tail call i32 @sprintf(i8* dereferenceable(1) %23, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %19, i8* %19)
%25 = tail call i32 @free(i8* %19)
%26 = tail call i32 @strlen(i8* %23)
%27 = add nsw i32 %26, 2
%28 = tail call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = sdiv i32 %26, 2
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %23, i64 %31
%33 = tail call i32 @strcpy(i8* %29, i8* %32)
%34 = getelementptr inbounds i8, i8* %29, i64 %31
%35 = add nsw i32 %30, 1
%36 = tail call i32 @strncat(i8* %34, i8* %23, i32 %35)
%37 = tail call i32 @free(i8* %23)
%38 = tail call i32 @strlen(i8* %29)
%39 = icmp sgt i32 %38, %9
br i1 %39, label %40, label %17
40: ; preds = %17
%41 = inttoptr i64 %28 to i8*
br label %42
42: ; preds = %40, %10
%43 = phi i8* [ %14, %10 ], [ %41, %40 ]
%44 = add nuw nsw i32 %11, 1
%45 = icmp eq i32 %44, 10
br i1 %45, label %46, label %10
46: ; preds = %42
%47 = tail call i32 @strlen(i8* %43)
%48 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %47)
ret i32 0
}