#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 */
scalar_t__ bson_malloc(int);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *mongo_ns_to_cmd_db(const char *ns) {
  char *current = NULL;
  char *cmd_db_name = NULL;
  int len = 0;
  for (current = (char *)ns; *current != '.'; current++) {
    len++;
  }
  cmd_db_name = (char *)bson_malloc(len + 6);
  strncpy(cmd_db_name, ns, len);
  strncpy(cmd_db_name + len, ".$cmd", 6);
  return cmd_db_name;
}


x86 assembly (clang, -O0):
<mongo_ns_to_cmd_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1175 <mongo_ns_to_cmd_db+0x55>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    114b <mongo_ns_to_cmd_db+0x2b>
mov    -0x1c(%rbp),%edi
add    $0x6,%edi
call   1030 <bson_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
lea    0xe5a(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mongo_ns_to_cmd_db>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
cmpb   $0x2e,(%rdi)
je     114c <mongo_ns_to_cmd_db+0x2c>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0x2e,0x1(%r14,%rbx,1)
lea    0x1(%rbx),%rbx
jne    1140 <mongo_ns_to_cmd_db+0x20>
lea    0x6(%rbx),%edi
call   1030 <bson_malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <strncpy@plt>
mov    %ebx,%edi
add    %r15,%rdi
lea    0xe90(%rip),%rsi
mov    $0x6,%edx
call   1040 <strncpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c".$cmd\00"
define i8* @mongo_ns_to_cmd_db(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %3
store i8* null, i8** %4
store i32 0, i32* %5
%6 = load i8*, i8** %2
store i8* %6, i8** %3
br label %7
7: ; preds = %15, %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 46
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = load i32, i32* %5
%14 = add nsw i32 %13, 1
store i32 %14, i32* %5
br label %15
15: ; preds = %12
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
br label %7
18: ; preds = %7
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 6
%21 = call i64 @bson_malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = load i8*, i8** %2
%25 = load i32, i32* %5
%26 = call i32 @strncpy(i8* %23, i8* %24, i32 %25)
%27 = load i8*, i8** %4
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = call i32 @strncpy(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 6)
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
@.str = [6 x i8] c".$cmd\00"
define i8* @mongo_ns_to_cmd_db(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 46
br i1 %3, label %11, label %4
4: ; preds = %1, %4
%5 = phi i32 [ %7, %4 ], [ 0, %1 ]
%6 = phi i8* [ %8, %4 ], [ %0, %1 ]
%7 = add nuw nsw i32 %5, 1
%8 = getelementptr inbounds i8, i8* %6, i64 1
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 46
br i1 %10, label %11, label %4
11: ; preds = %4, %1
%12 = phi i32 [ 0, %1 ], [ %7, %4 ]
%13 = add nuw nsw i32 %12, 6
%14 = tail call i64 @bson_malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @strncpy(i8* %15, i8* %0, i32 %12)
%17 = zext i32 %12 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = tail call i32 @strncpy(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 6)
ret i8* %15
}