#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 *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
size_t strlen(char const *);
scalar_t__ strncmp(char const *, char *, size_t);
char *strtok(char *, char *);
int get_value_from_response(const char *buf, const char *key, char *retbuf,
                            size_t retbuflen) {
  int ret;
  char *tokens, *kv_pair;
  size_t keylen = strlen(key);
  tokens = strdup(buf);
  if (tokens == NULL)
    return -3;
  ret = -1;
  kv_pair = strtok(tokens, "&,\r\n");
  for (; kv_pair != NULL; kv_pair = strtok(NULL, "&,\r\n")) {
    if (strncmp(key, kv_pair, keylen) == 0) {
      char *val = &kv_pair[keylen];
      if (strlen(val) > retbuflen - 1) {
        ret = -2;
      } else {
        strcpy(retbuf, val);
        ret = 1;
      }
      break;
    }
  }
  free(tokens);
  return ret;
}


x86 assembly (clang, -O0):
<get_value_from_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11a9 <get_value_from_response+0x49>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1262 <get_value_from_response+0x102>
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1253 <get_value_from_response+0xf3>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1235 <get_value_from_response+0xd5>
mov    -0x40(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jbe    121c <get_value_from_response+0xbc>
movl   $0xfffffffe,-0x2c(%rbp)
jmp    1230 <get_value_from_response+0xd0>
mov    -0x20(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1050 <strcpy@plt>
movl   $0x1,-0x2c(%rbp)
jmp    1253 <get_value_from_response+0xf3>
jmp    123a <get_value_from_response+0xda>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdbb(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x40(%rbp)
jmp    11c4 <get_value_from_response+0x64>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_value_from_response>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     11f7 <get_value_from_response+0x97>
mov    %rax,%r13
mov    %r12,0x10(%rsp)
lea    0xe5f(%rip),%rsi
mov    %rax,%rdi
call   1070 <strtok@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     1231 <get_value_from_response+0xd1>
mov    %rax,%r14
mov    %r15,0x8(%rsp)
lea    0xe3d(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11ff <get_value_from_response+0x9f>
xor    %edi,%edi
mov    %r15,%rsi
call   1070 <strtok@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11d0 <get_value_from_response+0x70>
jmp    1231 <get_value_from_response+0xd1>
mov    $0xfffffffd,%r12d
jmp    1239 <get_value_from_response+0xd9>
add    %rbx,%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    0x10(%rsp),%rcx
add    $0xffffffffffffffff,%rcx
mov    $0xfffffffe,%r12d
cmp    %rcx,%rax
ja     1231 <get_value_from_response+0xd1>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1050 <strcpy@plt>
mov    $0x1,%r12d
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"&,\0D\0A\00"
define i32 @get_value_from_response(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%15 = load i8*, i8** %7
%16 = call i64 @strlen(i8* %15)
store i64 %16, i64* %13
%17 = load i8*, i8** %6
%18 = call i8* @strdup(i8* %17)
store i8* %18, i8** %11
%19 = load i8*, i8** %11
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 -3, i32* %5
br label %56
22: ; preds = %4
store i32 -1, i32* %10
%23 = load i8*, i8** %11
%24 = call i8* @strtok(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %24, i8** %12
br label %25
25: ; preds = %50, %22
%26 = load i8*, i8** %12
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %52
28: ; preds = %25
%29 = load i8*, i8** %7
%30 = load i8*, i8** %12
%31 = load i64, i64* %13
%32 = call i64 @strncmp(i8* %29, i8* %30, i64 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %49
34: ; preds = %28
%35 = load i8*, i8** %12
%36 = load i64, i64* %13
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %14
%38 = load i8*, i8** %14
%39 = call i64 @strlen(i8* %38)
%40 = load i64, i64* %9
%41 = sub i64 %40, 1
%42 = icmp ugt i64 %39, %41
br i1 %42, label %43, label %44
43: ; preds = %34
store i32 -2, i32* %10
br label %48
44: ; preds = %34
%45 = load i8*, i8** %8
%46 = load i8*, i8** %14
%47 = call i32 @strcpy(i8* %45, i8* %46)
store i32 1, i32* %10
br label %48
48: ; preds = %44, %43
br label %52
49: ; preds = %28
br label %50
50: ; preds = %49
%51 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %51, i8** %12
br label %25
52: ; preds = %48, %25
%53 = load i8*, i8** %11
%54 = call i32 @free(i8* %53)
%55 = load i32, i32* %10
store i32 %55, i32* %5
br label %56
56: ; preds = %52, %21
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
@.str = [5 x i8] c"&,\0D\0A\00"
define i32 @get_value_from_response(i8* nocapture %0, i8* %1, i8* %2, i64 %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i8* @strdup(i8* %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %28, label %8
8: ; preds = %4
%9 = tail call i8* @strtok(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %25, label %11
11: ; preds = %8, %22
%12 = phi i8* [ %23, %22 ], [ %9, %8 ]
%13 = tail call i64 @strncmp(i8* %1, i8* %12, i64 %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = getelementptr inbounds i8, i8* %12, i64 %5
%17 = tail call i64 @strlen(i8* %16)
%18 = add i64 %3, -1
%19 = icmp ugt i64 %17, %18
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = tail call i32 @strcpy(i8* %2, i8* %16)
br label %25
22: ; preds = %11
%23 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %11
25: ; preds = %22, %8, %20, %15
%26 = phi i32 [ 1, %20 ], [ -2, %15 ], [ -1, %8 ], [ -1, %22 ]
%27 = tail call i32 @free(i8* %6)
br label %28
28: ; preds = %4, %25
%29 = phi i32 [ %26, %25 ], [ -3, %4 ]
ret i32 %29
}