#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*/ assert(int);
int checkCmdValidAndGetTotalLen(int, int, int, char const **, size_t const *);
int /*<<< orphan*/ free(char *);
int genericMemcachedCommand(int, char *, int, int, char const **,
                            size_t const *);
int getRequestTypeFromString(char const *, size_t const);
char *malloc(int);
size_t const strlen(char const *);
int memcachedFormatCommandArgv(char **target, int argc, const char **argv,
                               const size_t *argvlen) {
  char *cmd = NULL;
  int pos;
  int totlen;
  int type;
  if (target == NULL || argc < 1)
    return -1;
  type = getRequestTypeFromString(argv[0], argvlen == NULL ? strlen(argv[0])
                                                           : argvlen[0]);
  if (type < 0) {
    goto format_err;
  }
  totlen = checkCmdValidAndGetTotalLen(type, 1, argc, argv, argvlen);
  if (totlen < 0) {
    goto format_err;
  }
  cmd = malloc(totlen + 1);
  if (cmd == NULL)
    goto memory_err;
  pos = genericMemcachedCommand(type, cmd, 1, argc, argv, argvlen);
  if (pos < 0) {
    goto format_err;
  }
  assert(pos == totlen);
  cmd[pos] = '\0';
  *target = cmd;
  return totlen;
format_err:
  if (cmd)
    free(cmd);
  return -2;
memory_err:
  return -1;
}


