#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 EINVAL;
int ENOMEM;
int /*<<< orphan*/ free(char *);
char *strdup(char *);
int strdup_between_offsets(void *stru_dst, void *stru_src, size_t offset) {
  char **src;
  char **dst;
  char *p = NULL;
  if (!stru_src || !stru_dst)
    return -EINVAL;
  src = (char **)((char *)stru_src + offset);
  dst = (char **)((char *)stru_dst + offset);
  if (*src) {
    p = strdup(*src);
    if (!p)
      return -ENOMEM;
  }
  free(*dst);
  *dst = p;
  return 0;
}


x86 assembly (clang, -O0):
<strdup_between_offsets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1152 <strdup_between_offsets+0x32>
cmpq   $0x0,-0x10(%rbp)
jne    1165 <strdup_between_offsets+0x45>
xor    %eax,%eax
mov    0x2e7d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <strdup_between_offsets+0xbc>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     11be <strdup_between_offsets+0x9e>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strdup@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11b9 <strdup_between_offsets+0x99>
xor    %eax,%eax
mov    0x2e31(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11dc <strdup_between_offsets+0xbc>
jmp    11be <strdup_between_offsets+0x9e>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdup_between_offsets>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     1159 <strdup_between_offsets+0x39>
test   %rsi,%rsi
je     1159 <strdup_between_offsets+0x39>
mov    %rdx,%r14
mov    %rdi,%r15
mov    (%rsi,%rdx,1),%rdi
xor    %ebx,%ebx
test   %rdi,%rdi
je     1166 <strdup_between_offsets+0x46>
call   1040 <strdup@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1169 <strdup_between_offsets+0x49>
mov    0x2e89(%rip),%rax
jmp    1160 <strdup_between_offsets+0x40>
mov    0x2e78(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    1176 <strdup_between_offsets+0x56>
xor    %r12d,%r12d
mov    (%r15,%r14,1),%rdi
call   1030 <free@plt>
mov    %r12,(%r15,%r14,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
@ENOMEM = global i32 0
define i32 @strdup_between_offsets(i8* %0, i8* %1, i64 %2) {
%4 = icmp ne i8* %1, null
%5 = icmp ne i8* %0, null
%6 = and i1 %5, %4
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %27
10: ; preds = %3
%11 = getelementptr inbounds i8, i8* %1, i64 %2
%12 = bitcast i8* %11 to i8**
%13 = getelementptr inbounds i8, i8* %0, i64 %2
%14 = bitcast i8* %13 to i8**
%15 = load i8*, i8** %12
%16 = icmp eq i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %10
%18 = tail call i8* @strdup(i8* %15)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @ENOMEM
%22 = sub nsw i32 0, %21
br label %27
23: ; preds = %17, %10
%24 = phi i8* [ %18, %17 ], [ null, %10 ]
%25 = load i8*, i8** %14
%26 = tail call i32 @free(i8* %25)
store i8* %24, i8** %14
br label %27
27: ; preds = %23, %20, %7
%28 = phi i32 [ 0, %23 ], [ %22, %20 ], [ %9, %7 ]
ret i32 %28
}