#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*/ abort();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
char *safeStrcpy(char *dest, const char *src, size_t maxLen) {
  size_t len = 0;
  while (src[len] != '\0') {
    if (!(len < maxLen - 1)) {
      fprintf(stderr, "safeStrcpy: String exceeds max length\n");
      abort();
    }
    len++;
    dest[len] = src[len];
  }
  dest[len + 1] = '\0';
  return dest;
}


x86 assembly (clang, -O0):
<safeStrcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11a9 <safeStrcpy+0x89>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     1182 <safeStrcpy+0x62>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    0xe8a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    113c <safeStrcpy+0x1c>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,0x1(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safeStrcpy>:
push   %rax
cmpb   $0x0,(%rsi)
je     114b <safeStrcpy+0x2b>
mov    $0x1,%eax
nopl   0x0(%rax,%rax,1)
cmp    %rax,%rdx
je     1159 <safeStrcpy+0x39>
movzbl (%rsi,%rax,1),%ecx
mov    %cl,(%rdi,%rax,1)
lea    0x1(%rax),%rcx
cmpb   $0x0,(%rsi,%rax,1)
mov    %rcx,%rax
jne    1130 <safeStrcpy+0x10>
jmp    1150 <safeStrcpy+0x30>
mov    $0x1,%ecx
movb   $0x0,(%rdi,%rcx,1)
mov    %rdi,%rax
pop    %rcx
ret
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [39 x i8] c"safeStrcpy: String exceeds max length\0A\00"
define i8* @safeStrcpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %7
br label %8
8: ; preds = %24, %3
%9 = load i8*, i8** %5
%10 = load i64, i64* %7
%11 = getelementptr inbounds i8, i8* %9, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %8
%16 = load i64, i64* %7
%17 = load i64, i64* %6
%18 = sub i64 %17, 1
%19 = icmp ult i64 %16, %18
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %15
%25 = load i64, i64* %7
%26 = add i64 %25, 1
store i64 %26, i64* %7
%27 = load i8*, i8** %5
%28 = load i64, i64* %7
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = load i8, i8* %29
%31 = load i8*, i8** %4
%32 = load i64, i64* %7
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 %30, i8* %33
br label %8
34: ; preds = %8
%35 = load i8*, i8** %4
%36 = load i64, i64* %7
%37 = add i64 %36, 1
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %4
ret i8* %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [39 x i8] c"safeStrcpy: String exceeds max length\0A\00"
define i8* @safeStrcpy(i8* returned writeonly %0, i8* nocapture %1, i64 %2) {
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %24, label %6
6: ; preds = %3
%7 = add i64 %2, -1
br label %8
8: ; preds = %6, %15
%9 = phi i64 [ 0, %6 ], [ %16, %15 ]
%10 = icmp eq i64 %9, %7
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %8
%16 = add i64 %9, 1
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = getelementptr inbounds i8, i8* %0, i64 %16
store i8 %18, i8* %19
%20 = load i8, i8* %17
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %8
22: ; preds = %15
%23 = add i64 %9, 2
br label %24
24: ; preds = %22, %3
%25 = phi i64 [ 1, %3 ], [ %23, %22 ]
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
ret i8* %0
}