#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 */
unsigned int get_arg_length(char const *);
char *get_arg_ptr(char const *, unsigned int);
int /*<<< orphan*/ strncpy(char *, char const *, unsigned int);
char *argncpy(const char *arg_str, unsigned int index, char *buffer,
              unsigned int length) {
  const char *ptr = get_arg_ptr(arg_str, index);
  unsigned int len;
  if (!ptr)
    return 0;
  len = get_arg_length(ptr);
  if (!strncpy(buffer, ptr, length))
    return 0;
  buffer[len] = 0;
  return buffer;
}


x86 assembly (clang, -O0):
<argncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <get_arg_ptr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    116e <argncpy+0x3e>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <argncpy+0x83>
mov    -0x30(%rbp),%rdi
call   1040 <get_arg_length@plt>
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <strncpy@plt>
cmp    $0x0,%eax
jne    11a0 <argncpy+0x70>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <argncpy+0x83>
mov    -0x20(%rbp),%rax
mov    -0x34(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<argncpy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%rbx
call   1050 <get_arg_ptr@plt>
test   %rax,%rax
je     1170 <argncpy+0x40>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <get_arg_length@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
test   %eax,%eax
je     1170 <argncpy+0x40>
mov    %r15d,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    1172 <argncpy+0x42>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @argncpy(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i8* @get_arg_ptr(i8* %12, i32 %13)
store i8* %14, i8** %10
%15 = load i8*, i8** %10
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %4
store i8* null, i8** %5
br label %33
18: ; preds = %4
%19 = load i8*, i8** %10
%20 = call i32 @get_arg_length(i8* %19)
store i32 %20, i32* %11
%21 = load i8*, i8** %8
%22 = load i8*, i8** %10
%23 = load i32, i32* %9
%24 = call i32 @strncpy(i8* %21, i8* %22, i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %18
store i8* null, i8** %5
br label %33
27: ; preds = %18
%28 = load i8*, i8** %8
%29 = load i32, i32* %11
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %8
store i8* %32, i8** %5
br label %33
33: ; preds = %27, %26, %17
%34 = load i8*, i8** %5
ret i8* %34
}

LLVM IR (-O2):
define i8* @argncpy(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i8* @get_arg_ptr(i8* %0, i32 %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %14, label %7
7: ; preds = %4
%8 = tail call i32 @get_arg_length(i8* %5)
%9 = tail call i32 @strncpy(i8* %2, i8* %5, i32 %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %7
%12 = zext i32 %8 to i64
%13 = getelementptr inbounds i8, i8* %2, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %7, %4, %11
%15 = phi i8* [ %2, %11 ], [ null, %4 ], [ null, %7 ]
ret i8* %15
}