#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 */
struct katcl_parse {
  int dummy;
};

/* Variables and functions */
int after_add_parse_katcl(struct katcl_parse *, unsigned int, int);
scalar_t__ before_add_parse_katcl(struct katcl_parse *, int);
int /*<<< orphan*/ memcpy(char *, char *, unsigned int);
char *request_space_parse_katcl(struct katcl_parse *, unsigned int);
int add_buffer_parse_katcl(struct katcl_parse *p, int flags, void *buffer,
                           unsigned int len) {
  char *src, *dst;
  src = buffer;
  if (before_add_parse_katcl(p, flags) < 0) {
    return -1;
  }
  if (len > 0) {
    dst = request_space_parse_katcl(p, len + 1);
    if (dst == NULL) {
      return -1;
    }
    memcpy(dst, src, len);
  }
  return after_add_parse_katcl(p, len, 1);
}


x86 assembly (clang, -O0):
<add_buffer_parse_katcl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <before_add_parse_katcl@plt>
cmp    $0x0,%rax
jge    1180 <add_buffer_parse_katcl+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <add_buffer_parse_katcl+0x98>
cmpl   $0x0,-0x24(%rbp)
jbe    11c4 <add_buffer_parse_katcl+0x84>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
add    $0x1,%esi
call   1060 <request_space_parse_katcl@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11b4 <add_buffer_parse_katcl+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <add_buffer_parse_katcl+0x98>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    $0x1,%edx
call   1030 <after_add_parse_katcl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_buffer_parse_katcl>:
push   %r15
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rdi,%rbx
call   1050 <before_add_parse_katcl@plt>
test   %rax,%rax
js     1191 <add_buffer_parse_katcl+0x51>
test   %r14d,%r14d
je     117c <add_buffer_parse_katcl+0x3c>
lea    0x1(%r14),%esi
mov    %rbx,%rdi
call   1060 <request_space_parse_katcl@plt>
test   %rax,%rax
je     1191 <add_buffer_parse_katcl+0x51>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <after_add_parse_katcl@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.katcl_parse = type { i32 }
define i32 @add_buffer_parse_katcl(%struct.katcl_parse* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.katcl_parse*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store %struct.katcl_parse* %0, %struct.katcl_parse** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %8
store i8* %12, i8** %10
%13 = load %struct.katcl_parse*, %struct.katcl_parse** %6
%14 = load i32, i32* %7
%15 = call i64 @before_add_parse_katcl(%struct.katcl_parse* %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %38
18: ; preds = %4
%19 = load i32, i32* %9
%20 = icmp ugt i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = load %struct.katcl_parse*, %struct.katcl_parse** %6
%23 = load i32, i32* %9
%24 = add i32 %23, 1
%25 = call i8* @request_space_parse_katcl(%struct.katcl_parse* %22, i32 %24)
store i8* %25, i8** %11
%26 = load i8*, i8** %11
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 -1, i32* %5
br label %38
29: ; preds = %21
%30 = load i8*, i8** %11
%31 = load i8*, i8** %10
%32 = load i32, i32* %9
%33 = call i32 @memcpy(i8* %30, i8* %31, i32 %32)
br label %34
34: ; preds = %29, %18
%35 = load %struct.katcl_parse*, %struct.katcl_parse** %6
%36 = load i32, i32* %9
%37 = call i32 @after_add_parse_katcl(%struct.katcl_parse* %35, i32 %36, i32 1)
store i32 %37, i32* %5
br label %38
38: ; preds = %34, %28, %17
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.katcl_parse = type { i32 }
define i32 @add_buffer_parse_katcl(%struct.katcl_parse* %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i64 @before_add_parse_katcl(%struct.katcl_parse* %0, i32 %1)
%6 = icmp slt i64 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = icmp eq i32 %3, 0
br i1 %8, label %15, label %9
9: ; preds = %7
%10 = add i32 %3, 1
%11 = tail call i8* @request_space_parse_katcl(%struct.katcl_parse* %0, i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = tail call i32 @memcpy(i8* %11, i8* %2, i32 %3)
br label %15
15: ; preds = %13, %7
%16 = tail call i32 @after_add_parse_katcl(%struct.katcl_parse* %0, i32 %3, i32 1)
br label %17
17: ; preds = %9, %4, %15
%18 = phi i32 [ %16, %15 ], [ -1, %4 ], [ -1, %9 ]
ret i32 %18
}