x86 assembly (clang, -O0):
<memcachedFormatCommandArgv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a4 <memcachedFormatCommandArgv+0x34>
cmpl   $0x1,-0x14(%rbp)
jge    11b0 <memcachedFormatCommandArgv+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <memcachedFormatCommandArgv+0x168>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11db <memcachedFormatCommandArgv+0x6b>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
jmp    11e6 <memcachedFormatCommandArgv+0x76>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1070 <getRequestTypeFromString@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1205 <memcachedFormatCommandArgv+0x95>
jmp    12b1 <memcachedFormatCommandArgv+0x141>
mov    -0x3c(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x1,%esi
call   1080 <checkCmdValidAndGetTotalLen@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    122f <memcachedFormatCommandArgv+0xbf>
jmp    12b1 <memcachedFormatCommandArgv+0x141>
mov    -0x38(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    124e <memcachedFormatCommandArgv+0xde>
jmp    12d1 <memcachedFormatCommandArgv+0x161>
mov    -0x3c(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
mov    $0x1,%edx
call   1090 <genericMemcachedCommand@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    127c <memcachedFormatCommandArgv+0x10c>
jmp    12b1 <memcachedFormatCommandArgv+0x141>
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d8 <memcachedFormatCommandArgv+0x168>
cmpq   $0x0,-0x30(%rbp)
je     12c5 <memcachedFormatCommandArgv+0x155>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12d8 <memcachedFormatCommandArgv+0x168>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memcachedFormatCommandArgv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     123e <memcachedFormatCommandArgv+0xce>
mov    %esi,%r14d
test   %esi,%esi
jle    123e <memcachedFormatCommandArgv+0xce>
mov    %rcx,%r12
mov    %rdx,%r15
mov    (%rdx),%rbx
test   %rcx,%rcx
mov    %rdi,(%rsp)
je     11ac <memcachedFormatCommandArgv+0x3c>
mov    (%r12),%rsi
jmp    11b7 <memcachedFormatCommandArgv+0x47>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rsi
mov    %rbx,%rdi
call   1070 <getRequestTypeFromString@plt>
test   %eax,%eax
js     1239 <memcachedFormatCommandArgv+0xc9>
mov    %eax,%r13d
mov    %eax,%edi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15,%rcx
mov    %r12,%r8
call   1080 <checkCmdValidAndGetTotalLen@plt>
test   %eax,%eax
js     1239 <memcachedFormatCommandArgv+0xc9>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     123e <memcachedFormatCommandArgv+0xce>
mov    %rbx,%rbp
mov    %r13d,%edi
mov    %rax,%r13
mov    %rax,%rsi
mov    $0x1,%edx
mov    %r14d,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1090 <genericMemcachedCommand@plt>
test   %eax,%eax
js     1231 <memcachedFormatCommandArgv+0xc1>
mov    %eax,%ebx
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1050 <assert@plt>
mov    %ebx,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    (%rsp),%rax
mov    %r13,(%rax)
jmp    123e <memcachedFormatCommandArgv+0xce>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @memcachedFormatCommandArgv(i8** %0, i32 %1, i8** %2, i64* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i64*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8** %0, i8*** %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store i64* %3, i64** %9
store i8* null, i8** %10
%14 = load i8**, i8*** %6
%15 = icmp eq i8** %14, null
br i1 %15, label %19, label %16
16: ; preds = %4
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %16, %4
store i32 -1, i32* %5
br label %88
20: ; preds = %16
%21 = load i8**, i8*** %8
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = load i64*, i64** %9
%25 = icmp eq i64* %24, null
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i8**, i8*** %8
%28 = getelementptr inbounds i8*, i8** %27, i64 0
%29 = load i8*, i8** %28
%30 = call i64 @strlen(i8* %29)
br label %35
31: ; preds = %20
%32 = load i64*, i64** %9
%33 = getelementptr inbounds i64, i64* %32, i64 0
%34 = load i64, i64* %33
br label %35
35: ; preds = %31, %26
%36 = phi i64 [ %30, %26 ], [ %34, %31 ]
%37 = call i32 @getRequestTypeFromString(i8* %23, i64 %36)
store i32 %37, i32* %13
%38 = load i32, i32* %13
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
br label %80
41: ; preds = %35
%42 = load i32, i32* %13
%43 = load i32, i32* %7
%44 = load i8**, i8*** %8
%45 = load i64*, i64** %9
%46 = call i32 @checkCmdValidAndGetTotalLen(i32 %42, i32 1, i32 %43, i8** %44, i64* %45)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %41
br label %80
50: ; preds = %41
%51 = load i32, i32* %12
%52 = add nsw i32 %51, 1
%53 = call i8* @malloc(i32 %52)
store i8* %53, i8** %10
%54 = load i8*, i8** %10
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %57
56: ; preds = %50
br label %87
57: ; preds = %50
%58 = load i32, i32* %13
%59 = load i8*, i8** %10
%60 = load i32, i32* %7
%61 = load i8**, i8*** %8
%62 = load i64*, i64** %9
%63 = call i32 @genericMemcachedCommand(i32 %58, i8* %59, i32 1, i32 %60, i8** %61, i64* %62)
store i32 %63, i32* %11
%64 = load i32, i32* %11
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %57
br label %80
67: ; preds = %57
%68 = load i32, i32* %11
%69 = load i32, i32* %12
%70 = icmp eq i32 %68, %69
%71 = zext i1 %70 to i32
%72 = call i32 @assert(i32 %71)
%73 = load i8*, i8** %10
%74 = load i32, i32* %11
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
%77 = load i8*, i8** %10
%78 = load i8**, i8*** %6
store i8* %77, i8** %78
%79 = load i32, i32* %12
store i32 %79, i32* %5
br label %88
80: ; preds = %66, %49, %40
%81 = load i8*, i8** %10
%82 = icmp ne i8* %81, null
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i8*, i8** %10
%85 = call i32 @free(i8* %84)
br label %86
86: ; preds = %83, %80
store i32 -2, i32* %5
br label %88
87: ; preds = %56
store i32 -1, i32* %5
br label %88
88: ; preds = %87, %86, %67, %19
%89 = load i32, i32* %5
ret i32 %89
}

LLVM IR (-O2):
define i32 @memcachedFormatCommandArgv(i8** writeonly %0, i32 %1, i8** %2, i64* %3) {
%5 = icmp eq i8** %0, null
%6 = icmp slt i32 %1, 1
%7 = or i1 %5, %6
br i1 %7, label %37, label %8
8: ; preds = %4
%9 = load i8*, i8** %2
%10 = icmp eq i64* %3, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i64 @strlen(i8* dereferenceable(1) %9)
br label %15
13: ; preds = %8
%14 = load i64, i64* %3
br label %15
15: ; preds = %13, %11
%16 = phi i64 [ %12, %11 ], [ %14, %13 ]
%17 = tail call i32 @getRequestTypeFromString(i8* %9, i64 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %37, label %19
19: ; preds = %15
%20 = tail call i32 @checkCmdValidAndGetTotalLen(i32 %17, i32 1, i32 %1, i8** %2, i64* %3)
%21 = icmp slt i32 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %19
%23 = add nuw nsw i32 %20, 1
%24 = tail call i8* @malloc(i32 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %37, label %26
26: ; preds = %22
%27 = tail call i32 @genericMemcachedCommand(i32 %17, i8* %24, i32 1, i32 %1, i8** %2, i64* %3)
%28 = icmp slt i32 %27, 0
br i1 %28, label %35, label %29
29: ; preds = %26
%30 = icmp eq i32 %27, %20
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
%33 = zext i32 %27 to i64
%34 = getelementptr inbounds i8, i8* %24, i64 %33
store i8 0, i8* %34
store i8* %24, i8** %0
br label %37
35: ; preds = %26
%36 = tail call i32 @free(i8* %24)
br label %37
37: ; preds = %19, %15, %22, %35, %4, %29
%38 = phi i32 [ %20, %29 ], [ -1, %4 ], [ -2, %35 ], [ -1, %22 ], [ -2, %15 ], [ -2, %19 ]
ret i32 %38
}