#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *strchr(char const *, char);
int strlen(char const *);
char *get_env_val(const char *env) {
  char *p_equal = strchr(env, '=');
  if (!p_equal) {
    return NULL;
  }
  int val_len = strlen(env) - (p_equal - env) - 1;
  char *val = (char *)malloc(val_len + 1);
  memcpy(val, p_equal + 1, val_len);
  val[val_len] = '\0';
  return val;
}


x86 assembly (clang, -O0):
<get_env_val>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3d,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1176 <get_env_val+0x36>
movq   $0x0,-0x8(%rbp)
jmp    11cd <get_env_val+0x8d>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cltq
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_env_val>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    $0x3d,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     118f <get_env_val+0x4f>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
sub    %r14d,%ebx
lea    (%rax,%rbx,1),%edi
lea    (%rax,%rbx,1),%ebp
add    $0xffffffff,%ebp
call   1060 <malloc@plt>
mov    %rax,%rbx
add    $0x1,%r14
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    1191 <get_env_val+0x51>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @get_env_val(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strchr(i8* %7, i8 signext 61)
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %38
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
%15 = sext i32 %14 to i64
%16 = load i8*, i8** %4
%17 = load i8*, i8** %3
%18 = ptrtoint i8* %16 to i64
%19 = ptrtoint i8* %17 to i64
%20 = sub i64 %18, %19
%21 = sub nsw i64 %15, %20
%22 = sub nsw i64 %21, 1
%23 = trunc i64 %22 to i32
store i32 %23, i32* %5
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
%26 = call i64 @malloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i8*, i8** %4
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = load i32, i32* %5
%32 = call i32 @memcpy(i8* %28, i8* %30, i32 %31)
%33 = load i8*, i8** %6
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
%37 = load i8*, i8** %6
store i8* %37, i8** %2
br label %38
38: ; preds = %12, %11
%39 = load i8*, i8** %2
ret i8* %39
}

LLVM IR (-O2):
define i8* @get_env_val(i8* %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 61)
%3 = icmp eq i8* %2, null
br i1 %3, label %18, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* %0)
%6 = ptrtoint i8* %2 to i64
%7 = ptrtoint i8* %0 to i64
%8 = sub i64 %7, %6
%9 = trunc i64 %8 to i32
%10 = add i32 %5, %9
%11 = add i32 %10, -1
%12 = tail call i64 @malloc(i32 %10)
%13 = inttoptr i64 %12 to i8*
%14 = getelementptr inbounds i8, i8* %2, i64 1
%15 = tail call i32 @memcpy(i8* %13, i8* %14, i32 %11)
%16 = sext i32 %11 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %1, %4
%19 = phi i8* [ %13, %4 ], [ null, %1 ]
ret i8* %19
}