#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 arglen(char *, char *, int, int);
int /*<<< orphan*/ argncpy(char *, int, char *, char *, int, int);
char *malloc(int);
char *argdup(char *src, char *sep, int arg, int opt) {
  char *dst;
  int l;
  l = arglen(src, sep, arg, opt);
  if (l < 0)
    return (NULL);
  dst = malloc(++l);
  if (!dst)
    return (NULL);
  argncpy(dst, l, src, sep, arg, opt);
  return (dst);
}


x86 assembly (clang, -O0):
<argdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1030 <arglen@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1173 <argdup+0x43>
movq   $0x0,-0x8(%rbp)
jmp    11c1 <argdup+0x91>
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
mov    %edi,-0x2c(%rbp)
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119d <argdup+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    11c1 <argdup+0x91>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
mov    -0x20(%rbp),%r9d
call   1040 <argncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<argdup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%r13
call   1030 <arglen@plt>
test   %eax,%eax
js     117c <argdup+0x4c>
mov    %eax,%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     117c <argdup+0x4c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
mov    %r12,%rcx
mov    %r15d,%r8d
mov    %r14d,%r9d
call   1040 <argncpy@plt>
jmp    117e <argdup+0x4e>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @argdup(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = load i8*, i8** %7
%14 = load i32, i32* %8
%15 = load i32, i32* %9
%16 = call i32 @arglen(i8* %12, i8* %13, i32 %14, i32 %15)
store i32 %16, i32* %11
%17 = load i32, i32* %11
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %4
store i8* null, i8** %5
br label %36
20: ; preds = %4
%21 = load i32, i32* %11
%22 = add nsw i32 %21, 1
store i32 %22, i32* %11
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %20
store i8* null, i8** %5
br label %36
27: ; preds = %20
%28 = load i8*, i8** %10
%29 = load i32, i32* %11
%30 = load i8*, i8** %6
%31 = load i8*, i8** %7
%32 = load i32, i32* %8
%33 = load i32, i32* %9
%34 = call i32 @argncpy(i8* %28, i32 %29, i8* %30, i8* %31, i32 %32, i32 %33)
%35 = load i8*, i8** %10
store i8* %35, i8** %5
br label %36
36: ; preds = %27, %26, %19
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
define i8* @argdup(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i32 @arglen(i8* %0, i8* %1, i32 %2, i32 %3)
%6 = icmp slt i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = add nuw nsw i32 %5, 1
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @argncpy(i8* %9, i32 %8, i8* %0, i8* %1, i32 %2, i32 %3)
br label %13
13: ; preds = %7, %4, %11
%14 = phi i8* [ %9, %11 ], [ null, %4 ], [ null, %7 ]
ret i8* %14
}