#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, size_t);
size_t strnlen(char *, int);
void add_path(char *path_str, size_t *path_str_len, char *path) {
  size_t len = strnlen(path, 1024);
  if (*path_str_len + len + 2 > 1024) {
    fprintf(stderr, "clld: error: path buffer overflow\n");
    exit(-1);
  }
  strncpy(path_str + *path_str_len, path, len);
  *path_str_len += len + 1;
}


x86 assembly (clang, -O0):
<add_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x400,%esi
call   1040 <strnlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
add    -0x20(%rbp),%rax
add    $0x2,%rax
cmp    $0x400,%rax
jbe    11a0 <add_path+0x60>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
add    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rcx
add    $0x1,%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_path>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rdx,%rdi
mov    $0x400,%esi
call   1040 <strnlen@plt>
mov    %rax,%rbx
mov    (%r15),%rax
lea    (%rbx,%rax,1),%rcx
add    $0xfffffffffffffc01,%rcx
cmp    $0xfffffffffffffbfe,%rcx
jbe    11a2 <add_path+0x62>
add    %rax,%r12
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
mov    (%r15),%rax
add    %rbx,%rax
add    $0x1,%rax
mov    %rax,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [35 x i8] c"clld: error: path buffer overflow\0A\00"
define void @add_path(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 @strnlen(i8* %8, i32 1024)
store i64 %9, i64* %7
%10 = load i64*, i64** %5
%11 = load i64, i64* %10
%12 = load i64, i64* %7
%13 = add i64 %11, %12
%14 = add i64 %13, 2
%15 = icmp ugt i64 %14, 1024
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %3
%21 = load i8*, i8** %4
%22 = load i64*, i64** %5
%23 = load i64, i64* %22
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = call i32 @strncpy(i8* %24, i8* %25, i64 %26)
%28 = load i64, i64* %7
%29 = add i64 %28, 1
%30 = load i64*, i64** %5
%31 = load i64, i64* %30
%32 = add i64 %31, %29
store i64 %32, i64* %30
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [35 x i8] c"clld: error: path buffer overflow\0A\00"
define void @add_path(i8* %0, i64* nocapture %1, i8* %2) {
%4 = tail call i64 @strnlen(i8* %2, i32 1024)
%5 = load i64, i64* %1
%6 = add i64 %4, -1023
%7 = add i64 %6, %5
%8 = icmp ult i64 %7, -1025
br i1 %8, label %9, label %13
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %3
%14 = getelementptr inbounds i8, i8* %0, i64 %5
%15 = tail call i32 @strncpy(i8* %14, i8* %2, i64 %4)
%16 = add i64 %4, 1
%17 = load i64, i64* %1
%18 = add i64 %16, %17
store i64 %18, i64* %1
ret void
}