#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 append_char(char **, char, int, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
char *append_test() {
  int curr_size = sizeof(char) * 2;
  char *dynstr = malloc(curr_size);
  curr_size = append_char(&dynstr, 'O', 0, curr_size);
  curr_size = append_char(&dynstr, 'K', 1, curr_size);
  curr_size = append_char(&dynstr, 0, 2, curr_size);
  do {
    if (!(curr_size == 4)) {
      free(dynstr);
      return "Dynstr didn't grow as expected";
    }
  } while (0);
  do {
    if (!(dynstr[0] == 'O' && dynstr[1] == 'K' && dynstr[2] == 0)) {
      free(dynstr);
      return "Characters set incorrectly in append_test";
    }
  } while (0);
  return 0;
}


x86 assembly (clang, -O0):
<append_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x2,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rdi
mov    $0x4f,%esi
xor    %edx,%edx
call   1040 <append_char@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rdi
mov    $0x4b,%esi
mov    $0x1,%edx
call   1040 <append_char@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   1040 <append_char@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
je     11b3 <append_test+0x83>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe56(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1215 <append_test+0xe5>
jmp    11b8 <append_test+0x88>
jmp    11bd <append_test+0x8d>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x4f,%eax
jne    11ef <append_test+0xbf>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x4b,%eax
jne    11ef <append_test+0xbf>
mov    -0x18(%rbp),%rax
movsbl 0x2(%rax),%eax
cmp    $0x0,%eax
je     1208 <append_test+0xd8>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xe20(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1215 <append_test+0xe5>
jmp    120d <append_test+0xdd>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_test>:
push   %rbx
sub    $0x10,%rsp
mov    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4f,%esi
xor    %edx,%edx
mov    $0x2,%ecx
call   1040 <append_char@plt>
mov    %rbx,%rdi
mov    $0x4b,%esi
mov    $0x1,%edx
mov    %eax,%ecx
call   1040 <append_char@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x2,%edx
mov    %eax,%ecx
call   1040 <append_char@plt>
mov    0x8(%rsp),%rdi
cmp    $0x4,%eax
jne    11ab <append_test+0x7b>
cmpb   $0x4f,(%rdi)
jne    119d <append_test+0x6d>
cmpb   $0x4b,0x1(%rdi)
jne    119d <append_test+0x6d>
cmpb   $0x0,0x2(%rdi)
je     11bd <append_test+0x8d>
call   1030 <free@plt>
lea    0xe76(%rip),%rax
jmp    11b7 <append_test+0x87>
call   1030 <free@plt>
lea    0xe49(%rip),%rax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
jmp    11b7 <append_test+0x87>

LLVM IR (-O0):
@.str = [31 x i8] c"Dynstr didn't grow as expected\00"
@.str.1 = [42 x i8] c"Characters set incorrectly in append_test\00"
define i8* @append_test() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
store i32 2, i32* %2
%4 = load i32, i32* %2
%5 = call i8* @malloc(i32 %4)
store i8* %5, i8** %3
%6 = load i32, i32* %2
%7 = call i32 @append_char(i8** %3, i8 signext 79, i32 0, i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = call i32 @append_char(i8** %3, i8 signext 75, i32 1, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = call i32 @append_char(i8** %3, i8 signext 0, i32 2, i32 %10)
store i32 %11, i32* %2
br label %12
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, 4
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = call i32 @free(i8* %16)
store i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8** %1
br label %43
18: ; preds = %12
br label %19
19: ; preds = %18
br label %20
20: ; preds = %19
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 79
br i1 %25, label %26, label %38
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 75
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 2
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %32, %26, %20
%39 = load i8*, i8** %3
%40 = call i32 @free(i8* %39)
store i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %43
41: ; preds = %32
br label %42
42: ; preds = %41
store i8* null, i8** %1
br label %43
43: ; preds = %42, %38, %15
%44 = load i8*, i8** %1
ret i8* %44
}

LLVM IR (-O2):
@.str = [31 x i8] c"Dynstr didn't grow as expected\00"
@.str.1 = [42 x i8] c"Characters set incorrectly in append_test\00"
define i8* @append_test() {
%1 = alloca i8*
%2 = bitcast i8** %1 to i8*
%3 = tail call dereferenceable_or_null(2) i8* @malloc(i32 2)
store i8* %3, i8** %1
%4 = call i32 @append_char(i8** %1, i8 signext 79, i32 0, i32 2)
%5 = call i32 @append_char(i8** %1, i8 signext 75, i32 1, i32 %4)
%6 = call i32 @append_char(i8** %1, i8 signext 0, i32 2, i32 %5)
%7 = icmp eq i32 %6, 4
%8 = load i8*, i8** %1
br i1 %7, label %11, label %9
9: ; preds = %0
%10 = call i32 @free(i8* %8)
br label %24
11: ; preds = %0
%12 = load i8, i8* %8
%13 = icmp eq i8 %12, 79
br i1 %13, label %14, label %22
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %8, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 75
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %8, i64 2
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18, %14, %11
%23 = call i32 @free(i8* %8)
br label %24
24: ; preds = %18, %22, %9
%25 = phi i8* [ getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), %22 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), %9 ], [ null, %18 ]
ret i8* %25
}