#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 */
struct szl_obj {
  int dummy;
};
struct szl_interp {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
char *strndup(char *, size_t);
int /*<<< orphan*/ szl_as_str(struct szl_interp *, struct szl_obj *, char **,
                              size_t *);
int /*<<< orphan*/ szl_set_last_strerror(struct szl_interp *,
                                         int /*<<< orphan*/);
__attribute__((nonnull(1, 2, 3))) char *
szl_strdup(struct szl_interp *interp, struct szl_obj *obj, size_t *len) {
  char *s, *s2;
  if (!szl_as_str(interp, obj, &s, len))
    return NULL;
  s2 = strndup(s, *len);
  if (!s2)
    szl_set_last_strerror(interp, ENOMEM);
  return s2;
}


x86 assembly (clang, -O0):
<szl_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdx
call   1050 <szl_as_str@plt>
cmp    $0x0,%eax
jne    116f <szl_strdup+0x3f>
movq   $0x0,-0x8(%rbp)
jmp    11a8 <szl_strdup+0x78>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strndup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a0 <szl_strdup+0x70>
mov    -0x10(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1040 <szl_set_last_strerror@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<szl_strdup>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rdi,%r14
mov    %rsp,%rdx
mov    %rbx,%rcx
call   1050 <szl_as_str@plt>
test   %eax,%eax
je     116b <szl_strdup+0x3b>
mov    (%rsp),%rdi
mov    (%rbx),%rsi
call   1030 <strndup@plt>
test   %rax,%rax
jne    116d <szl_strdup+0x3d>
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <szl_set_last_strerror@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.szl_interp = type { i32 }
%struct.szl_obj = type { i32 }
@ENOMEM = global i32 0
define i8* @szl_strdup(%struct.szl_interp* %0, %struct.szl_obj* %1, i64* %2) {
%4 = alloca i8*
%5 = alloca %struct.szl_interp*
%6 = alloca %struct.szl_obj*
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i8*
store %struct.szl_interp* %0, %struct.szl_interp** %5
store %struct.szl_obj* %1, %struct.szl_obj** %6
store i64* %2, i64** %7
%10 = load %struct.szl_interp*, %struct.szl_interp** %5
%11 = load %struct.szl_obj*, %struct.szl_obj** %6
%12 = load i64*, i64** %7
%13 = call i32 @szl_as_str(%struct.szl_interp* %10, %struct.szl_obj* %11, i8** %8, i64* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %3
store i8* null, i8** %4
br label %29
16: ; preds = %3
%17 = load i8*, i8** %8
%18 = load i64*, i64** %7
%19 = load i64, i64* %18
%20 = call i8* @strndup(i8* %17, i64 %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
%22 = icmp ne i8* %21, null
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = load %struct.szl_interp*, %struct.szl_interp** %5
%25 = load i32, i32* @ENOMEM
%26 = call i32 @szl_set_last_strerror(%struct.szl_interp* %24, i32 %25)
br label %27
27: ; preds = %23, %16
%28 = load i8*, i8** %9
store i8* %28, i8** %4
br label %29
29: ; preds = %27, %15
%30 = load i8*, i8** %4
ret i8* %30
}

LLVM IR (-O2):
%struct.szl_interp = type { i32 }
%struct.szl_obj = type { i32 }
@ENOMEM = global i32 0
define noalias i8* @szl_strdup(%struct.szl_interp* %0, %struct.szl_obj* %1, i64* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i32 @szl_as_str(%struct.szl_interp* %0, %struct.szl_obj* %1, i8** %4, i64* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = load i64, i64* %2
%11 = call i8* @strndup(i8* %9, i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = load i32, i32* @ENOMEM
%15 = call i32 @szl_set_last_strerror(%struct.szl_interp* %0, i32 %14)
br label %16
16: ; preds = %8, %13, %3
%17 = phi i8* [ null, %3 ], [ null, %13 ], [ %11, %8 ]
ret i8* %17
}