#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 PATH_MAX;
int _strlen(char const *, size_t);
int cat_path(char *, size_t, char const *, char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int ncat_path(char *buf, size_t len, const char *dir, const char *base,
              size_t blen) {
  char name[PATH_MAX + 1];
  int alen = _strlen(base, blen);
  if (blen > PATH_MAX || !alen)
    return 0;
  strncpy(name, base, alen);
  name[alen] = '\0';
  return cat_path(buf, len, dir, name);
}


x86 assembly (clang, -O0):
<ncat_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <_strlen@plt>
mov    %eax,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e5a(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
ja     11ac <ncat_path+0x7c>
cmpl   $0x0,-0x44(%rbp)
jne    11bf <ncat_path+0x8f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    11f6 <ncat_path+0xc6>
mov    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x50(%rbp),%rcx
movslq -0x44(%rbp),%rax
movb   $0x0,(%rcx,%rax,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <cat_path@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ncat_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,%r15
mov    %rcx,%r13
mov    %rdx,-0x40(%rbp)
mov    %rsi,-0x38(%rbp)
mov    %rdi,-0x30(%rbp)
mov    0x2e96(%rip),%r12
mov    (%r12),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rcx,%rdi
mov    %r8,%rsi
call   1040 <_strlen@plt>
mov    %eax,%r14d
movslq (%r12),%rcx
xor    %eax,%eax
cmp    %r15,%rcx
jb     11b9 <ncat_path+0x89>
test   %r14d,%r14d
je     11b9 <ncat_path+0x89>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    %rbx,%rcx
call   1050 <cat_path@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
define i32 @ncat_path(i8* %0, i64 %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%16 = load i32, i32* @PATH_MAX
%17 = add nsw i32 %16, 1
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %12
%20 = alloca i8, i64 %18
store i64 %18, i64* %13
%21 = load i8*, i8** %10
%22 = load i64, i64* %11
%23 = call i32 @_strlen(i8* %21, i64 %22)
store i32 %23, i32* %14
%24 = load i64, i64* %11
%25 = load i32, i32* @PATH_MAX
%26 = sext i32 %25 to i64
%27 = icmp ugt i64 %24, %26
br i1 %27, label %31, label %28
28: ; preds = %5
%29 = load i32, i32* %14
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %28, %5
store i32 0, i32* %6
store i32 1, i32* %15
br label %43
32: ; preds = %28
%33 = load i8*, i8** %10
%34 = load i32, i32* %14
%35 = call i32 @strncpy(i8* %20, i8* %33, i32 %34)
%36 = load i32, i32* %14
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %20, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %7
%40 = load i64, i64* %8
%41 = load i8*, i8** %9
%42 = call i32 @cat_path(i8* %39, i64 %40, i8* %41, i8* %20)
store i32 %42, i32* %6
store i32 1, i32* %15
br label %43
43: ; preds = %32, %31
%44 = load i8*, i8** %12
%45 = load i32, i32* %6
ret i32 %45
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
define i32 @ncat_path(i8* %0, i64 %1, i8* %2, i8* %3, i64 %4) {
%6 = load i32, i32* @PATH_MAX
%7 = add nsw i32 %6, 1
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = tail call i32 @_strlen(i8* %3, i64 %4)
%11 = load i32, i32* @PATH_MAX
%12 = sext i32 %11 to i64
%13 = icmp uge i64 %12, %4
%14 = icmp ne i32 %10, 0
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %16, label %21
16: ; preds = %5
%17 = call i32 @strncpy(i8* %9, i8* %3, i32 %10)
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds i8, i8* %9, i64 %18
store i8 0, i8* %19
%20 = call i32 @cat_path(i8* %0, i64 %1, i8* %2, i8* %9)
br label %21
21: ; preds = %5, %16
%22 = phi i32 [ %20, %16 ], [ 0, %5 ]
ret i32 %22
}