#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*/ strcpy(char *, char const *);
size_t strlen(char const *);
size_t stb_strscpy(char *d, const char *s, size_t n) {
  size_t len = strlen(s);
  if (len >= n) {
    if (n)
      d[0] = 0;
    return 0;
  }
  strcpy(d, s);
  return len + 1;
}


x86 assembly (clang, -O0):
<stb_strscpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     116e <stb_strscpy+0x4e>
cmpq   $0x0,-0x20(%rbp)
je     1161 <stb_strscpy+0x41>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1187 <stb_strscpy+0x67>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stb_strscpy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    %r12,%rax
jae    1152 <stb_strscpy+0x32>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
add    $0x1,%rbx
jmp    115d <stb_strscpy+0x3d>
test   %r12,%r12
je     115b <stb_strscpy+0x3b>
movb   $0x0,(%r14)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @stb_strscpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp uge i64 %11, %12
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load i64, i64* %7
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 0, i8* %19
br label %20
20: ; preds = %17, %14
store i64 0, i64* %4
br label %27
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = load i8*, i8** %6
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = load i64, i64* %8
%26 = add i64 %25, 1
store i64 %26, i64* %4
br label %27
27: ; preds = %21, %20
%28 = load i64, i64* %4
ret i64 %28
}

LLVM IR (-O2):
define i64 @stb_strscpy(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = icmp ult i64 %4, %2
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = icmp eq i64 %2, 0
br i1 %7, label %12, label %8
8: ; preds = %6
store i8 0, i8* %0
br label %12
9: ; preds = %3
%10 = tail call i32 @strcpy(i8* %0, i8* %1)
%11 = add nuw i64 %4, 1
br label %12
12: ; preds = %6, %8, %9
%13 = phi i64 [ %11, %9 ], [ 0, %8 ], [ 0, %6 ]
ret i64 %13
}