#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*/ fprintf(int /*<<< orphan*/, char *, ...);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlcpy(char *, char *, int);
int strlen(char *);
int main(void) {
  size_t copied;
  char *src = malloc(100);
  if (src == NULL) {
    fprintf(stderr, "Memory allocation failure.\n");
    return 1;
  }
  strcpy(src, "Hey, dude!");
  char *dst = malloc(10);
  if (dst == NULL) {
    fprintf(stderr, "Memory allocation failure.\n");
    return 1;
  }
  copied = strlcpy(dst, src, 10);
  if (copied != 10)
    fprintf(stderr, "Expected 10 but got %zu for test #1.\n", copied);
  copied = strlcpy(dst, src, strlen(src) + 1);
  if (copied != 10)
    fprintf(stderr, "Expected 10 but got %zu for test #2.\n", copied);
  copied = strlcpy(NULL, src, 0);
  if (copied != 10)
    fprintf(stderr, "Expected 10 but got %zu for test #3.\n", copied);
  strlcpy(src + 9, src, strlen(src) + 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119b <main+0x4b>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x197>
mov    -0x18(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1030 <strcpy@plt>
mov    $0xa,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e7 <main+0x97>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x197>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strlcpy@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xa,-0x10(%rbp)
je     1223 <main+0xd3>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1070 <strlcpy@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xa,-0x10(%rbp)
je     1278 <main+0x128>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
call   1070 <strlcpy@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0xa,-0x10(%rbp)
je     12b1 <main+0x161>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1070 <strlcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    $0x64,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1244 <main+0xf4>
mov    %rax,%r15
lea    0xeaa(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    $0xa,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1244 <main+0xf4>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0xa,%edx
call   1070 <strlcpy@plt>
cmp    $0xa,%rax
je     11c0 <main+0x70>
mov    0x2e33(%rip),%rcx
mov    (%rcx),%edi
lea    0xe71(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <strlcpy@plt>
cmp    $0xa,%rax
je     11f6 <main+0xa6>
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%edi
lea    0xe61(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %r14d,%r14d
xor    %edi,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1070 <strlcpy@plt>
cmp    $0xa,%rax
je     1225 <main+0xd5>
mov    0x2dce(%rip),%rcx
mov    (%rcx),%edi
lea    0xe58(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15,%rbx
add    $0x9,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <strlcpy@plt>
jmp    1261 <main+0x111>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Memory allocation failure.\0A\00"
@.str.1 = [11 x i8] c"Hey, dude!\00"
@.str.2 = [38 x i8] c"Expected 10 but got %zu for test.\0A\00"
@.str.3 = [38 x i8] c"Expected 10 but got %zu for test.\0A\00"
@.str.4 = [38 x i8] c"Expected 10 but got %zu for test.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i8* @malloc(i32 100)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %60
11: ; preds = %0
%12 = load i8*, i8** %3
%13 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%14 = call i8* @malloc(i32 10)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %60
20: ; preds = %11
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = call i64 @strlcpy(i8* %21, i8* %22, i32 10)
store i64 %23, i64* %2
%24 = load i64, i64* %2
%25 = icmp ne i64 %24, 10
br i1 %25, label %26, label %30
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i64, i64* %2
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i64 %28)
br label %30
30: ; preds = %26, %20
%31 = load i8*, i8** %4
%32 = load i8*, i8** %3
%33 = load i8*, i8** %3
%34 = call i32 @strlen(i8* %33)
%35 = add nsw i32 %34, 1
%36 = call i64 @strlcpy(i8* %31, i8* %32, i32 %35)
store i64 %36, i64* %2
%37 = load i64, i64* %2
%38 = icmp ne i64 %37, 10
br i1 %38, label %39, label %43
39: ; preds = %30
%40 = load i32, i32* @stderr
%41 = load i64, i64* %2
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i64 %41)
br label %43
43: ; preds = %39, %30
%44 = load i8*, i8** %3
%45 = call i64 @strlcpy(i8* null, i8* %44, i32 0)
store i64 %45, i64* %2
%46 = load i64, i64* %2
%47 = icmp ne i64 %46, 10
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = load i64, i64* %2
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i64 %50)
br label %52
52: ; preds = %48, %43
%53 = load i8*, i8** %3
%54 = getelementptr inbounds i8, i8* %53, i64 9
%55 = load i8*, i8** %3
%56 = load i8*, i8** %3
%57 = call i32 @strlen(i8* %56)
%58 = add nsw i32 %57, 1
%59 = call i64 @strlcpy(i8* %54, i8* %55, i32 %58)
store i32 0, i32* %1
br label %60
60: ; preds = %52, %17, %8
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Memory allocation failure.\0A\00"
@.str.1 = [11 x i8] c"Hey, dude!\00"
@.str.2 = [38 x i8] c"Expected 10 but got %zu for test.\0A\00"
@.str.3 = [38 x i8] c"Expected 10 but got %zu for test.\0A\00"
@.str.4 = [38 x i8] c"Expected 10 but got %zu for test.\0A\00"
define i32 @main() {
%1 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%2 = icmp eq i8* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %38
6: ; preds = %0
%7 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %6
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %38
13: ; preds = %6
%14 = tail call i64 @strlcpy(i8* %8, i8* %1, i32 10)
%15 = icmp eq i64 %14, 10
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i64 %14)
br label %19
19: ; preds = %16, %13
%20 = tail call i32 @strlen(i8* %1)
%21 = add nsw i32 %20, 1
%22 = tail call i64 @strlcpy(i8* %8, i8* %1, i32 %21)
%23 = icmp eq i64 %22, 10
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i64 %22)
br label %27
27: ; preds = %24, %19
%28 = tail call i64 @strlcpy(i8* null, i8* %1, i32 0)
%29 = icmp eq i64 %28, 10
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i64 %28)
br label %33
33: ; preds = %30, %27
%34 = getelementptr inbounds i8, i8* %1, i64 9
%35 = tail call i32 @strlen(i8* %1)
%36 = add nsw i32 %35, 1
%37 = tail call i64 @strlcpy(i8* %34, i8* %1, i32 %36)
br label %38
38: ; preds = %10, %33, %3
%39 = phi i32 [ 1, %3 ], [ 1, %10 ], [ 0, %33 ]
ret i32 %39
}