#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*/ free(void *);
char *getenv(char *);
scalar_t__ malloc(size_t);
size_t strcspn(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int igetenv(char *name, char *value, size_t nam_len, size_t val_len) {
  int i, env_len;
  char *cname, *ep;
  for (i = 0; i < val_len; value[i++] = ' ')
    ;
  if ((cname = (char *)malloc(nam_len + 1)) == NULL)
    return 0;
  strncpy(cname, name, nam_len);
  cname[nam_len] = '\0';
  cname[strcspn(cname, " ")] = '\0';
  ep = getenv(cname);
  free((void *)cname);
  if (ep == NULL)
    return 0;
  if ((env_len = strlen(ep)) > val_len)
    return (-env_len);
  else {
    strncpy(value, ep, env_len);
    return env_len;
  }
}


x86 assembly (clang, -O0):
<igetenv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11ad <igetenv+0x4d>
jmp    1192 <igetenv+0x32>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0x20,(%rax,%rcx,1)
jmp    117f <igetenv+0x1f>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11d4 <igetenv+0x74>
movl   $0x0,-0x4(%rbp)
jmp    127e <igetenv+0x11e>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1050 <strncpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1070 <strcspn@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
jne    1243 <igetenv+0xe3>
movl   $0x0,-0x4(%rbp)
jmp    127e <igetenv+0x11e>
mov    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x30(%rbp)
cltq
cmp    -0x28(%rbp),%rax
jbe    1268 <igetenv+0x108>
xor    %eax,%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127e <igetenv+0x11e>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<igetenv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r13
test   %rcx,%rcx
je     119c <igetenv+0x2c>
mov    %r14,%rdi
mov    $0x20,%esi
mov    %r15,%rdx
call   1070 <memset@plt>
lea    0x1(%rbx),%rdi
call   1090 <malloc@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     1213 <igetenv+0xa3>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1050 <strncpy@plt>
movb   $0x0,0x0(%rbp,%rbx,1)
lea    0xe37(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcspn@plt>
movb   $0x0,0x0(%rbp,%rax,1)
mov    %rbp,%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <free@plt>
test   %rbx,%rbx
je     1213 <igetenv+0xa3>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
cltq
cmp    %r15,%rax
jbe    1205 <igetenv+0x95>
neg    %r12d
jmp    1213 <igetenv+0xa3>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <strncpy@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
define i32 @igetenv(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i32 0, i32* %10
br label %14
14: ; preds = %20, %4
%15 = load i32, i32* %10
%16 = sext i32 %15 to i64
%17 = load i64, i64* %9
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %26
19: ; preds = %14
br label %20
20: ; preds = %19
%21 = load i8*, i8** %7
%22 = load i32, i32* %10
%23 = add nsw i32 %22, 1
store i32 %23, i32* %10
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 32, i8* %25
br label %14
26: ; preds = %14
%27 = load i64, i64* %8
%28 = add i64 %27, 1
%29 = call i64 @malloc(i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %12
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 0, i32* %5
br label %68
33: ; preds = %26
%34 = load i8*, i8** %12
%35 = load i8*, i8** %6
%36 = load i64, i64* %8
%37 = trunc i64 %36 to i32
%38 = call i32 @strncpy(i8* %34, i8* %35, i32 %37)
%39 = load i8*, i8** %12
%40 = load i64, i64* %8
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %12
%43 = load i8*, i8** %12
%44 = call i64 @strcspn(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %12
%47 = call i8* @getenv(i8* %46)
store i8* %47, i8** %13
%48 = load i8*, i8** %12
%49 = call i32 @free(i8* %48)
%50 = load i8*, i8** %13
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %53
52: ; preds = %33
store i32 0, i32* %5
br label %68
53: ; preds = %33
%54 = load i8*, i8** %13
%55 = call i32 @strlen(i8* %54)
store i32 %55, i32* %11
%56 = sext i32 %55 to i64
%57 = load i64, i64* %9
%58 = icmp ugt i64 %56, %57
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = load i32, i32* %11
%61 = sub nsw i32 0, %60
store i32 %61, i32* %5
br label %68
62: ; preds = %53
%63 = load i8*, i8** %7
%64 = load i8*, i8** %13
%65 = load i32, i32* %11
%66 = call i32 @strncpy(i8* %63, i8* %64, i32 %65)
%67 = load i32, i32* %11
store i32 %67, i32* %5
br label %68
68: ; preds = %62, %59, %52, %32
%69 = load i32, i32* %5
ret i32 %69
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define i32 @igetenv(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = icmp eq i64 %3, 0
br i1 %5, label %7, label %6
6: ; preds = %4
br label %7
7: ; preds = %6, %4
%8 = add i64 %2, 1
%9 = tail call i64 @malloc(i64 %8)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %29, label %12
12: ; preds = %7
%13 = trunc i64 %2 to i32
%14 = tail call i32 @strncpy(i8* %10, i8* %0, i32 %13)
%15 = getelementptr inbounds i8, i8* %10, i64 %2
store i8 0, i8* %15
%16 = tail call i64 @strcspn(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds i8, i8* %10, i64 %16
store i8 0, i8* %17
%18 = tail call i8* @getenv(i8* %10)
%19 = tail call i32 @free(i8* %10)
%20 = icmp eq i8* %18, null
br i1 %20, label %29, label %21
21: ; preds = %12
%22 = tail call i32 @strlen(i8* %18)
%23 = sext i32 %22 to i64
%24 = icmp ugt i64 %23, %3
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = sub nsw i32 0, %22
br label %29
27: ; preds = %21
%28 = tail call i32 @strncpy(i8* %1, i8* %18, i32 %22)
br label %29
29: ; preds = %12, %7, %27, %25
%30 = phi i32 [ %26, %25 ], [ %22, %27 ], [ 0, %7 ], [ 0, %12 ]
ret i32 %30
}