#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 */
typedef scalar_t__ uint64_t;
typedef int uint32_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, void const *, int);
int snprintf(char *, size_t, char *, char *, char const *, int, int, int, ...);
int /*<<< orphan*/ strncpy(char *, char *, int);
off_t storage_command(char *buf, size_t bufsz, char *cmd, const char *key,
                      size_t keylen, const void *dta, int dtalen,
                      uint32_t flags, uint32_t exp, uint64_t cas) {
  int j = 0, len;
  if (!dta) {
    return 0;
  }
  if (!cas) {
    len = snprintf(buf, bufsz, "%s %s %d %d %d\r\n", cmd, key, flags, exp,
                   dtalen);
  } else {
    len = snprintf(buf, bufsz, "%s %s %d %d %d %llu returncas\r\n", cmd, key,
                   flags, exp, dtalen, (unsigned long long)cas);
  }
  j = dtalen < bufsz - len - 3 ? dtalen : bufsz - len - 3;
  memcpy(buf + len, dta, j);
  strncpy(buf + len + j, "\r\n", 3);
  return len + j + 3;
}


x86 assembly (clang, -O0):
<storage_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    117b <storage_command+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    1287 <storage_command+0x157>
cmpq   $0x0,0x28(%rbp)
jne    11bf <storage_command+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    0x18(%rbp),%r9d
mov    0x20(%rbp),%r10d
mov    0x10(%rbp),%eax
lea    0xe58(%rip),%rdx
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x40(%rbp)
jmp    11fe <storage_command+0xce>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    0x18(%rbp),%r9d
mov    0x20(%rbp),%r11d
mov    0x10(%rbp),%r10d
mov    0x28(%rbp),%rax
lea    0xe2b(%rip),%rdx
mov    %r11d,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %rax,0x10(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x40(%rbp)
movslq 0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x40(%rbp),%rdx
sub    %rdx,%rcx
sub    $0x3,%rcx
cmp    %rcx,%rax
jae    1227 <storage_command+0xf7>
movslq 0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    123a <storage_command+0x10a>
mov    -0x18(%rbp),%rax
movslq -0x40(%rbp),%rcx
sub    %rcx,%rax
sub    $0x3,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x40(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
movslq -0x40(%rbp),%rax
add    %rax,%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
lea    0xd9d(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%eax
add    -0x3c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<storage_command>:
test   %r9,%r9
je     1189 <storage_command+0x59>
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %r9,%r15
mov    %rcx,%r8
mov    %rdx,%rcx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x48(%rsp),%r11
mov    0x40(%rsp),%r10d
mov    0x38(%rsp),%r9d
mov    0x30(%rsp),%r12d
test   %r11,%r11
je     118c <storage_command+0x5c>
sub    $0x8,%rsp
lea    0xea1(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
push   %r11
push   %r12
push   %r10
call   1040 <snprintf@plt>
add    $0x20,%rsp
jmp    11a8 <storage_command+0x78>
xor    %eax,%eax
ret
lea    0xe6d(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
push   %r12
push   %r10
call   1040 <snprintf@plt>
add    $0x10,%rsp
movslq %r12d,%rcx
movslq %eax,%r12
sub    %r12,%rbx
add    $0xfffffffffffffffd,%rbx
cmp    %rcx,%rbx
cmova  %rcx,%rbx
add    %r12,%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <memcpy@plt>
movslq %ebx,%rbx
add    %rbx,%r14
lea    0xe35(%rip),%rsi
mov    %r14,%rdi
mov    $0x3,%edx
call   1030 <strncpy@plt>
lea    (%r12,%rbx,1),%eax
add    $0x3,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"%s %s %d %d %d\0D\0A\00"
@.str.1 = [32 x i8] c"%s %s %d %d %d %llu returncas\0D\0A\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define i32 @storage_command(i8* %0, i64 %1, i8* %2, i8* %3, i64 %4, i8* %5, i32 %6, i32 %7, i32 %8, i64 %9) {
%11 = icmp eq i8* %5, null
br i1 %11, label %35, label %12
12: ; preds = %10
%13 = icmp eq i64 %9, 0
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 (i8*, i64, i8*, i8*, i8*, i32, i32, i32, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %3, i32 %7, i32 %8, i32 %6)
br label %18
16: ; preds = %12
%17 = tail call i32 (i8*, i64, i8*, i8*, i8*, i32, i32, i32, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %3, i32 %7, i32 %8, i32 %6, i64 %9)
br label %18
18: ; preds = %16, %14
%19 = phi i32 [ %17, %16 ], [ %15, %14 ]
%20 = sext i32 %6 to i64
%21 = sext i32 %19 to i64
%22 = sub i64 %1, %21
%23 = add i64 %22, -3
%24 = icmp ugt i64 %23, %20
%25 = select i1 %24, i64 %20, i64 %23
%26 = trunc i64 %25 to i32
%27 = getelementptr inbounds i8, i8* %0, i64 %21
%28 = tail call i32 @memcpy(i8* %27, i8* %5, i32 %26)
%29 = shl i64 %25, 32
%30 = ashr exact i64 %29, 32
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = tail call i32 @strncpy(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 3)
%33 = add i32 %19, 3
%34 = add i32 %33, %26
br label %35
35: ; preds = %10, %18
%36 = phi i32 [ %34, %18 ], [ 0, %10 ]
ret i32 %36
}