#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*/ assert(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ safestrcpy(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
void add_to_buffer(char *permbuf, unsigned int *permbuflen, char *tmpbuf) {
  char *end = permbuf + *permbuflen;
  int tmpbuflen = strlen(tmpbuf);
  assert(tmpbuflen < 80);
  if (tmpbuflen + *permbuflen + 1 >= 80) {
    printf("cleaing old stuff\n");
    strcpy(permbuf, tmpbuf);
    return;
  }
  safestrcpy(end, tmpbuf, tmpbuflen);
  *permbuflen += tmpbuflen;
}


x86 assembly (clang, -O0):
<add_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x50,-0x24(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%rcx
add    (%rcx),%eax
add    $0x1,%eax
cmp    $0x50,%eax
jb     11c5 <add_to_buffer+0x75>
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11e0 <add_to_buffer+0x90>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <safestrcpy@plt>
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_to_buffer>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    (%rsi),%r13d
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0x50,%eax
setl   %dil
call   1060 <assert@plt>
mov    (%r12),%eax
add    %ebx,%eax
add    $0xffffffb1,%eax
cmp    $0xffffffaf,%eax
ja     11ab <add_to_buffer+0x5b>
lea    0xe6e(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <strcpy@plt>
add    %r13,%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1070 <safestrcpy@plt>
add    %ebx,(%r12)
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [19 x i8] c"cleaing old stuff\0A\00"
define void @add_to_buffer(i8* %0, i32* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
%10 = load i32*, i32** %5
%11 = load i32, i32* %10
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
store i8* %13, i8** %7
%14 = load i8*, i8** %6
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = icmp slt i32 %16, 80
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %8
%21 = load i32*, i32** %5
%22 = load i32, i32* %21
%23 = add i32 %20, %22
%24 = add i32 %23, 1
%25 = icmp uge i32 %24, 80
br i1 %25, label %26, label %31
26: ; preds = %3
%27 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%28 = load i8*, i8** %4
%29 = load i8*, i8** %6
%30 = call i32 @strcpy(i8* %28, i8* %29)
br label %40
31: ; preds = %3
%32 = load i8*, i8** %7
%33 = load i8*, i8** %6
%34 = load i32, i32* %8
%35 = call i32 @safestrcpy(i8* %32, i8* %33, i32 %34)
%36 = load i32, i32* %8
%37 = load i32*, i32** %5
%38 = load i32, i32* %37
%39 = add i32 %38, %36
store i32 %39, i32* %37
br label %40
40: ; preds = %31, %26
ret void
}

LLVM IR (-O2):
@str = [18 x i8] c"cleaing old stuff\00"
define void @add_to_buffer(i8* %0, i32* nocapture %1, i8* %2) {
%4 = load i32, i32* %1
%5 = tail call i32 @strlen(i8* %2)
%6 = icmp slt i32 %5, 80
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* %1
%10 = add i32 %5, -79
%11 = add i32 %10, %9
%12 = icmp ult i32 %11, -80
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @strcpy(i8* %0, i8* %2)
br label %22
16: ; preds = %3
%17 = zext i32 %4 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = tail call i32 @safestrcpy(i8* %18, i8* %2, i32 %5)
%20 = load i32, i32* %1
%21 = add i32 %20, %5
store i32 %21, i32* %1
br label %22
22: ; preds = %16, %13
ret void
}