#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*/ free(char *);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int ap_str_makestr(char **d, const char *s) {
  if (*d != NULL)
    free(*d);
  *d = NULL;
  if (s == NULL)
    return 0;
  if (NULL == (*d = (char *)malloc(strlen(s) + 1)))
    return 0;
  strcpy((char *)(*d), s);
  return 1;
}


x86 assembly (clang, -O0):
<ap_str_makestr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     116a <ap_str_makestr+0x2a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x18(%rbp)
jne    118c <ap_str_makestr+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <ap_str_makestr+0x99>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11c2 <ap_str_makestr+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <ap_str_makestr+0x99>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ap_str_makestr>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1157 <ap_str_makestr+0x17>
call   1030 <free@plt>
movq   $0x0,(%rbx)
xor    %ebp,%ebp
test   %r14,%r14
je     118e <ap_str_makestr+0x4e>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     118e <ap_str_makestr+0x4e>
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ap_str_makestr(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%6 = load i8**, i8*** %4
%7 = load i8*, i8** %6
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8**, i8*** %4
%11 = load i8*, i8** %10
%12 = call i32 @free(i8* %11)
br label %13
13: ; preds = %9, %2
%14 = load i8**, i8*** %4
store i8* null, i8** %14
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 0, i32* %3
br label %32
18: ; preds = %13
%19 = load i8*, i8** %5
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
%24 = load i8**, i8*** %4
store i8* %23, i8** %24
%25 = icmp eq i8* null, %23
br i1 %25, label %26, label %27
26: ; preds = %18
store i32 0, i32* %3
br label %32
27: ; preds = %18
%28 = load i8**, i8*** %4
%29 = load i8*, i8** %28
%30 = load i8*, i8** %5
%31 = call i32 @strcpy(i8* %29, i8* %30)
store i32 1, i32* %3
br label %32
32: ; preds = %27, %26, %17
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
define i32 @ap_str_makestr(i8** nocapture %0, i8* %1) {
%3 = load i8*, i8** %0
%4 = icmp eq i8* %3, null
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @free(i8* %3)
br label %7
7: ; preds = %5, %2
store i8* null, i8** %0
%8 = icmp eq i8* %1, null
br i1 %8, label %17, label %9
9: ; preds = %7
%10 = tail call i64 @strlen(i8* %1)
%11 = add nsw i64 %10, 1
%12 = tail call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %0
%14 = icmp eq i64 %12, 0
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = tail call i32 @strcpy(i8* %13, i8* %1)
br label %17
17: ; preds = %9, %7, %15
%18 = phi i32 [ 1, %15 ], [ 0, %7 ], [ 0, %9 ]
ret i32 %18
}