#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, int);
scalar_t__ before_add_parse_katcl(struct katcl_parse *, int);
int /*<<< orphan*/ memcpy(char *, void *, 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 *dst;
  if (before_add_parse_katcl(p, flags) < 0) {
    return -1;
  }
  dst = request_space_parse_katcl(p, len + 1);
  if (dst == NULL) {
    return -1;
  }
  if (len > 0) {
    if (buffer == NULL) {
      return -1;
    }
    memcpy(dst, buffer, len);
  } else {
  }
  return after_add_parse_katcl(p, len, 1, flags);
}


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

x86 assembly (clang, -O2):
<add_buffer_parse_katcl>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%rbp
call   1050 <before_add_parse_katcl@plt>
test   %rax,%rax
js     119e <add_buffer_parse_katcl+0x5e>
lea    0x1(%rbx),%esi
mov    %rbp,%rdi
call   1060 <request_space_parse_katcl@plt>
test   %rax,%rax
je     119e <add_buffer_parse_katcl+0x5e>
test   %ebx,%ebx
je     1182 <add_buffer_parse_katcl+0x42>
test   %r15,%r15
je     119e <add_buffer_parse_katcl+0x5e>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r14d,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <after_add_parse_katcl@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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*
store %struct.katcl_parse* %0, %struct.katcl_parse** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load %struct.katcl_parse*, %struct.katcl_parse** %6
%12 = load i32, i32* %7
%13 = call i64 @before_add_parse_katcl(%struct.katcl_parse* %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 -1, i32* %5
br label %42
16: ; preds = %4
%17 = load %struct.katcl_parse*, %struct.katcl_parse** %6
%18 = load i32, i32* %9
%19 = add i32 %18, 1
%20 = call i8* @request_space_parse_katcl(%struct.katcl_parse* %17, i32 %19)
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %5
br label %42
24: ; preds = %16
%25 = load i32, i32* %9
%26 = icmp ugt i32 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %24
%28 = load i8*, i8** %8
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -1, i32* %5
br label %42
31: ; preds = %27
%32 = load i8*, i8** %10
%33 = load i8*, i8** %8
%34 = load i32, i32* %9
%35 = call i32 @memcpy(i8* %32, i8* %33, i32 %34)
br label %37
36: ; preds = %24
br label %37
37: ; preds = %36, %31
%38 = load %struct.katcl_parse*, %struct.katcl_parse** %6
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = call i32 @after_add_parse_katcl(%struct.katcl_parse* %38, i32 %39, i32 1, i32 %40)
store i32 %41, i32* %5
br label %42
42: ; preds = %37, %30, %23, %15
%43 = load i32, i32* %5
ret i32 %43
}

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 %19, label %7
7: ; preds = %4
%8 = add i32 %3, 1
%9 = tail call i8* @request_space_parse_katcl(%struct.katcl_parse* %0, i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %19, label %11
11: ; preds = %7
%12 = icmp eq i32 %3, 0
br i1 %12, label %17, label %13
13: ; preds = %11
%14 = icmp eq i8* %2, null
br i1 %14, label %19, label %15
15: ; preds = %13
%16 = tail call i32 @memcpy(i8* %9, i8* %2, i32 %3)
br label %17
17: ; preds = %11, %15
%18 = tail call i32 @after_add_parse_katcl(%struct.katcl_parse* %0, i32 %3, i32 1, i32 %1)
br label %19
19: ; preds = %13, %7, %4, %17
%20 = phi i32 [ %18, %17 ], [ -1, %4 ], [ -1, %7 ], [ -1, %13 ]
ret i32 %20
}