#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 */
char *realloc(char *, long);
long strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, long);
int zn_strset(char **dest, const char *src) {
  if (src == NULL || dest == NULL)
    return -1;
  long len = strlen(src);
  char *ptr = *dest;
  ptr = realloc(ptr, len + 1);
  if (ptr == NULL) {
    ptr = realloc(ptr, len + 1);
    if (ptr == NULL)
      return -1;
  }
  strncpy(ptr, src, len);
  ptr[len] = '\0';
  *dest = ptr;
  return 0;
}


x86 assembly (clang, -O0):
<zn_strset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1156 <zn_strset+0x26>
cmpq   $0x0,-0x10(%rbp)
jne    1162 <zn_strset+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <zn_strset+0xca>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cb <zn_strset+0x9b>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c6 <zn_strset+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fa <zn_strset+0xca>
jmp    11cb <zn_strset+0x9b>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zn_strset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11ad <zn_strset+0x6d>
mov    %rsi,%r12
test   %rsi,%rsi
je     11ad <zn_strset+0x6d>
mov    %rdi,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    (%rbx),%rdi
lea    0x1(%rax),%r13
mov    %r13,%rsi
call   1060 <realloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1193 <zn_strset+0x53>
mov    %r13,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11ad <zn_strset+0x6d>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,0x0(%rbp,%r15,1)
mov    %rbp,(%rbx)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @zn_strset(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8**, i8*** %4
%12 = icmp eq i8** %11, null
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i32 -1, i32* %3
br label %44
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %6
%17 = load i8**, i8*** %4
%18 = load i8*, i8** %17
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i64, i64* %6
%21 = add nsw i64 %20, 1
%22 = call i8* @realloc(i8* %19, i64 %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %34
25: ; preds = %14
%26 = load i8*, i8** %7
%27 = load i64, i64* %6
%28 = add nsw i64 %27, 1
%29 = call i8* @realloc(i8* %26, i64 %28)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %3
br label %44
33: ; preds = %25
br label %34
34: ; preds = %33, %14
%35 = load i8*, i8** %7
%36 = load i8*, i8** %5
%37 = load i64, i64* %6
%38 = call i32 @strncpy(i8* %35, i8* %36, i64 %37)
%39 = load i8*, i8** %7
%40 = load i64, i64* %6
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %7
%43 = load i8**, i8*** %4
store i8* %42, i8** %43
store i32 0, i32* %3
br label %44
44: ; preds = %34, %32, %13
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
define i32 @zn_strset(i8** %0, i8* %1) {
%3 = icmp eq i8* %1, null
%4 = icmp eq i8** %0, null
%5 = or i1 %4, %3
br i1 %5, label %19, label %6
6: ; preds = %2
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = load i8*, i8** %0
%9 = add nsw i64 %7, 1
%10 = tail call i8* @realloc(i8* %8, i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = tail call i8* @malloc(i64 %9)
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %12, %6
%16 = phi i8* [ %13, %12 ], [ %10, %6 ]
%17 = tail call i32 @strncpy(i8* %16, i8* %1, i64 %7)
%18 = getelementptr inbounds i8, i8* %16, i64 %7
store i8 0, i8* %18
store i8* %16, i8** %0
br label %19
19: ; preds = %15, %12, %2
%20 = phi i32 [ -1, %2 ], [ 0, %15 ], [ -1, %12 ]
ret i32 %20
}