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

/* Variables and functions */
char *da_cloud_membuf_alloc(struct da_cloud_membuf **, scalar_t__);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *da_cloud_membuf_strdup(struct da_cloud_membuf **dcm, const char *src) {
  if (dcm == NULL)
    return (NULL);
  char *p = da_cloud_membuf_alloc(dcm, strlen(src) + 1);
  if (p == NULL)
    return (NULL);
  strcpy(p, src);
  return (p);
}


x86 assembly (clang, -O0):
<da_cloud_membuf_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <da_cloud_membuf_strdup+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11aa <da_cloud_membuf_strdup+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1050 <da_cloud_membuf_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1195 <da_cloud_membuf_strdup+0x65>
movq   $0x0,-0x8(%rbp)
jmp    11aa <da_cloud_membuf_strdup+0x7a>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<da_cloud_membuf_strdup>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1168 <da_cloud_membuf_strdup+0x38>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbx,%rdi
call   1050 <da_cloud_membuf_alloc@plt>
test   %rax,%rax
je     1168 <da_cloud_membuf_strdup+0x38>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    116a <da_cloud_membuf_strdup+0x3a>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.da_cloud_membuf = type { i32 }
define i8* @da_cloud_membuf_strdup(%struct.da_cloud_membuf** %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.da_cloud_membuf**
%5 = alloca i8*
%6 = alloca i8*
store %struct.da_cloud_membuf** %0, %struct.da_cloud_membuf*** %4
store i8* %1, i8** %5
%7 = load %struct.da_cloud_membuf**, %struct.da_cloud_membuf*** %4
%8 = icmp eq %struct.da_cloud_membuf** %7, null
br i1 %8, label %9, label %10
9: ; preds = %2
store i8* null, i8** %3
br label %24
10: ; preds = %2
%11 = load %struct.da_cloud_membuf**, %struct.da_cloud_membuf*** %4
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %13, 1
%15 = call i8* @da_cloud_membuf_alloc(%struct.da_cloud_membuf** %11, i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %10
store i8* null, i8** %3
br label %24
19: ; preds = %10
%20 = load i8*, i8** %6
%21 = load i8*, i8** %5
%22 = call i32 @strcpy(i8* %20, i8* %21)
%23 = load i8*, i8** %6
store i8* %23, i8** %3
br label %24
24: ; preds = %19, %18, %9
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
%struct.da_cloud_membuf = type { i32 }
define i8* @da_cloud_membuf_strdup(%struct.da_cloud_membuf** %0, i8* %1) {
%3 = icmp eq %struct.da_cloud_membuf** %0, null
br i1 %3, label %11, label %4
4: ; preds = %2
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @da_cloud_membuf_alloc(%struct.da_cloud_membuf** %0, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @strcpy(i8* %7, i8* %1)
br label %11
11: ; preds = %9, %4, %2
%12 = phi i8* [ null, %2 ], [ %7, %9 ], [ null, %4 ]
ret i8* %12
}