#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 ERANGE;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
int strcpy_s(char *dst, size_t dst_size, const char *src) {
  size_t src_size;
  if (dst == NULL || src == NULL)
    return EINVAL;
  src_size = strlen(src);
  if (src_size + 1 > dst_size)
    return ERANGE;
  memcpy(dst, src, src_size);
  dst[src_size] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<strcpy_s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     114a <strcpy_s+0x2a>
cmpq   $0x0,-0x20(%rbp)
jne    115b <strcpy_s+0x3b>
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <strcpy_s+0x8f>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jbe    118b <strcpy_s+0x6b>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11af <strcpy_s+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcpy_s>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     115a <strcpy_s+0x3a>
mov    %rdx,%rbx
test   %rdx,%rdx
je     115a <strcpy_s+0x3a>
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r12
add    $0x1,%rax
cmp    %r15,%rax
jbe    1165 <strcpy_s+0x45>
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
jmp    117a <strcpy_s+0x5a>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
jmp    117a <strcpy_s+0x5a>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%r14,%r12,1)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@ERANGE = global i32 0
define i32 @strcpy_s(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%9 = load i8*, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11, %3
%15 = load i32, i32* @EINVAL
store i32 %15, i32* %4
br label %33
16: ; preds = %11
%17 = load i8*, i8** %7
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = add i64 %19, 1
%21 = load i64, i64* %6
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = load i32, i32* @ERANGE
store i32 %24, i32* %4
br label %33
25: ; preds = %16
%26 = load i8*, i8** %5
%27 = load i8*, i8** %7
%28 = load i64, i64* %8
%29 = call i32 @memcpy(i8* %26, i8* %27, i64 %28)
%30 = load i8*, i8** %5
%31 = load i64, i64* %8
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 0, i8* %32
store i32 0, i32* %4
br label %33
33: ; preds = %25, %23, %14
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@EINVAL = global i32 0
@ERANGE = global i32 0
define i32 @strcpy_s(i8* %0, i64 %1, i8* %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %2, null
%6 = or i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = load i32, i32* @EINVAL
br label %18
9: ; preds = %3
%10 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%11 = add i64 %10, 1
%12 = icmp ugt i64 %11, %1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = load i32, i32* @ERANGE
br label %18
15: ; preds = %9
%16 = tail call i32 @memcpy(i8* %0, i8* %2, i64 %10)
%17 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %17
br label %18
18: ; preds = %15, %13, %7
%19 = phi i32 [ %8, %7 ], [ %14, %13 ], [ 0, %15 ]
ret i32 %19
}