#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 *strdup(char const *);
char *strdup_to_offset(void *stru, size_t offset, const char *str) {
  char *n = NULL;
  char **o = (char **)((char *)stru + offset);
  if (str) {
    n = strdup(str);
    if (!n)
      return NULL;
  }
  free(*o);
  *o = n;
  return n;
}


x86 assembly (clang, -O0):
<strdup_to_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     117d <strdup_to_offset+0x5d>
mov    -0x20(%rbp),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1178 <strdup_to_offset+0x58>
movq   $0x0,-0x8(%rbp)
jmp    119c <strdup_to_offset+0x7c>
jmp    117d <strdup_to_offset+0x5d>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup_to_offset>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdx,%rdx
je     1144 <strdup_to_offset+0x24>
mov    %rdx,%rdi
call   1040 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1146 <strdup_to_offset+0x26>
xor    %ebx,%ebx
jmp    1153 <strdup_to_offset+0x33>
xor    %ebx,%ebx
mov    (%r15,%r14,1),%rdi
call   1030 <free@plt>
mov    %rbx,(%r15,%r14,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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