#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*/ LOG_NOTICE;
char *getenv(char *);
int /*<<< orphan*/ sstrncpy(char *, char *, size_t);
char *strstr(char *, char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
void cgi_get_cookie(const char *name, char *cookie, size_t cookie_len) {
  char *p;
  char *c;
  if ((c = getenv("HTTP_COOKIE")) != NULL) {
    if ((p = strstr(c, name))) {
      p = strstr(p, "=");
      p++;
      if (p) {
        sstrncpy(cookie, p, cookie_len);
        p = strstr(cookie, ";");
        if (p)
          *p = 0;
      }
    }
  } else {
    syslog(LOG_NOTICE, "HTTP_COOKIE not found\n");
  }
}


x86 assembly (clang, -O0):
<cgi_get_cookie>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xea5(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11fa <cgi_get_cookie+0xba>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f5 <cgi_get_cookie+0xb5>
mov    -0x20(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f0 <cgi_get_cookie+0xb0>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <sstrncpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11eb <cgi_get_cookie+0xab>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    11f0 <cgi_get_cookie+0xb0>
jmp    11f5 <cgi_get_cookie+0xb5>
jmp    120f <cgi_get_cookie+0xcf>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1050 <syslog@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgi_get_cookie>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe9b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b6 <cgi_get_cookie+0x66>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11b0 <cgi_get_cookie+0x60>
mov    %rax,%rdi
mov    $0x3d,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1050 <sstrncpy@plt>
mov    %r15,%rdi
mov    $0x3b,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11b0 <cgi_get_cookie+0x60>
movb   $0x0,(%rax)
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <syslog@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"HTTP_COOKIE\00"
@.str.1 = [2 x i8] c"=\00"
@.str.2 = [2 x i8] c";\00"
@LOG_NOTICE = global i32 0
@.str.3 = [23 x i8] c"HTTP_COOKIE not found\0A\00"
define void @cgi_get_cookie(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %8
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %37
11: ; preds = %3
%12 = load i8*, i8** %8
%13 = load i8*, i8** %4
%14 = call i8* @strstr(i8* %12, i8* %13)
store i8* %14, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %36
16: ; preds = %11
%17 = load i8*, i8** %7
%18 = call i8* @strstr(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %35
23: ; preds = %16
%24 = load i8*, i8** %5
%25 = load i8*, i8** %7
%26 = load i64, i64* %6
%27 = call i32 @sstrncpy(i8* %24, i8* %25, i64 %26)
%28 = load i8*, i8** %5
%29 = call i8* @strstr(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = load i8*, i8** %7
store i8 0, i8* %33
br label %34
34: ; preds = %32, %23
br label %35
35: ; preds = %34, %16
br label %36
36: ; preds = %35, %11
br label %40
37: ; preds = %3
%38 = load i32, i32* @LOG_NOTICE
%39 = call i32 @syslog(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %37, %36
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"HTTP_COOKIE\00"
@LOG_NOTICE = global i32 0
@.str.3 = [23 x i8] c"HTTP_COOKIE not found\0A\00"
define void @cgi_get_cookie(i8* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %16, label %6
6: ; preds = %3
%7 = tail call i8* @strstr(i8* %4, i8* dereferenceable(1) %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %19, label %9
9: ; preds = %6
%10 = tail call i8* @strchr(i8* %7, i32 61)
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = tail call i32 @sstrncpy(i8* %1, i8* %11, i64 %2)
%13 = tail call i8* @strchr(i8* dereferenceable(1) %1, i32 59)
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %9
store i8 0, i8* %13
br label %19
16: ; preds = %3
%17 = load i32, i32* @LOG_NOTICE
%18 = tail call i32 @syslog(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %6, %9, %15, %16
ret void
}