#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
char *badds(char *, char *);
char *btxid(char *);
scalar_t__ checkTimeout(int, int);
int /*<<< orphan*/ errno;
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ puterr(char *, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int reqCookie(int fd, char *out) {
  char buf[1024], *p;
  int len = -1, i, n;
  p = buf;
  *p++ = 'd';
  *p++ = '1';
  *p++ = ':';
  *p++ = 'q';
  p = badds(p, "cookie");
  p = btxid(p);
  *p++ = 'e';
  if ((n = write(fd, buf, p - buf)) < 0) {
    puterr("socket write", errno);
    goto out;
  }
  if (checkTimeout(fd, 2) < 0)
    goto out;
  if ((n = read(fd, buf, sizeof(buf))) < 0) {
    puterr("socket read", errno);
    goto out;
  }
  for (i = 0, p = buf; i < n; ++i, ++p) {
    if (*p == ':') {
      if (memcmp(++p, "cookie10", 8) == 0) {
        len = 10;
        memcpy(out, p + 9, len);
        break;
      }
    }
  }
out:
  return len;
}


x86 assembly (clang, -O0):
<reqCookie>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x41c(%rbp)
ff ff ff
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x418(%rbp)
movb   $0x64,(%rax)
mov    -0x418(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x418(%rbp)
movb   $0x31,(%rax)
mov    -0x418(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x418(%rbp)
movb   $0x3a,(%rax)
mov    -0x418(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x418(%rbp)
movb   $0x71,(%rax)
mov    -0x418(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1040 <badds@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
call   1090 <btxid@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x418(%rbp)
movb   $0x65,(%rax)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jge    129a <reqCookie+0x11a>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    0xd77(%rip),%rdi
call   1050 <puterr@plt>
jmp    13bb <reqCookie+0x23b>
mov    -0x4(%rbp),%edi
mov    $0x2,%esi
call   10a0 <checkTimeout@plt>
cmp    $0x0,%rax
jge    12b6 <reqCookie+0x136>
jmp    13bb <reqCookie+0x23b>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x424(%rbp)
cmp    $0x0,%eax
jge    12f3 <reqCookie+0x173>
mov    0x2d00(%rip),%rax
mov    (%rax),%esi
lea    0xd2b(%rip),%rdi
call   1050 <puterr@plt>
jmp    13bb <reqCookie+0x23b>
movl   $0x0,-0x420(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x420(%rbp),%eax
cmp    -0x424(%rbp),%eax
jge    13b6 <reqCookie+0x236>
mov    -0x418(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    138b <reqCookie+0x20b>
mov    -0x418(%rbp),%rdi
add    $0x1,%rdi
mov    %rdi,-0x418(%rbp)
lea    0xcd7(%rip),%rsi
mov    $0x8,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
jne    1386 <reqCookie+0x206>
movl   $0xa,-0x41c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x418(%rbp),%rsi
add    $0x9,%rsi
mov    -0x41c(%rbp),%edx
call   1080 <memcpy@plt>
jmp    13b6 <reqCookie+0x236>
jmp    138b <reqCookie+0x20b>
jmp    1390 <reqCookie+0x210>
mov    -0x420(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x420(%rbp)
mov    -0x418(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x418(%rbp)
jmp    130b <reqCookie+0x18b>
jmp    13bb <reqCookie+0x23b>
mov    -0x41c(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reqCookie>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
movl   $0x713a3164,(%rsp)
lea    0x4(%rsp),%rdi
lea    0xe57(%rip),%rsi
call   1040 <badds@plt>
mov    %rax,%rdi
call   1090 <btxid@plt>
movb   $0x65,(%rax)
mov    %rsp,%rsi
sub    %esi,%eax
add    $0x1,%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     126b <reqCookie+0xeb>
mov    %ebx,%edi
mov    $0x2,%esi
call   10a0 <checkTimeout@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
js     129d <reqCookie+0x11d>
mov    %rsp,%r13
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1288 <reqCookie+0x108>
je     129d <reqCookie+0x11d>
mov    %eax,%ebx
lea    0xe0a(%rip),%r12
jmp    122f <reqCookie+0xaf>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rbp
add    $0x1,%rbp
mov    %rbp,%r13
add    $0xffffffff,%ebx
je     129d <reqCookie+0x11d>
cmpb   $0x3a,0x0(%r13)
jne    1220 <reqCookie+0xa0>
lea    0x1(%r13),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x8,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
jne    1223 <reqCookie+0xa3>
add    $0xa,%r13
mov    $0xa,%r15d
mov    %r14,%rdi
mov    %r13,%rsi
mov    $0xa,%edx
call   1080 <memcpy@plt>
jmp    129d <reqCookie+0x11d>
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
lea    0xd8c(%rip),%rdi
call   1050 <puterr@plt>
mov    $0xffffffff,%r15d
jmp    129d <reqCookie+0x11d>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xd7c(%rip),%rdi
call   1050 <puterr@plt>
mov    %r15d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"cookie\00"
@.str.1 = [13 x i8] c"socket write\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"socket read\00"
@.str.3 = [9 x i8] c"cookie10\00"
define i32 @reqCookie(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 -1, i32* %7
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = getelementptr inbounds i8, i8* %11, i32 1
store i8* %12, i8** %6
store i8 100, i8* %11
%13 = load i8*, i8** %6
%14 = getelementptr inbounds i8, i8* %13, i32 1
store i8* %14, i8** %6
store i8 49, i8* %13
%15 = load i8*, i8** %6
%16 = getelementptr inbounds i8, i8* %15, i32 1
store i8* %16, i8** %6
store i8 58, i8* %15
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %6
store i8 113, i8* %17
%19 = load i8*, i8** %6
%20 = call i8* @badds(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = call i8* @btxid(i8* %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %6
store i8 101, i8* %23
%25 = load i32, i32* %3
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = load i8*, i8** %6
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = trunc i64 %31 to i32
%33 = call i32 @write(i32 %25, i8* %26, i32 %32)
store i32 %33, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %2
%36 = load i32, i32* @errno
%37 = call i32 @puterr(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %81
38: ; preds = %2
%39 = load i32, i32* %3
%40 = call i64 @checkTimeout(i32 %39, i32 2)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
br label %81
43: ; preds = %38
%44 = load i32, i32* %3
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 1024)
store i32 %46, i32* %9
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* @errno
%50 = call i32 @puterr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %81
51: ; preds = %43
store i32 0, i32* %8
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
store i8* %52, i8** %6
br label %53
53: ; preds = %75, %51
%54 = load i32, i32* %8
%55 = load i32, i32* %9
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %80
57: ; preds = %53
%58 = load i8*, i8** %6
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 58
br i1 %61, label %62, label %74
62: ; preds = %57
%63 = load i8*, i8** %6
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %6
%65 = call i64 @memcmp(i8* %64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%66 = icmp eq i64 %65, 0
br i1 %66, label %67, label %73
67: ; preds = %62
store i32 10, i32* %7
%68 = load i8*, i8** %4
%69 = load i8*, i8** %6
%70 = getelementptr inbounds i8, i8* %69, i64 9
%71 = load i32, i32* %7
%72 = call i32 @memcpy(i8* %68, i8* %70, i32 %71)
br label %80
73: ; preds = %62
br label %74
74: ; preds = %73, %57
br label %75
75: ; preds = %74
%76 = load i32, i32* %8
%77 = add nsw i32 %76, 1
store i32 %77, i32* %8
%78 = load i8*, i8** %6
%79 = getelementptr inbounds i8, i8* %78, i32 1
store i8* %79, i8** %6
br label %53
80: ; preds = %67, %53
br label %81
81: ; preds = %80, %48, %42, %35
%82 = load i32, i32* %7
ret i32 %82
}

LLVM IR (-O2):
@.str = [7 x i8] c"cookie\00"
@.str.1 = [13 x i8] c"socket write\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"socket read\00"
@.str.3 = [9 x i8] c"cookie10\00"
define i32 @reqCookie(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1
store i8 100, i8* %4
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 2
store i8 49, i8* %5
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 3
store i8 58, i8* %6
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 4
store i8 113, i8* %7
%9 = call i8* @badds(i8* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = call i8* @btxid(i8* %9)
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8 101, i8* %10
%12 = ptrtoint i8* %11 to i64
%13 = ptrtoint [1024 x i8]* %3 to i64
%14 = sub i64 %12, %13
%15 = trunc i64 %14 to i32
%16 = call i32 @write(i32 %0, i8* %4, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i32 @puterr(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %49
21: ; preds = %2
%22 = call i64 @checkTimeout(i32 %0, i32 2)
%23 = icmp slt i64 %22, 0
br i1 %23, label %49, label %24
24: ; preds = %21
%25 = call i32 @read(i32 %0, i8* %4, i32 1024)
%26 = icmp slt i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = icmp eq i32 %25, 0
br i1 %28, label %49, label %32
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = call i32 @puterr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %49
32: ; preds = %27, %44
%33 = phi i32 [ %46, %44 ], [ 0, %27 ]
%34 = phi i8* [ %47, %44 ], [ %4, %27 ]
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 58
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = getelementptr inbounds i8, i8* %34, i64 1
%39 = call i64 @memcmp(i8* %38, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = getelementptr inbounds i8, i8* %34, i64 10
%43 = call i32 @memcpy(i8* %1, i8* %42, i32 10)
br label %49
44: ; preds = %32, %37
%45 = phi i8* [ %38, %37 ], [ %34, %32 ]
%46 = add nuw nsw i32 %33, 1
%47 = getelementptr inbounds i8, i8* %45, i64 1
%48 = icmp eq i32 %46, %25
br i1 %48, label %49, label %32
49: ; preds = %44, %27, %41, %21, %29, %18
%50 = phi i32 [ -1, %18 ], [ -1, %21 ], [ -1, %29 ], [ 10, %41 ], [ -1, %27 ], [ -1, %44 ]
ret i32 %50
}