#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 *getenv(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int strlen(char *);
char *strstr(char *, char *);
int getCookie(char *cookie) {
  char *cookie_env = NULL, *cookie_ptr = NULL;
  cookie_env = getenv("HTTP_COOKIE");
  if (!cookie_env)
    return -1;
  cookie_ptr = strstr(cookie_env, "MWS=");
  if (!cookie_ptr)
    return -2;
  if (strlen(cookie_ptr) < 67)
    return -3;
  memcpy(cookie, cookie_ptr + 4, 64);
  return 0;
}


x86 assembly (clang, -O0):
<getCookie>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe9d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1183 <getCookie+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <getCookie+0xa9>
mov    -0x18(%rbp),%rdi
lea    0xe7e(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ae <getCookie+0x6e>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e9 <getCookie+0xa9>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x43,%eax
jge    11cc <getCookie+0x8c>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11e9 <getCookie+0xa9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getCookie>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xeb2(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     119b <getCookie+0x5b>
lea    0xead(%rip),%rsi
mov    %rax,%rdi
call   1060 <strstr@plt>
test   %rax,%rax
je     11a2 <getCookie+0x62>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    $0xfffffffd,%eax
cmp    $0x43,%ecx
jl     11a7 <getCookie+0x67>
add    $0x4,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    11a7 <getCookie+0x67>
mov    $0xffffffff,%eax
jmp    11a7 <getCookie+0x67>
mov    $0xfffffffe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [12 x i8] c"HTTP_COOKIE\00"
@.str.1 = [5 x i8] c"MWS=\00"
define i32 @getCookie(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
store i8* null, i8** %5
%6 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %1
store i32 -1, i32* %2
br label %26
10: ; preds = %1
%11 = load i8*, i8** %4
%12 = call i8* @strstr(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %10
store i32 -2, i32* %2
br label %26
16: ; preds = %10
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = icmp slt i32 %18, 67
br i1 %19, label %20, label %21
20: ; preds = %16
store i32 -3, i32* %2
br label %26
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i64 4
%25 = call i32 @memcpy(i8* %22, i8* %24, i32 64)
store i32 0, i32* %2
br label %26
26: ; preds = %21, %20, %15, %9
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [12 x i8] c"HTTP_COOKIE\00"
@.str.1 = [5 x i8] c"MWS=\00"
define i32 @getCookie(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = tail call i8* @strstr(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %5)
%9 = icmp slt i32 %8, 67
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = getelementptr inbounds i8, i8* %5, i64 4
%12 = tail call i32 @memcpy(i8* %0, i8* %11, i32 64)
br label %13
13: ; preds = %7, %4, %1, %10
%14 = phi i32 [ 0, %10 ], [ -1, %1 ], [ -2, %4 ], [ -3, %7 ]
ret i32 %14
}