#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(unsigned int);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
void read_str(char **str, char *value) {
  if (*str != NULL)
    (void)free(*str);
  if (value != NULL && strlen(value)) {
    *str = (char *)malloc((unsigned)(strlen(value) + 1));
    (void)strcpy(*str, value);
  } else
    *str = NULL;
}


x86 assembly (clang, -O0):
<read_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     116a <read_str+0x2a>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x10(%rbp)
je     11bb <read_str+0x7b>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
je     11bb <read_str+0x7b>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11c6 <read_str+0x86>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_str>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rdi),%rdi
test   %rdi,%rdi
je     1157 <read_str+0x17>
call   1030 <free@plt>
test   %r14,%r14
je     1188 <read_str+0x48>
mov    %r14,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     1188 <read_str+0x48>
add    $0x1,%eax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <strcpy@plt>
movq   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define void @read_str(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
%8 = icmp eq i8* %1, null
br i1 %8, label %18, label %9
9: ; preds = %7
%10 = tail call i64 @strlen(i8* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %18, label %12
12: ; preds = %9
%13 = trunc i64 %10 to i32
%14 = add i32 %13, 1
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %0
%17 = tail call i32 @strcpy(i8* %16, i8* %1)
br label %19
18: ; preds = %9, %7
store i8* null, i8** %0
br label %19
19: ; preds = %18, %12
ret void
}