#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*/ die(char *, char const *const, int const);
void strlimcpy(char *dest, const char *const source, const int limit) {
  int n = (int)limit;
  const char *s = source;
  while (n--)
    if (!(*dest++ = *s++))
      return;
  die("buffer overflow. strlimcpy(dest, '%s', %d).", source, limit);
}


x86 assembly (clang, -O0):
<strlimcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x18(%rbp)
cmp    $0x0,%eax
je     1179 <strlimcpy+0x69>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    (%rax),%al
mov    -0x8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %al,(%rcx)
cmp    $0x0,%al
jne    1174 <strlimcpy+0x64>
jmp    118c <strlimcpy+0x7c>
jmp    1131 <strlimcpy+0x21>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe79(%rip),%rdi
call   1030 <die@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlimcpy>:
mov    %edx,%edx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    %eax,%edx
je     1134 <strlimcpy+0x24>
movzbl (%rsi,%rax,1),%ecx
mov    %cl,(%rdi,%rax,1)
add    $0x1,%rax
test   %cl,%cl
jne    1120 <strlimcpy+0x10>
ret
lea    0xec5(%rip),%rdi
jmp    1030 <die@plt>

LLVM IR (-O0):
@.str = [44 x i8] c"buffer overflow. strlimcpy(dest, '%s', %d).\00"
define void @strlimcpy(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
store i32 %9, i32* %7
%10 = load i8*, i8** %5
store i8* %10, i8** %8
br label %11
11: ; preds = %23, %3
%12 = load i32, i32* %7
%13 = add nsw i32 %12, -1
store i32 %13, i32* %7
%14 = icmp ne i32 %12, 0
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = load i8*, i8** %8
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %8
%18 = load i8, i8* %16
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %4
store i8 %18, i8* %19
%21 = icmp ne i8 %18, 0
br i1 %21, label %23, label %22
22: ; preds = %15
br label %28
23: ; preds = %15
br label %11
24: ; preds = %11
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = call i32 @die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26)
br label %28
28: ; preds = %24, %22
ret void
}

LLVM IR (-O2):
@.str = [44 x i8] c"buffer overflow. strlimcpy(dest, '%s', %d).\00"
define void @strlimcpy(i8* nocapture writeonly %0, i8* %1, i32 %2) {
br label %4
4: ; preds = %9, %3
%5 = phi i8* [ %0, %3 ], [ %13, %9 ]
%6 = phi i32 [ %2, %3 ], [ %10, %9 ]
%7 = phi i8* [ %1, %3 ], [ %11, %9 ]
%8 = icmp eq i32 %6, 0
br i1 %8, label %15, label %9
9: ; preds = %4
%10 = add nsw i32 %6, -1
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = load i8, i8* %7
%13 = getelementptr inbounds i8, i8* %5, i64 1
store i8 %12, i8* %5
%14 = icmp eq i8 %12, 0
br i1 %14, label %17, label %4
15: ; preds = %4
%16 = tail call i32 @die(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %2)
br label %17
17: ; preds = %9, %15
ret void
}