#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*/ ENOMEM;
int /*<<< orphan*/ errno;
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
scalar_t__ smtp_si_add_size_t(size_t, int, size_t *);
size_t strlen(char const *);
char *smtp_strdup(const char *s) {
  char *dup;
  size_t dup_len;
  size_t slen;
  slen = strlen(s);
  if (smtp_si_add_size_t(slen, 1, &dup_len)) {
    dup = NULL;
    errno = ENOMEM;
  } else if ((dup = malloc(dup_len)) != NULL) {
    memcpy(dup, s, dup_len);
  }
  return dup;
}


x86 assembly (clang, -O0):
<smtp_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
lea    -0x18(%rbp),%rdx
call   1040 <smtp_si_add_size_t@plt>
cmp    $0x0,%rax
je     1194 <smtp_strdup+0x54>
movq   $0x0,-0x10(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4b(%rip),%rax
mov    %ecx,(%rax)
jmp    11c1 <smtp_strdup+0x81>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     11bc <smtp_strdup+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
jmp    11c1 <smtp_strdup+0x81>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smtp_strdup>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
call   1030 <strlen@plt>
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
mov    $0x1,%esi
call   1040 <smtp_si_add_size_t@plt>
test   %rax,%rax
je     117e <smtp_strdup+0x3e>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    0x2e60(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
jmp    11a1 <smtp_strdup+0x61>
mov    0x8(%rsp),%r15
mov    %r15,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     117a <smtp_strdup+0x3a>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @smtp_strdup(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = call i64 @smtp_si_add_size_t(i64 %8, i32 1, i64* %4)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
store i8* null, i8** %3
%12 = load i32, i32* @ENOMEM
store i32 %12, i32* @errno
br label %23
13: ; preds = %1
%14 = load i64, i64* %4
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = load i8*, i8** %2
%20 = load i64, i64* %4
%21 = call i32 @memcpy(i8* %18, i8* %19, i64 %20)
br label %22
22: ; preds = %17, %13
br label %23
23: ; preds = %22, %11
%24 = load i8*, i8** %3
ret i8* %24
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i8* @smtp_strdup(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = call i64 @smtp_si_add_size_t(i64 %4, i32 1, i64* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = load i32, i32* @ENOMEM
store i32 %8, i32* @errno
br label %15
9: ; preds = %1
%10 = load i64, i64* %2
%11 = call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = call i32 @memcpy(i8* %11, i8* %0, i64 %10)
br label %15
15: ; preds = %9, %13, %7
%16 = phi i8* [ null, %7 ], [ %11, %13 ], [ null, %9 ]
ret i8* %16
}