#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 *strchr(char *, char);
char *strdup(char const *);
int strlen(char const *);
char *getcookie(const char *rawcookie) {
  char *cookie = NULL;
  char *tmpcookie;
  char *cookieend;
  if (strlen(rawcookie) < 2)
    return NULL;
  tmpcookie = strdup(rawcookie + 2);
  cookieend = strchr(tmpcookie, ';');
  if (cookieend)
    *cookieend = '\0';
  cookie = strdup(tmpcookie);
  if (tmpcookie) {
    free(tmpcookie);
    tmpcookie = NULL;
  };
  return cookie;
}


x86 assembly (clang, -O0):
<getcookie>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x2,%eax
jge    1173 <getcookie+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11d9 <getcookie+0x99>
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
call   1060 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x3b,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a8 <getcookie+0x68>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d1 <getcookie+0x91>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcookie>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x2,%eax
jge    1155 <getcookie+0x15>
xor    %ebx,%ebx
jmp    1191 <getcookie+0x51>
add    $0x2,%rbx
mov    %rbx,%rdi
call   1060 <strdup@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x3b,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1179 <getcookie+0x39>
movb   $0x0,(%rax)
mov    %r14,%rdi
call   1060 <strdup@plt>
mov    %rax,%rbx
test   %r14,%r14
je     1191 <getcookie+0x51>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i8* @getcookie(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* null, i8** %2
br label %31
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = getelementptr inbounds i8, i8* %12, i64 2
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = call i8* @strchr(i8* %15, i8 signext 59)
store i8* %16, i8** %6
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = load i8*, i8** %6
store i8 0, i8* %20
br label %21
21: ; preds = %19, %11
%22 = load i8*, i8** %5
%23 = call i8* @strdup(i8* %22)
store i8* %23, i8** %4
%24 = load i8*, i8** %5
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i8*, i8** %5
%28 = call i32 @free(i8* %27)
store i8* null, i8** %5
br label %29
29: ; preds = %26, %21
%30 = load i8*, i8** %4
store i8* %30, i8** %2
br label %31
31: ; preds = %29, %10
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
define noalias i8* @getcookie(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 2
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %0, i64 2
%6 = tail call i8* @strdup(i8* %5)
%7 = tail call i8* @strchr(i8* dereferenceable(1) %6, i8 signext 59)
%8 = icmp eq i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %4
store i8 0, i8* %7
br label %10
10: ; preds = %9, %4
%11 = tail call i8* @strdup(i8* %6)
%12 = icmp eq i8* %6, null
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 @free(i8* %6)
br label %15
15: ; preds = %10, %13, %1
%16 = phi i8* [ null, %1 ], [ %11, %13 ], [ %11, %10 ]
ret i8* %16
}