#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*/ fprintf(int /*<<< orphan*/, char *, char const *);
char *getenv(char const *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int copy_env(const char *name, char *buf, size_t bufsize) {
  if (bufsize > 0) {
    buf[0] = '\0';
  }
  const char *value = getenv(name);
  if (!value) {
    return 0;
  }
  size_t len = strlen(value);
  if (len + 1 > bufsize) {
    fprintf(stderr, "Env variable too long: %s\n", name);
    return 0;
  }
  memcpy(buf, value, len + 1);
  return 1;
}


x86 assembly (clang, -O0):
<copy_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    1166 <copy_env+0x26>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118a <copy_env+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    11ea <copy_env+0xaa>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
cmp    -0x20(%rbp),%rax
jbe    11ce <copy_env+0x8e>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe43(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ea <copy_env+0xaa>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1060 <memcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_env>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdx,%rdx
je     115a <copy_env+0x1a>
movb   $0x0,(%r14)
mov    %r15,%rdi
call   1030 <getenv@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     11ac <copy_env+0x6c>
mov    %rax,%rbp
mov    %rax,%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
cmp    %rbx,%rax
jbe    1198 <copy_env+0x58>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
jmp    11ac <copy_env+0x6c>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1060 <memcpy@plt>
mov    $0x1,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Env variable too long: %s\0A\00"
define i32 @copy_env(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i64, i64* %7
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = getelementptr inbounds i8, i8* %13, i64 0
store i8 0, i8* %14
br label %15
15: ; preds = %12, %3
%16 = load i8*, i8** %5
%17 = call i8* @getenv(i8* %16)
store i8* %17, i8** %8
%18 = load i8*, i8** %8
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %15
store i32 0, i32* %4
br label %38
21: ; preds = %15
%22 = load i8*, i8** %8
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %9
%24 = load i64, i64* %9
%25 = add i64 %24, 1
%26 = load i64, i64* %7
%27 = icmp ugt i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %30)
store i32 0, i32* %4
br label %38
32: ; preds = %21
%33 = load i8*, i8** %6
%34 = load i8*, i8** %8
%35 = load i64, i64* %9
%36 = add i64 %35, 1
%37 = call i32 @memcpy(i8* %33, i8* %34, i64 %36)
store i32 1, i32* %4
br label %38
38: ; preds = %32, %28, %20
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Env variable too long: %s\0A\00"
define i32 @copy_env(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %6, label %5
5: ; preds = %3
store i8 0, i8* %1
br label %6
6: ; preds = %5, %3
%7 = tail call i8* @getenv(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %18, label %9
9: ; preds = %6
%10 = tail call i64 @strlen(i8* %7)
%11 = add i64 %10, 1
%12 = icmp ugt i64 %11, %2
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %18
16: ; preds = %9
%17 = tail call i32 @memcpy(i8* %1, i8* %7, i64 %11)
br label %18
18: ; preds = %13, %16, %6
%19 = phi i32 [ 0, %6 ], [ 0, %13 ], [ 1, %16 ]
ret i32 %19
}