#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*/ lib_free(char *);
char *lib_realloc(char *, scalar_t__);
char *lib_stralloc(char const *);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int util_string_set(char **str, const char *new_value) {
  if (*str == NULL) {
    if (new_value != NULL)
      *str = lib_stralloc(new_value);
  } else {
    if (new_value == NULL) {
      lib_free(*str);
      *str = NULL;
    } else {
      if (strcmp(*str, new_value) == 0)
        return -1;
      *str = lib_realloc(*str, strlen(new_value) + 1);
      strcpy(*str, new_value);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<util_string_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a1 <util_string_set+0x41>
cmpq   $0x0,-0x18(%rbp)
je     119c <util_string_set+0x3c>
mov    -0x18(%rbp),%rdi
call   1040 <lib_stralloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1231 <util_string_set+0xd1>
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <util_string_set+0x68>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <lib_free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
jmp    122c <util_string_set+0xcc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    11ee <util_string_set+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <util_string_set+0xd8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1080 <lib_realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1231 <util_string_set+0xd1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_string_set>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rdi),%rdi
test   %rdi,%rdi
je     11ab <util_string_set+0x4b>
test   %rbx,%rbx
je     11bf <util_string_set+0x5f>
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     11cf <util_string_set+0x6f>
mov    (%r14),%rbp
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %rbp,%rdi
call   1080 <lib_realloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    11cb <util_string_set+0x6b>
xor    %ebp,%ebp
test   %rbx,%rbx
je     11d4 <util_string_set+0x74>
mov    %rbx,%rdi
call   1040 <lib_stralloc@plt>
mov    %rax,(%r14)
jmp    11d4 <util_string_set+0x74>
call   1050 <lib_free@plt>
movq   $0x0,(%r14)
xor    %ebp,%ebp
jmp    11d4 <util_string_set+0x74>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @util_string_set(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 eq i8* %7, null
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i8*, i8** %5
%14 = call i8* @lib_stralloc(i8* %13)
%15 = load i8**, i8*** %4
store i8* %14, i8** %15
br label %16
16: ; preds = %12, %9
br label %45
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = call i32 @lib_free(i8* %22)
%24 = load i8**, i8*** %4
store i8* null, i8** %24
br label %44
25: ; preds = %17
%26 = load i8**, i8*** %4
%27 = load i8*, i8** %26
%28 = load i8*, i8** %5
%29 = call i64 @strcmp(i8* %27, i8* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %3
br label %46
32: ; preds = %25
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = load i8*, i8** %5
%36 = call i64 @strlen(i8* %35)
%37 = add nsw i64 %36, 1
%38 = call i8* @lib_realloc(i8* %34, i64 %37)
%39 = load i8**, i8*** %4
store i8* %38, i8** %39
%40 = load i8**, i8*** %4
%41 = load i8*, i8** %40
%42 = load i8*, i8** %5
%43 = call i32 @strcpy(i8* %41, i8* %42)
br label %44
44: ; preds = %32, %20
br label %45
45: ; preds = %44, %16
store i32 0, i32* %3
br label %46
46: ; preds = %45, %31
%47 = load i32, i32* %3
ret i32 %47
}

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