#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*/ printf(char *, ...);
int str_append(char *p_str, char ch, int count, int max_len) {
  int len = 0;
  int value = (int)'0';
  while (p_str[len]) {
    len++;
  }
  if (len + 1 >= max_len) {
    printf("compresses string exceeding given string length\n");
    return -1;
  }
  value += count;
  printf("DEBUG: char:= %c, count:= %d, value:= %d\n", ch, count, value);
  p_str[len] = ch;
  p_str[len + 1] = value;
  p_str[len + 2] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<str_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x30,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1156 <str_append+0x46>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1136 <str_append+0x26>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jl     117f <str_append+0x6f>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <str_append+0xcb>
mov    -0x18(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
movsbl -0x11(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_append>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %ebp,%ebp
cmpb   $0x0,(%rbx,%rbp,1)
lea    0x1(%rbp),%rbp
jne    1130 <str_append+0x10>
cmp    %ecx,%ebp
jge    1177 <str_append+0x57>
lea    -0x1(%rbp),%rax
mov    %eax,%r13d
lea    0x30(%rdx),%r14d
movsbl %sil,%r12d
lea    0xeac(%rip),%rdi
xor    %r15d,%r15d
mov    %r12d,%esi
mov    %r14d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12b,(%rbx,%r13,1)
mov    %ebp,%eax
mov    %r14b,(%rbx,%rax,1)
add    $0x1,%ebp
movb   $0x0,(%rbx,%rbp,1)
jmp    1189 <str_append+0x69>
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [49 x i8] c"compresses string exceeding given string length\0A\00"
@.str.1 = [42 x i8] c"DEBUG: char:= %c, count:= %d, value:= %d\0A\00"
define i32 @str_append(i8* %0, i8 signext %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i8 %1, i8* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i32 48, i32* %11
br label %12
12: ; preds = %19, %4
%13 = load i8*, i8** %6
%14 = load i32, i32* %10
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* %10
%21 = add nsw i32 %20, 1
store i32 %21, i32* %10
br label %12
22: ; preds = %12
%23 = load i32, i32* %10
%24 = add nsw i32 %23, 1
%25 = load i32, i32* %9
%26 = icmp sge i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %55
29: ; preds = %22
%30 = load i32, i32* %8
%31 = load i32, i32* %11
%32 = add nsw i32 %31, %30
store i32 %32, i32* %11
%33 = load i8, i8* %7
%34 = sext i8 %33 to i32
%35 = load i32, i32* %8
%36 = load i32, i32* %11
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %35, i32 %36)
%38 = load i8, i8* %7
%39 = load i8*, i8** %6
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 %38, i8* %42
%43 = load i32, i32* %11
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %6
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 %44, i8* %49
%50 = load i8*, i8** %6
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 2
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
store i8 0, i8* %54
store i32 0, i32* %5
br label %55
55: ; preds = %29, %27
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@.str.1 = [42 x i8] c"DEBUG: char:= %c, count:= %d, value:= %d\0A\00"
@str = [48 x i8] c"compresses string exceeding given string length\00"
define i32 @str_append(i8* nocapture %0, i8 signext %1, i32 %2, i32 %3) {
br label %5
5: ; preds = %5, %4
%6 = phi i64 [ %10, %5 ], [ 0, %4 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
%10 = add nuw i64 %6, 1
br i1 %9, label %11, label %5
11: ; preds = %5
%12 = trunc i64 %6 to i32
%13 = add nuw nsw i32 %12, 1
%14 = icmp slt i32 %13, %3
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %29
17: ; preds = %11
%18 = and i64 %6, 4294967295
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = add nsw i32 %2, 48
%21 = sext i8 %1 to i32
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %2, i32 %20)
store i8 %1, i8* %19
%23 = trunc i32 %20 to i8
%24 = zext i32 %13 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 %23, i8* %25
%26 = add i64 %6, 2
%27 = and i64 %26, 4294967295
%28 = getelementptr inbounds i8, i8* %0, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %17, %15
%30 = phi i32 [ -1, %15 ], [ 0, %17 ]
ret i32 %30
}