#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*/ append_string(char *, char *);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int strlen(char *);
void test_append_string() {
  char string1[(4096 * 4)];
  char string2[(4096 * 4)];
  char expected[(4096 * 4)];
  int i;
  printf("test_append_string...");
  sprintf(string1, "%s", "string1");
  sprintf(string1, "%s", "string2");
  sprintf(expected, "%s\n%s", string1, string2);
  append_string(string2, string1);
  assert(strlen(string1) == strlen(expected));
  for (i = 0; i < strlen(string1); i++) {
    assert(string1[i] == expected[i]);
  }
  printf("Ok\n");
}


x86 assembly (clang, -O0):
<test_append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc010,%rsp
lea    0xe9e(%rip),%rdi
call   1040 <printf@plt>
lea    -0x4000(%rbp),%rdi
lea    0xeb4(%rip),%rsi
lea    0xe9a(%rip),%rdx
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x4000(%rbp),%rdi
lea    0xe98(%rip),%rsi
lea    0xe86(%rip),%rdx
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0xc000(%rbp),%rdi
lea    -0x4000(%rbp),%rdx
lea    -0x8000(%rbp),%rcx
lea    0xe6b(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x8000(%rbp),%rdi
lea    -0x4000(%rbp),%rsi
call   1050 <append_string@plt>
lea    -0x4000(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xc008(%rbp)
lea    -0xc000(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0xc008(%rbp),%eax
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movl   $0x0,-0xc004(%rbp)
mov    -0xc004(%rbp),%eax
mov    %eax,-0xc00c(%rbp)
lea    -0x4000(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0xc00c(%rbp),%eax
cmp    %ecx,%eax
jge    127d <test_append_string+0x12d>
movslq -0xc004(%rbp),%rax
movsbl -0x4000(%rbp,%rax,1),%eax
ff
movslq -0xc004(%rbp),%rcx
movsbl -0xc000(%rbp,%rcx,1),%ecx
ff
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0xc004(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc004(%rbp)
jmp    1214 <test_append_string+0xc4>
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
add    $0xc010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_append_string>:
push   %r15
push   %r14
push   %rbx
sub    $0xc000,%rsp
lea    0xe8d(%rip),%rdi
call   1050 <printf@plt>
movabs $0x32676e69727473,%rax
mov    %rax,(%rsp)
lea    0xe89(%rip),%rsi
lea    0x4000(%rsp),%r14
mov    %rsp,%r15
lea    0x8000(%rsp),%rbx
mov    %r14,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1060 <append_string@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
cmp    %eax,%ebx
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    1219 <test_append_string+0xb9>
xor    %ebx,%ebx
mov    %rsp,%r14
nopl   0x0(%rax,%rax,1)
movzbl (%rsp,%rbx,1),%eax
xor    %edi,%edi
cmp    0x4000(%rsp,%rbx,1),%al
sete   %dil
call   1070 <assert@plt>
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11f0 <test_append_string+0x90>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
add    $0xc000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"test_append_string...\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [8 x i8] c"string1\00"
@.str.3 = [8 x i8] c"string2\00"
@.str.4 = [6 x i8] c"%s\0A%s\00"
@.str.5 = [4 x i8] c"Ok\0A\00"
define void @test_append_string() {
%1 = alloca [16384 x i8]
%2 = alloca [16384 x i8]
%3 = alloca [16384 x i8]
%4 = alloca i32
%5 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%7 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%8 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%9 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%10 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%12 = getelementptr inbounds [16384 x i8], [16384 x i8]* %2, i64 0, i64 0
%13 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [16384 x i8], [16384 x i8]* %2, i64 0, i64 0
%15 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%16 = call i32 @append_string(i8* %14, i8* %15)
%17 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
%21 = icmp eq i32 %18, %20
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
store i32 0, i32* %4
br label %24
24: ; preds = %43, %0
%25 = load i32, i32* %4
%26 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = icmp slt i32 %25, %27
br i1 %28, label %29, label %46
29: ; preds = %24
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 %31
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = load i32, i32* %4
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %34, %39
%41 = zext i1 %40 to i32
%42 = call i32 @assert(i32 %41)
br label %43
43: ; preds = %29
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %24
46: ; preds = %24
%47 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"test_append_string...\00"
@.str.4 = [6 x i8] c"%s\0A%s\00"
@str = [3 x i8] c"Ok\00"
define void @test_append_string() {
%1 = alloca [16384 x i8]
%2 = alloca [16384 x i8]
%3 = alloca [16384 x i8]
%4 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [16384 x i8], [16384 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%8 = bitcast [16384 x i8]* %1 to i64*
store i64 14187472748704883, i64* %8
%9 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %4, i8* %5)
%10 = call i32 @append_string(i8* %5, i8* %4)
%11 = call i32 @strlen(i8* %4)
%12 = call i32 @strlen(i8* %6)
%13 = icmp eq i32 %11, %12
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = call i32 @strlen(i8* %4)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %0, %18
%19 = phi i64 [ %27, %18 ], [ 0, %0 ]
%20 = getelementptr inbounds [16384 x i8], [16384 x i8]* %1, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = getelementptr inbounds [16384 x i8], [16384 x i8]* %3, i64 0, i64 %19
%23 = load i8, i8* %22
%24 = icmp eq i8 %21, %23
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
%27 = add nuw nsw i64 %19, 1
%28 = call i32 @strlen(i8* %4)
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %18, label %31
31: ; preds = %18, %0
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
ret void
}