#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 */
char *appendtostring(char *, int);
int /*<<< orphan*/ exit(int);
char *getenv(char *);
int /*<<< orphan*/ perror(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void fortrangetenv_(char *varnm, char *envnm, int *lenstr, int *status,
                    int varnmlength, int envnmlength) {
  char *varname = "getenv";
  char *envname;
  if (!(varname = appendtostring(varnm, varnmlength))) {
    perror("Failed : appendtostring (varname) in posixwrapper:fortrangetenv");
    exit(1);
  }
  if (!(envname = getenv(varname))) {
    perror("Failed : getenv in posixwrapper:fortrangetenv");
    exit(1);
  }
  *lenstr = strlen(envname);
  strncpy(envnm, envname, *lenstr);
  *status = 0;
}


x86 assembly (clang, -O0):
<fortrangetenv_>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
lea    0xeb2(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1060 <appendtostring@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11bb <fortrangetenv_+0x5b>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e8 <fortrangetenv_+0x88>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fortrangetenv_>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %r8d,%esi
call   1060 <appendtostring@plt>
test   %rax,%rax
je     11ba <fortrangetenv_+0x5a>
mov    %rax,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11c3 <fortrangetenv_+0x63>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,(%r12)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
movl   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe3f(%rip),%rdi
jmp    11ca <fortrangetenv_+0x6a>
lea    0xe76(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"getenv\00"
@.str.1 = [64 x i8] c"Failed : appendtostring (varname) in posixwrapper:fortrangetenv\00"
@.str.2 = [46 x i8] c"Failed : getenv in posixwrapper:fortrangetenv\00"
define void @fortrangetenv_(i8* %0, i8* %1, i32* %2, i32* %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i32 %4, i32* %11
store i32 %5, i32* %12
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8** %13
%15 = load i8*, i8** %7
%16 = load i32, i32* %11
%17 = call i8* @appendtostring(i8* %15, i32 %16)
store i8* %17, i8** %13
%18 = icmp ne i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %6
%20 = call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %6
%23 = load i8*, i8** %13
%24 = call i8* @getenv(i8* %23)
store i8* %24, i8** %14
%25 = icmp ne i8* %24, null
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = load i8*, i8** %14
%31 = call i32 @strlen(i8* %30)
%32 = load i32*, i32** %9
store i32 %31, i32* %32
%33 = load i8*, i8** %8
%34 = load i8*, i8** %14
%35 = load i32*, i32** %9
%36 = load i32, i32* %35
%37 = call i32 @strncpy(i8* %33, i8* %34, i32 %36)
%38 = load i32*, i32** %10
store i32 0, i32* %38
ret void
}

LLVM IR (-O2):
@.str.1 = [64 x i8] c"Failed : appendtostring (varname) in posixwrapper:fortrangetenv\00"
@.str.2 = [46 x i8] c"Failed : getenv in posixwrapper:fortrangetenv\00"
define void @fortrangetenv_(i8* %0, i8* %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3, i32 %4, i32 %5) {
%7 = tail call i8* @appendtostring(i8* %0, i32 %4)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6
%13 = tail call i8* @getenv(i8* %7)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = tail call i32 @strlen(i8* %13)
store i32 %19, i32* %2
%20 = tail call i32 @strncpy(i8* %1, i8* %13, i32 %19)
store i32 0, i32* %3
ret void
}