#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*/ memcpy(char **, char **, int);
char **xmalloc(int);
const char **copy_env(char **env) {
  char **s;
  int n = 1;
  for (s = env; *s; s++)
    n++;
  s = xmalloc(n * sizeof(const char *));
  memcpy(s, env, n * sizeof(const char *));
  return (const char **)s;
}


x86 assembly (clang, -O0):
<copy_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1163 <copy_env+0x43>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    113b <copy_env+0x1b>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1030 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_env>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
cmpq   $0x0,(%rdi)
je     114d <copy_env+0x2d>
mov    $0x8,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpq   $0x0,(%r14,%rbx,1)
lea    0x8(%rbx),%rbx
jne    1140 <copy_env+0x20>
jmp    1152 <copy_env+0x32>
mov    $0x8,%ebx
mov    %ebx,%edi
call   1030 <xmalloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8** @copy_env(i8** %0) {
%2 = alloca i8**
%3 = alloca i8**
%4 = alloca i32
store i8** %0, i8*** %2
store i32 1, i32* %4
%5 = load i8**, i8*** %2
store i8** %5, i8*** %3
br label %6
6: ; preds = %13, %1
%7 = load i8**, i8*** %3
%8 = load i8*, i8** %7
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = load i32, i32* %4
%12 = add nsw i32 %11, 1
store i32 %12, i32* %4
br label %13
13: ; preds = %10
%14 = load i8**, i8*** %3
%15 = getelementptr inbounds i8*, i8** %14, i32 1
store i8** %15, i8*** %3
br label %6
16: ; preds = %6
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = call i8** @xmalloc(i32 %20)
store i8** %21, i8*** %3
%22 = load i8**, i8*** %3
%23 = load i8**, i8*** %2
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 8
%27 = trunc i64 %26 to i32
%28 = call i32 @memcpy(i8** %22, i8** %23, i32 %27)
%29 = load i8**, i8*** %3
ret i8** %29
}

LLVM IR (-O2):
define i8** @copy_env(i8** %0) {
%2 = load i8*, i8** %0
%3 = icmp eq i8* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1, %4
%5 = phi i32 [ %7, %4 ], [ 1, %1 ]
%6 = phi i8** [ %8, %4 ], [ %0, %1 ]
%7 = add nuw nsw i32 %5, 1
%8 = getelementptr inbounds i8*, i8** %6, i64 1
%9 = load i8*, i8** %8
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %4
11: ; preds = %4
%12 = shl i32 %7, 3
br label %13
13: ; preds = %11, %1
%14 = phi i32 [ 8, %1 ], [ %12, %11 ]
%15 = tail call i8** @xmalloc(i32 %14)
%16 = tail call i32 @memcpy(i8** %15, i8** %0, i32 %14)
ret i8** %15
}