#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 *, int, int /*<<< orphan*/);
scalar_t__ before_add_parse_katcl(struct katcl_parse *, int);
char *request_space_parse_katcl(struct katcl_parse *, int);
int snprintf(char *, int, char *, unsigned long);
int add_unsigned_long_parse_katcl(struct katcl_parse *p, int flags,
                                  unsigned long v) {
  int result;
  char *ptr;
  if (before_add_parse_katcl(p, flags) < 0) {
    return -1;
  }
  ptr = request_space_parse_katcl(p, 32);
  if (ptr == NULL) {
    return -1;
  }
  result = snprintf(ptr, 32, "%lu", v);
  if ((result <= 0) || (result >= 32)) {
    return -1;
  }
  return after_add_parse_katcl(p, result, 0);
}


x86 assembly (clang, -O0):
<add_unsigned_long_parse_katcl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <before_add_parse_katcl@plt>
cmp    $0x0,%rax
jge    1175 <add_unsigned_long_parse_katcl+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <add_unsigned_long_parse_katcl+0xab>
mov    -0x10(%rbp),%rdi
mov    $0x20,%esi
call   1060 <request_space_parse_katcl@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119e <add_unsigned_long_parse_katcl+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <add_unsigned_long_parse_katcl+0xab>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x20,%esi
lea    0xe4e(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    11ce <add_unsigned_long_parse_katcl+0x8e>
cmpl   $0x20,-0x24(%rbp)
jl     11da <add_unsigned_long_parse_katcl+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <add_unsigned_long_parse_katcl+0xab>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
xor    %edx,%edx
call   1040 <after_add_parse_katcl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_unsigned_long_parse_katcl>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
call   1050 <before_add_parse_katcl@plt>
test   %rax,%rax
js     1185 <add_unsigned_long_parse_katcl+0x45>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1060 <request_space_parse_katcl@plt>
test   %rax,%rax
je     1185 <add_unsigned_long_parse_katcl+0x45>
lea    0xe93(%rip),%rdx
mov    %rax,%rdi
mov    $0x20,%esi
mov    %r14,%rcx
call   1030 <snprintf@plt>
lea    -0x20(%rax),%ecx
cmp    $0xffffffe1,%ecx
jae    1192 <add_unsigned_long_parse_katcl+0x52>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
mov    %eax,%esi
xor    %edx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <after_add_parse_katcl@plt>

LLVM IR (-O0):
%struct.katcl_parse = type { i32 }
@.str = [4 x i8] c"%lu\00"
define i32 @add_unsigned_long_parse_katcl(%struct.katcl_parse* %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.katcl_parse*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store %struct.katcl_parse* %0, %struct.katcl_parse** %5
store i32 %1, i32* %6
store i64 %2, i64* %7
%10 = load %struct.katcl_parse*, %struct.katcl_parse** %5
%11 = load i32, i32* %6
%12 = call i64 @before_add_parse_katcl(%struct.katcl_parse* %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %35
15: ; preds = %3
%16 = load %struct.katcl_parse*, %struct.katcl_parse** %5
%17 = call i8* @request_space_parse_katcl(%struct.katcl_parse* %16, i32 32)
store i8* %17, i8** %9
%18 = load i8*, i8** %9
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 -1, i32* %4
br label %35
21: ; preds = %15
%22 = load i8*, i8** %9
%23 = load i64, i64* %7
%24 = call i32 @snprintf(i8* %22, i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp sle i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %21
%28 = load i32, i32* %8
%29 = icmp sge i32 %28, 32
br i1 %29, label %30, label %31
30: ; preds = %27, %21
store i32 -1, i32* %4
br label %35
31: ; preds = %27
%32 = load %struct.katcl_parse*, %struct.katcl_parse** %5
%33 = load i32, i32* %8
%34 = call i32 @after_add_parse_katcl(%struct.katcl_parse* %32, i32 %33, i32 0)
store i32 %34, i32* %4
br label %35
35: ; preds = %31, %30, %20, %14
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.katcl_parse = type { i32 }
@.str = [4 x i8] c"%lu\00"
define i32 @add_unsigned_long_parse_katcl(%struct.katcl_parse* %0, i32 %1, i64 %2) {
%4 = tail call i64 @before_add_parse_katcl(%struct.katcl_parse* %0, i32 %1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %15, label %6
6: ; preds = %3
%7 = tail call i8* @request_space_parse_katcl(%struct.katcl_parse* %0, i32 32)
%8 = icmp eq i8* %7, null
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = tail call i32 @snprintf(i8* %7, i32 32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 %2)
%11 = add i32 %10, -32
%12 = icmp ult i32 %11, -31
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @after_add_parse_katcl(%struct.katcl_parse* %0, i32 %10, i32 0)
br label %15
15: ; preds = %9, %6, %3, %13
%16 = phi i32 [ %14, %13 ], [ -1, %3 ], [ -1, %6 ], [ -1, %9 ]
ret i32 %16
}