#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 *memdup(void const *, size_t);
int tls_set_mem(char **dest, size_t *destlen, const void *src, size_t srclen) {
  free(*dest);
  *dest = NULL;
  *destlen = 0;
  if (src != NULL)
    if ((*dest = memdup(src, srclen)) == NULL)
      return -1;
  *destlen = srclen;
  return 0;
}


x86 assembly (clang, -O0):
<tls_set_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1194 <tls_set_mem+0x74>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <memdup@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    118f <tls_set_mem+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <tls_set_mem+0x86>
jmp    1194 <tls_set_mem+0x74>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tls_set_mem>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
mov    (%rdi),%rdi
call   1030 <free@plt>
movq   $0x0,(%rbx)
movq   $0x0,(%r15)
test   %r12,%r12
je     1162 <tls_set_mem+0x42>
mov    %r12,%rdi
mov    %r14,%rsi
call   1040 <memdup@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     1169 <tls_set_mem+0x49>
mov    %r14,(%r15)
xor    %eax,%eax
jmp    116e <tls_set_mem+0x4e>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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