#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 katcp_dispatch {
  int dummy;
};

/* Variables and functions */
int append_string_katcp(struct katcp_dispatch *, int, char *);
char *arg_string_katcp(struct katcp_dispatch *, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
char *strdup(char *);
int prepend_generic_katcp(struct katcp_dispatch *d, int reply) {
  char *message, *string;
  int result;
  message = arg_string_katcp(d, 0);
  if (message == NULL) {
    return -1;
  }
  string = strdup(message);
  if (string == NULL) {
    return -1;
  }
  string[0] = reply ? '!' : '#';
  result = append_string_katcp(d, 0x01 | 0x10, string);
  free(string);
  return result;
}


x86 assembly (clang, -O0):
<prepend_generic_katcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <arg_string_katcp@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1175 <prepend_generic_katcp+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <prepend_generic_katcp+0x98>
mov    -0x20(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1199 <prepend_generic_katcp+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d8 <prepend_generic_katcp+0x98>
mov    -0x14(%rbp),%edx
mov    $0x23,%eax
mov    $0x21,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x11,%esi
call   1040 <append_string_katcp@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepend_generic_katcp>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %esi,%esi
call   1050 <arg_string_katcp@plt>
test   %rax,%rax
je     1191 <prepend_generic_katcp+0x51>
mov    %rax,%rdi
call   1060 <strdup@plt>
test   %rax,%rax
je     1191 <prepend_generic_katcp+0x51>
mov    %rax,%rbp
test   %r14d,%r14d
sete   %al
add    %al,%al
or     $0x21,%al
mov    %al,0x0(%rbp)
mov    %rbx,%rdi
mov    $0x11,%esi
mov    %rbp,%rdx
call   1040 <append_string_katcp@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    1196 <prepend_generic_katcp+0x56>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.katcp_dispatch = type { i32 }
define i32 @prepend_generic_katcp(%struct.katcp_dispatch* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.katcp_dispatch*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store %struct.katcp_dispatch* %0, %struct.katcp_dispatch** %4
store i32 %1, i32* %5
%9 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %4
%10 = call i8* @arg_string_katcp(%struct.katcp_dispatch* %9, i32 0)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %34
14: ; preds = %2
%15 = load i8*, i8** %6
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %7
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %3
br label %34
20: ; preds = %14
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
%23 = zext i1 %22 to i64
%24 = select i1 %22, i32 33, i32 35
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %7
%27 = getelementptr inbounds i8, i8* %26, i64 0
store i8 %25, i8* %27
%28 = load %struct.katcp_dispatch*, %struct.katcp_dispatch** %4
%29 = load i8*, i8** %7
%30 = call i32 @append_string_katcp(%struct.katcp_dispatch* %28, i32 17, i8* %29)
store i32 %30, i32* %8
%31 = load i8*, i8** %7
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* %8
store i32 %33, i32* %3
br label %34
34: ; preds = %20, %19, %13
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.katcp_dispatch = type { i32 }
define i32 @prepend_generic_katcp(%struct.katcp_dispatch* %0, i32 %1) {
%3 = tail call i8* @arg_string_katcp(%struct.katcp_dispatch* %0, i32 0)
%4 = icmp eq i8* %3, null
br i1 %4, label %13, label %5
5: ; preds = %2
%6 = tail call i8* @strdup(i8* %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = icmp eq i32 %1, 0
%10 = select i1 %9, i8 35, i8 33
store i8 %10, i8* %6
%11 = tail call i32 @append_string_katcp(%struct.katcp_dispatch* %0, i32 17, i8* %6)
%12 = tail call i32 @free(i8* %6)
br label %13
13: ; preds = %5, %2, %8
%14 = phi i32 [ %11, %8 ], [ -1, %2 ], [ -1, %5 ]
ret i32 %14
}