#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 *);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
int str_assign(char **pstr, char *text) {
  char *p;
  if (!text) {
    free(*pstr);
    *pstr = NULL;
    return 0;
  }
  p = realloc(*pstr, strlen(text) + 1);
  if (!p)
    return -1;
  *pstr = p;
  strcpy(p, text);
  return 0;
}


x86 assembly (clang, -O0):
<str_assign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117e <str_assign+0x3e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11dc <str_assign+0x9c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bd <str_assign+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <str_assign+0x9c>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_assign>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    (%rdi),%r15
test   %rsi,%rsi
je     117c <str_assign+0x3c>
mov    %rsi,%rbx
mov    %rsi,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %r15,%rdi
call   1060 <realloc@plt>
test   %rax,%rax
je     118f <str_assign+0x4f>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
jmp    118b <str_assign+0x4b>
mov    %r15,%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
xor    %eax,%eax
jmp    1194 <str_assign+0x54>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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