#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*/ assert(char const *);
char *getenv(char const *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *read_env(const char *env, char *s, size_t len) {
  assert(env);
  assert(s);
  char *val;
  if (!(val = getenv(env))) {
    return (NULL);
  }
  strncpy(s, val, len - 1);
  s[len - 1] = 0;
  return (s);
}


x86 assembly (clang, -O0):
<read_env>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117a <read_env+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    11a7 <read_env+0x77>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_env>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1050 <assert@plt>
mov    %r15,%rdi
call   1050 <assert@plt>
mov    %rbx,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     116f <read_env+0x3f>
lea    -0x1(%r14),%rdx
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <strncpy@plt>
movb   $0x0,-0x1(%r15,%r14,1)
jmp    1172 <read_env+0x42>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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