#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 */
char *mempcpy(char *, char const *, size_t);
size_t strlen(char const *);
size_t strpcpy(char **dest, size_t size, const char *src) {
  size_t len;
  len = strlen(src);
  if (len >= size) {
    if (size > 1)
      *dest = mempcpy(*dest, src, size - 1);
    size = 0;
  } else {
    if (len > 0) {
      *dest = mempcpy(*dest, src, len);
      size -= len;
    }
  }
  *dest[0] = '\0';
  return size;
}


x86 assembly (clang, -O0):
<strpcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jb     1199 <strpcpy+0x79>
cmpq   $0x1,-0x10(%rbp)
jbe    118c <strpcpy+0x6c>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    %rdi,-0x30(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
add    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x10(%rbp)
jmp    11e6 <strpcpy+0xc6>
cmpq   $0x0,-0x20(%rbp)
jbe    11e1 <strpcpy+0xc1>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    %rdi,-0x40(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    %rdx,-0x38(%rbp)
call   1040 <memcpy@plt>
mov    -0x40(%rbp),%rcx
mov    -0x38(%rbp),%rax
add    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11e6 <strpcpy+0xc6>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strpcpy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rdx,%rdi
call   1030 <strlen@plt>
cmp    %r12,%rax
jae    1163 <strpcpy+0x43>
mov    %rax,%rbx
test   %rax,%rax
je     118a <strpcpy+0x6a>
mov    (%r14),%r13
mov    %r13,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
add    %rbx,%r13
mov    %r13,(%r14)
sub    %rbx,%r12
jmp    118a <strpcpy+0x6a>
cmp    $0x2,%r12
jb     1187 <strpcpy+0x67>
mov    (%r14),%rbx
lea    -0x1(%r12),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
lea    (%rbx,%r12,1),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,(%r14)
xor    %r12d,%r12d
mov    (%r14),%rax
movb   $0x0,(%rax)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @strpcpy(i8** %0, i64 %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i8** %0, i8*** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %6
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = load i64, i64* %5
%12 = icmp uge i64 %10, %11
br i1 %12, label %13, label %25
13: ; preds = %3
%14 = load i64, i64* %5
%15 = icmp ugt i64 %14, 1
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = load i8**, i8*** %4
%18 = load i8*, i8** %17
%19 = load i8*, i8** %6
%20 = load i64, i64* %5
%21 = sub i64 %20, 1
%22 = call i8* @mempcpy(i8* %18, i8* %19, i64 %21)
%23 = load i8**, i8*** %4
store i8* %22, i8** %23
br label %24
24: ; preds = %16, %13
store i64 0, i64* %5
br label %39
25: ; preds = %3
%26 = load i64, i64* %7
%27 = icmp ugt i64 %26, 0
br i1 %27, label %28, label %38
28: ; preds = %25
%29 = load i8**, i8*** %4
%30 = load i8*, i8** %29
%31 = load i8*, i8** %6
%32 = load i64, i64* %7
%33 = call i8* @mempcpy(i8* %30, i8* %31, i64 %32)
%34 = load i8**, i8*** %4
store i8* %33, i8** %34
%35 = load i64, i64* %7
%36 = load i64, i64* %5
%37 = sub i64 %36, %35
store i64 %37, i64* %5
br label %38
38: ; preds = %28, %25
br label %39
39: ; preds = %38, %24
%40 = load i8**, i8*** %4
%41 = getelementptr inbounds i8*, i8** %40, i64 0
%42 = load i8*, i8** %41
store i8 0, i8* %42
%43 = load i64, i64* %5
ret i64 %43
}

LLVM IR (-O2):
define i64 @strpcpy(i8** nocapture %0, i64 %1, i8* nocapture %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%5 = icmp ult i64 %4, %1
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = icmp ugt i64 %1, 1
br i1 %7, label %8, label %18
8: ; preds = %6
%9 = load i8*, i8** %0
%10 = add i64 %1, -1
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %9, i8* align 1 %2, i64 %10, i1 false)
%11 = getelementptr inbounds i8, i8* %9, i64 %10
store i8* %11, i8** %0
br label %18
12: ; preds = %3
%13 = icmp eq i64 %4, 0
br i1 %13, label %18, label %14
14: ; preds = %12
%15 = load i8*, i8** %0
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 %15, i8* align 1 %2, i64 %4, i1 false)
%16 = getelementptr inbounds i8, i8* %15, i64 %4
store i8* %16, i8** %0
%17 = sub i64 %1, %4
br label %18
18: ; preds = %6, %8, %12, %14
%19 = phi i64 [ %17, %14 ], [ %1, %12 ], [ 0, %8 ], [ 0, %6 ]
%20 = load i8*, i8** %0
store i8 0, i8* %20
ret i64 %19
}