#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int main(int argc, char *argv[], char *envp[]) {
  int i = 1, j;
  char *environ = (char *)malloc(sizeof(char) * 512);
  int flag = 0;
  int length = 0;
  char *input_string = (char *)malloc(sizeof(char) * 512);
  int count = argc;
  if (argc == 1) {
    scanf("%s", input_string);
    printf("%s\n", input_string);
  } else {
    while (count > 1) {
      if (argv[i][0] == '$') {
        strcpy(environ, &argv[i][1]);
        flag = 0;
        length = strlen(environ) + 1;
        for (j = 0; envp[j]; j++) {
          if (strncmp(envp[j], environ, strlen(environ)) == 0) {
            printf("%s\n", envp[j] + length);
            flag = 1;
            break;
          }
        }
        if (flag == 0) {
          printf("%s is not a valid environment variable", environ);
          exit(0);
        }
      } else {
        printf("%s\n", argv[i]);
      }
      i++;
      count--;
    }
  }
  free(environ);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1200 <main+0x80>
mov    -0x38(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x38(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1070 <printf@plt>
jmp    1342 <main+0x1c2>
jmp    1205 <main+0x85>
cmpl   $0x1,-0x3c(%rbp)
jle    133d <main+0x1bd>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
jne    130e <main+0x18e>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
add    $0x1,%rsi
call   1050 <strcpy@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     12e8 <main+0x168>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12d5 <main+0x155>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
lea    0xd3f(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x2c(%rbp)
jmp    12e8 <main+0x168>
jmp    12da <main+0x15a>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    125d <main+0xdd>
cmpl   $0x0,-0x2c(%rbp)
jne    1309 <main+0x189>
mov    -0x28(%rbp),%rsi
lea    0xd0a(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    1326 <main+0x1a6>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xce2(%rip),%rdi
call   1070 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x3c(%rbp)
jmp    1205 <main+0x85>
jmp    1342 <main+0x1c2>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r13
mov    %edi,%r15d
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    $0x200,%edi
call   1080 <malloc@plt>
cmp    $0x1,%r15d
jne    11da <main+0x5a>
mov    %rax,%r12
lea    0xe42(%rip),%rdi
mov    %rax,%rsi
call   1090 <scanf@plt>
lea    0xe36(%rip),%rdi
mov    %r12,%rsi
call   1070 <printf@plt>
jmp    12a8 <main+0x128>
jle    12a8 <main+0x128>
mov    %r15d,%ebp
mov    $0x1,%r15d
lea    0xe13(%rip),%r12
mov    %r13,0x10(%rsp)
mov    %rbp,0x8(%rsp)
jmp    1215 <main+0x95>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1070 <printf@plt>
add    $0x1,%r15
cmp    %rbp,%r15
je     12a8 <main+0x128>
mov    0x0(%r13,%r15,8),%rsi
cmpb   $0x24,(%rsi)
jne    1200 <main+0x80>
add    $0x1,%rsi
mov    %rbx,%rdi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    (%r14),%rbp
test   %rbp,%rbp
je     12c1 <main+0x141>
mov    %eax,%r13d
add    $0x1,%r13d
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1280 <main+0x100>
mov    0x8(%r14,%r12,8),%rbp
add    $0x1,%r12
test   %rbp,%rbp
jne    1250 <main+0xd0>
jmp    12c1 <main+0x141>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%eax
movslq %r13d,%rsi
add    (%r14,%rax,8),%rsi
lea    0xd72(%rip),%r12
mov    %r12,%rdi
call   1070 <printf@plt>
mov    0x10(%rsp),%r13
mov    0x8(%rsp),%rbp
jmp    1208 <main+0x88>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3f(%rip),%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [39 x i8] c"%s is not a valid environment variable\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 1, i32* %8
%15 = call i64 @malloc(i32 512)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %10
store i32 0, i32* %11
store i32 0, i32* %12
%17 = call i64 @malloc(i32 512)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %13
%19 = load i32, i32* %5
store i32 %19, i32* %14
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i8*, i8** %13
%24 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %13
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %106
27: ; preds = %3
br label %28
28: ; preds = %100, %27
%29 = load i32, i32* %14
%30 = icmp sgt i32 %29, 1
br i1 %30, label %31, label %105
31: ; preds = %28
%32 = load i8**, i8*** %6
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = getelementptr inbounds i8, i8* %36, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 36
br i1 %40, label %41, label %93
41: ; preds = %31
%42 = load i8*, i8** %10
%43 = load i8**, i8*** %6
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8*, i8** %43, i64 %45
%47 = load i8*, i8** %46
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = call i32 @strcpy(i8* %42, i8* %48)
store i32 0, i32* %11
%50 = load i8*, i8** %10
%51 = call i32 @strlen(i8* %50)
%52 = add nsw i32 %51, 1
store i32 %52, i32* %12
store i32 0, i32* %9
br label %53
53: ; preds = %82, %41
%54 = load i8**, i8*** %7
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8*, i8** %54, i64 %56
%58 = load i8*, i8** %57
%59 = icmp ne i8* %58, null
br i1 %59, label %60, label %85
60: ; preds = %53
%61 = load i8**, i8*** %7
%62 = load i32, i32* %9
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
%65 = load i8*, i8** %64
%66 = load i8*, i8** %10
%67 = load i8*, i8** %10
%68 = call i32 @strlen(i8* %67)
%69 = call i64 @strncmp(i8* %65, i8* %66, i32 %68)
%70 = icmp eq i64 %69, 0
br i1 %70, label %71, label %81
71: ; preds = %60
%72 = load i8**, i8*** %7
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8*, i8** %72, i64 %74
%76 = load i8*, i8** %75
%77 = load i32, i32* %12
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
%80 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %79)
store i32 1, i32* %11
br label %85
81: ; preds = %60
br label %82
82: ; preds = %81
%83 = load i32, i32* %9
%84 = add nsw i32 %83, 1
store i32 %84, i32* %9
br label %53
85: ; preds = %71, %53
%86 = load i32, i32* %11
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %92
88: ; preds = %85
%89 = load i8*, i8** %10
%90 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %89)
%91 = call i32 @exit(i32 0)
unreachable
92: ; preds = %85
br label %100
93: ; preds = %31
%94 = load i8**, i8*** %6
%95 = load i32, i32* %8
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8*, i8** %94, i64 %96
%98 = load i8*, i8** %97
%99 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %98)
br label %100
100: ; preds = %93, %92
%101 = load i32, i32* %8
%102 = add nsw i32 %101, 1
store i32 %102, i32* %8
%103 = load i32, i32* %14
%104 = add nsw i32 %103, -1
store i32 %104, i32* %14
br label %28
105: ; preds = %28
br label %106
106: ; preds = %105, %22
%107 = load i8*, i8** %10
%108 = call i32 @free(i8* %107)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [39 x i8] c"%s is not a valid environment variable\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture %2) {
%4 = tail call i64 @malloc(i32 512)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i64 @malloc(i32 512)
%7 = inttoptr i64 %6 to i8*
%8 = icmp eq i32 %0, 1
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %55
11: ; preds = %9
%12 = zext i32 %0 to i64
br label %16
13: ; preds = %3
%14 = tail call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %7)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %7)
br label %55
16: ; preds = %11, %52
%17 = phi i64 [ 1, %11 ], [ %53, %52 ]
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 36
br i1 %21, label %22, label %50
22: ; preds = %16
%23 = getelementptr inbounds i8, i8* %19, i64 1
%24 = tail call i32 @strcpy(i8* %5, i8* %23)
%25 = tail call i32 @strlen(i8* %5)
%26 = add nsw i32 %25, 1
%27 = load i8*, i8** %2
%28 = icmp eq i8* %27, null
br i1 %28, label %47, label %29
29: ; preds = %22, %42
%30 = phi i64 [ %43, %42 ], [ 0, %22 ]
%31 = phi i8* [ %45, %42 ], [ %27, %22 ]
%32 = tail call i32 @strlen(i8* %5)
%33 = tail call i64 @strncmp(i8* %31, i8* %5, i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %29
%36 = and i64 %30, 4294967295
%37 = getelementptr inbounds i8*, i8** %2, i64 %36
%38 = load i8*, i8** %37
%39 = sext i32 %26 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %52
42: ; preds = %29
%43 = add nuw i64 %30, 1
%44 = getelementptr inbounds i8*, i8** %2, i64 %43
%45 = load i8*, i8** %44
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %29
47: ; preds = %22, %42
%48 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%49 = tail call i32 @exit(i32 0)
unreachable
50: ; preds = %16
%51 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %52
52: ; preds = %35, %50
%53 = add nuw nsw i64 %17, 1
%54 = icmp eq i64 %53, %12
br i1 %54, label %55, label %16
55: ; preds = %52, %9, %13
%56 = tail call i32 @free(i8* %5)
ret i32 0
}