#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*/ exit(int);
int /*<<< orphan*/ printf(char *);
scalar_t__ realloc(char *, size_t);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int safe_cat(char **pstr, int *plen, int pos, const char *str2) {
  int len2 = strlen(str2);
  while (*plen < pos + len2 + 1) {
    *plen += 128;
    *pstr = (char *)realloc(*pstr, (size_t)*plen);
    if (!*pstr) {
      printf("Out of memory\n");
      exit(1);
    }
  }
  strncpy((*pstr) + pos, str2, len2);
  (*pstr)[pos + len2] = '\0';
  return pos + len2;
}


x86 assembly (clang, -O0):
<safe_cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    -0x14(%rbp),%ecx
add    -0x24(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11de <safe_cat+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x80,%ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
movslq (%rax),%rsi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d9 <safe_cat+0x89>
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1173 <safe_cat+0x23>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x14(%rbp),%ecx
add    -0x24(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_cat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rcx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %eax,%r13d
add    %r15d,%r13d
nopl   0x0(%rax,%rax,1)
movslq 0x0(%rbp),%rsi
cmp    %r13d,%esi
jg     11b6 <safe_cat+0x66>
sub    $0xffffffffffffff80,%rsi
mov    %esi,0x0(%rbp)
mov    (%rbx),%rdi
call   1060 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
jne    1180 <safe_cat+0x30>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movslq %r15d,%rdi
add    (%rbx),%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
mov    (%rbx),%rax
movslq %r13d,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Out of memory\0A\00"
define i32 @safe_cat(i8** %0, i32* %1, i32 %2, i8* %3) {
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%10 = load i8*, i8** %8
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %9
br label %12
12: ; preds = %38, %4
%13 = load i32*, i32** %6
%14 = load i32, i32* %13
%15 = load i32, i32* %7
%16 = load i32, i32* %9
%17 = add nsw i32 %15, %16
%18 = add nsw i32 %17, 1
%19 = icmp slt i32 %14, %18
br i1 %19, label %20, label %39
20: ; preds = %12
%21 = load i32*, i32** %6
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 128
store i32 %23, i32* %21
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = load i32*, i32** %6
%27 = load i32, i32* %26
%28 = sext i32 %27 to i64
%29 = call i64 @realloc(i8* %25, i64 %28)
%30 = inttoptr i64 %29 to i8*
%31 = load i8**, i8*** %5
store i8* %30, i8** %31
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = icmp ne i8* %33, null
br i1 %34, label %38, label %35
35: ; preds = %20
%36 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
br label %12
39: ; preds = %12
%40 = load i8**, i8*** %5
%41 = load i8*, i8** %40
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8*, i8** %8
%46 = load i32, i32* %9
%47 = call i32 @strncpy(i8* %44, i8* %45, i32 %46)
%48 = load i8**, i8*** %5
%49 = load i8*, i8** %48
%50 = load i32, i32* %7
%51 = load i32, i32* %9
%52 = add nsw i32 %50, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %49, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %7
%56 = load i32, i32* %9
%57 = add nsw i32 %55, %56
ret i32 %57
}

LLVM IR (-O2):
@str = [14 x i8] c"Out of memory\00"
define i32 @safe_cat(i8** nocapture %0, i32* nocapture %1, i32 %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %3)
%6 = add nsw i32 %5, %2
br label %7
7: ; preds = %10, %4
%8 = load i32, i32* %1
%9 = icmp sgt i32 %8, %6
br i1 %9, label %20, label %10
10: ; preds = %7
%11 = add nsw i32 %8, 128
store i32 %11, i32* %1
%12 = load i8*, i8** %0
%13 = sext i32 %11 to i64
%14 = tail call i64 @realloc(i8* %12, i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %0
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %7
17: ; preds = %10
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %7
%21 = load i8*, i8** %0
%22 = sext i32 %2 to i64
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = tail call i32 @strncpy(i8* %23, i8* %3, i32 %5)
%25 = load i8*, i8** %0
%26 = sext i32 %6 to i64
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8 0, i8* %27
ret i32 %6
}