#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 *__convert_string(char *);
char *__get_option_string(char *);
scalar_t__ calloc(int, int);
char *fgets(char *, int, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ memcpy(char **, char **, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcasecmp(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
char **map_environment(int *argc, char **argv) {
  char *query_string, *request_method, *token;
  char **new_argv;
  request_method = getenv("REQUEST_METHOD");
  if (!request_method)
    return argv;
  if (!strcasecmp(request_method, "GET"))
    query_string = getenv("QUERY_STRING");
  else {
    query_string = (char *)calloc(512, sizeof(char));
    query_string = fgets(query_string, 512 - 2, stdin);
    if (query_string)
      query_string[strlen(query_string) - 2] = '\0';
  }
  if (!query_string)
    return argv;
  query_string = __convert_string(query_string);
  new_argv = (char **)calloc(128, sizeof argv);
  memcpy(new_argv, argv, *argc * sizeof argv);
  token = strtok(query_string, "&");
  do {
    new_argv[(*argc)++] = __get_option_string(token);
  } while ((token = strtok(NULL, "&")));
  return new_argv;
}


x86 assembly (clang, -O0):
<map_environment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe59(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c8 <map_environment+0x38>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1305 <map_environment+0x175>
mov    -0x28(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1040 <strcasecmp@plt>
cmp    $0x0,%eax
jne    11f6 <map_environment+0x66>
lea    0xe2b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
jmp    1255 <map_environment+0xc5>
mov    $0x200,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    $0x1fe,%esi
call   1070 <fgets@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1250 <map_environment+0xc0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1255 <map_environment+0xc5>
cmpq   $0x0,-0x20(%rbp)
jne    126d <map_environment+0xdd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1305 <map_environment+0x175>
mov    -0x20(%rbp),%rdi
call   1050 <__convert_string@plt>
mov    %rax,-0x20(%rbp)
mov    $0x80,%edi
mov    $0x8,%esi
call   1080 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
shl    $0x3,%rax
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xd6e(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   10b0 <__get_option_string@plt>
mov    %rax,%rdx
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd36(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    12bb <map_environment+0x12b>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_environment>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe5b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1287 <map_environment+0xf7>
lea    0xe55(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcasecmp@plt>
test   %eax,%eax
je     1208 <map_environment+0x78>
mov    $0x200,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%edx
mov    %rax,%rdi
mov    $0x1fe,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     1287 <map_environment+0xf7>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x2(%rax,%rbx,1)
jmp    121c <map_environment+0x8c>
lea    0xe04(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1287 <map_environment+0xf7>
mov    %rbx,%rdi
call   1050 <__convert_string@plt>
mov    %rax,%r12
mov    $0x80,%edi
mov    $0x8,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    (%r15),%edx
shl    $0x3,%edx
mov    %rax,%rdi
mov    %r14,%rsi
call   1090 <memcpy@plt>
lea    0xdcf(%rip),%r14
mov    %r12,%rdi
mov    %r14,%rsi
call   10a0 <strtok@plt>
nopl   0x0(%rax)
mov    %rax,%rdi
call   10b0 <__get_option_string@plt>
movslq (%r15),%rcx
lea    0x1(%rcx),%edx
mov    %edx,(%r15)
mov    %rax,(%rbx,%rcx,8)
xor    %edi,%edi
mov    %r14,%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
jne    1260 <map_environment+0xd0>
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"REQUEST_METHOD\00"
@.str.1 = [4 x i8] c"GET\00"
@.str.2 = [13 x i8] c"QUERY_STRING\00"
@stdin = global i32 0
@.str.3 = [2 x i8] c"&\00"
define i8** @map_environment(i32* nocapture %0, i8** %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %40, label %5
5: ; preds = %2
%6 = tail call i32 @strcasecmp(i8* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = tail call i64 @calloc(i32 512, i32 1)
%10 = inttoptr i64 %9 to i8*
%11 = load i32, i32* @stdin
%12 = tail call i8* @fgets(i8* %10, i32 510, i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %40, label %14
14: ; preds = %8
%15 = tail call i32 @strlen(i8* %12)
%16 = add nsw i32 %15, -2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %12, i64 %17
store i8 0, i8* %18
br label %22
19: ; preds = %5
%20 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i8* %20, null
br i1 %21, label %40, label %22
22: ; preds = %14, %19
%23 = phi i8* [ %12, %14 ], [ %20, %19 ]
%24 = tail call i8* @__convert_string(i8* %23)
%25 = tail call i64 @calloc(i32 128, i32 8)
%26 = inttoptr i64 %25 to i8**
%27 = load i32, i32* %0
%28 = shl i32 %27, 3
%29 = tail call i32 @memcpy(i8** %26, i8** %1, i32 %28)
%30 = tail call i8* @strtok(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %31, %22
%32 = phi i8* [ %30, %22 ], [ %38, %31 ]
%33 = tail call i8* @__get_option_string(i8* %32)
%34 = load i32, i32* %0
%35 = add nsw i32 %34, 1
store i32 %35, i32* %0
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8*, i8** %26, i64 %36
store i8* %33, i8** %37
%38 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %31
40: ; preds = %31, %8, %19, %2
%41 = phi i8** [ %1, %2 ], [ %1, %19 ], [ %1, %8 ], [ %26, %31 ]
ret i8** %41
}