#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(char *);
char *getenv(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
char *strchr(char *, char);
char *strdup(char *);
scalar_t__ strlen(char const *);
char *strndup(char *, int);
char *strstr(char *, char *);
char *extract_val(const char *name) {
  char *start;
  char *end;
  char *search;
  start = getenv("QUERY_STRING");
  if (start == NULL)
    return NULL;
  search = malloc(strlen(name) + 1);
  sprintf(search, "%s=", name);
  start = strstr(start, search);
  free(search);
  if (start == NULL)
    return NULL;
  start += strlen(name) + 1;
  end = strchr(start, '&');
  if (end == NULL) {
    return strdup(start);
  } else {
    return strndup(start, end - start);
  }
}


x86 assembly (clang, -O0):
<extract_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe5d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c4 <extract_val+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1281 <extract_val+0xf1>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10b0 <strstr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1223 <extract_val+0x93>
movq   $0x0,-0x8(%rbp)
jmp    1281 <extract_val+0xf1>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x26,%esi
call   1060 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1267 <extract_val+0xd7>
mov    -0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1281 <extract_val+0xf1>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1070 <strndup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_val>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe61(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1220 <extract_val+0x90>
mov    %rax,%r15
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xe46(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <strstr@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <free@plt>
test   %r15,%r15
je     1220 <extract_val+0x90>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r15,1),%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x26,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1228 <extract_val+0x98>
sub    %ebx,%eax
mov    %rbx,%rdi
mov    %eax,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <strndup@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    10a0 <strdup@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"QUERY_STRING\00"
@.str.1 = [4 x i8] c"%s=\00"
define i8* @extract_val(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %49
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %13, 1
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %6
%16 = load i8*, i8** %6
%17 = load i8*, i8** %3
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** %4
%20 = load i8*, i8** %6
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
%24 = load i8*, i8** %4
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %11
store i8* null, i8** %2
br label %49
27: ; preds = %11
%28 = load i8*, i8** %3
%29 = call i64 @strlen(i8* %28)
%30 = add nsw i64 %29, 1
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 %30
store i8* %32, i8** %4
%33 = load i8*, i8** %4
%34 = call i8* @strchr(i8* %33, i8 signext 38)
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %27
%38 = load i8*, i8** %4
%39 = call i8* @strdup(i8* %38)
store i8* %39, i8** %2
br label %49
40: ; preds = %27
%41 = load i8*, i8** %4
%42 = load i8*, i8** %5
%43 = load i8*, i8** %4
%44 = ptrtoint i8* %42 to i64
%45 = ptrtoint i8* %43 to i64
%46 = sub i64 %44, %45
%47 = trunc i64 %46 to i32
%48 = call i8* @strndup(i8* %41, i32 %47)
store i8* %48, i8** %2
br label %49
49: ; preds = %40, %37, %26, %10
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [13 x i8] c"QUERY_STRING\00"
@.str.1 = [4 x i8] c"%s=\00"
define noalias i8* @extract_val(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 1
%7 = tail call i8* @malloc(i64 %6)
%8 = tail call i32 @sprintf(i8* dereferenceable(1) %7, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%9 = tail call i8* @strstr(i8* %2, i8* dereferenceable(1) %7)
%10 = tail call i32 @free(i8* %7)
%11 = icmp eq i8* %9, null
br i1 %11, label %26, label %12
12: ; preds = %4
%13 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%14 = add nsw i64 %13, 1
%15 = getelementptr inbounds i8, i8* %9, i64 %14
%16 = tail call i8* @strchr(i8* %15, i8 signext 38)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i8* @strdup(i8* %15)
br label %26
20: ; preds = %12
%21 = ptrtoint i8* %16 to i64
%22 = ptrtoint i8* %15 to i64
%23 = sub i64 %21, %22
%24 = trunc i64 %23 to i32
%25 = tail call i8* @strndup(i8* %15, i32 %24)
br label %26
26: ; preds = %4, %1, %20, %18
%27 = phi i8* [ %19, %18 ], [ %25, %20 ], [ null, %1 ], [ null, %4 ]
ret i8* %27
}