#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 */
scalar_t__ SendBuffer(int, char *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
char *strstr(char *, char *);
int CRLFsend(char *buf, char *expect, int sd) {
  char iobuf[4098];
  int nread = 0;
  if (SendBuffer(sd, buf, strlen(buf))) {
    perror(buf);
    return 1;
  }
  if (SendBuffer(sd, "\r\n", 2)) {
    perror(buf);
    return 2;
  }
  do {
    int i;
    if ((i = read(sd, iobuf + nread, 4098 - nread)) <= 0) {
      perror(buf);
      return 3;
    }
    nread += i;
    if ((iobuf[nread - 2] == '\r') && (iobuf[nread - 1] == '\n')) {
      iobuf[nread - 2] = '\0';
      goto compare;
    }
  } while (nread < 4098);
  iobuf[4098 - 1] = '\0';
  fprintf(stderr, "CRLFsend %s:  return buffer not terminated.\n", iobuf);
compare:
  if (strstr(iobuf, expect) == NULL) {
    char *p;
    if ((p = strstr(iobuf, "\r\n")) != NULL)
      *p = '\0';
    fprintf(stderr, "CRLFsend %s\n\texpecting %s, got:\n\t%s\n", buf, expect,
            iobuf);
    return 4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<CRLFsend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x1024(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x103c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x1038(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x103c(%rbp),%edi
mov    -0x1038(%rbp),%rsi
mov    %eax,%edx
call   1030 <SendBuffer@plt>
cmp    $0x0,%rax
je     11d0 <CRLFsend+0x70>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136a <CRLFsend+0x20a>
mov    -0x1c(%rbp),%edi
lea    0xe26(%rip),%rsi
mov    $0x2,%edx
call   1030 <SendBuffer@plt>
cmp    $0x0,%rax
je     1203 <CRLFsend+0xa3>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    136a <CRLFsend+0x20a>
jmp    1208 <CRLFsend+0xa8>
mov    -0x1c(%rbp),%edi
lea    -0x1020(%rbp),%rsi
movslq -0x1024(%rbp),%rax
add    %rax,%rsi
mov    $0x1002,%edx
sub    -0x1024(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x1028(%rbp)
cmp    $0x0,%eax
jg     1250 <CRLFsend+0xf0>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    136a <CRLFsend+0x20a>
mov    -0x1028(%rbp),%eax
add    -0x1024(%rbp),%eax
mov    %eax,-0x1024(%rbp)
mov    -0x1024(%rbp),%eax
sub    $0x2,%eax
cltq
movsbl -0x1020(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
jne    12b2 <CRLFsend+0x152>
mov    -0x1024(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x1020(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    12b2 <CRLFsend+0x152>
mov    -0x1024(%rbp),%eax
sub    $0x2,%eax
cltq
movb   $0x0,-0x1020(%rbp,%rax,1)
jmp    12e9 <CRLFsend+0x189>
jmp    12b7 <CRLFsend+0x157>
cmpl   $0x1002,-0x1024(%rbp)
jl     1208 <CRLFsend+0xa8>
movb   $0x0,-0x1f(%rbp)
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rdx
lea    0xd21(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
jne    1363 <CRLFsend+0x203>
lea    -0x1020(%rbp),%rdi
lea    0xcef(%rip),%rsi
call   1080 <strstr@plt>
mov    %rax,-0x1030(%rbp)
cmp    $0x0,%rax
je     1331 <CRLFsend+0x1d1>
mov    -0x1030(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    -0x1020(%rbp),%r8
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x4,-0x4(%rbp)
jmp    136a <CRLFsend+0x20a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CRLFsend>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <SendBuffer@plt>
test   %rax,%rax
jne    1282 <CRLFsend+0x122>
lea    0xe66(%rip),%rsi
mov    $0x2,%ebx
mov    %r12d,%edi
mov    $0x2,%edx
call   1030 <SendBuffer@plt>
test   %rax,%rax
jne    1291 <CRLFsend+0x131>
xor    %ebx,%ebx
mov    %rsp,%rbp
jmp    11c8 <CRLFsend+0x68>
nopl   0x0(%rax)
cmp    $0x1002,%ebx
jge    1200 <CRLFsend+0xa0>
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    $0x1002,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1050 <read@plt>
test   %eax,%eax
jle    129b <CRLFsend+0x13b>
add    %eax,%ebx
movslq %ebx,%rax
cmpb   $0xd,-0x2(%rax,%rbp,1)
jne    11c0 <CRLFsend+0x60>
cmpb   $0xa,-0x1(%rax,%rbp,1)
jne    11c0 <CRLFsend+0x60>
movb   $0x0,-0x2(%rsp,%rax,1)
jmp    1222 <CRLFsend+0xc2>
movb   $0x0,0x1001(%rsp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rsp,%rdi
mov    %r14,%rsi
call   1080 <strstr@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    1270 <CRLFsend+0x110>
lea    0xdc5(%rip),%rsi
mov    %rsp,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     124b <CRLFsend+0xeb>
movb   $0x0,(%rax)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    %rsp,%r8
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x4,%ebx
mov    %ebx,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    1270 <CRLFsend+0x110>
mov    %r15,%rdi
call   1070 <perror@plt>
jmp    1270 <CRLFsend+0x110>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    $0x3,%ebx
jmp    1270 <CRLFsend+0x110>

LLVM IR (-O0):
@.str = [3 x i8] c"\0D\0A\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"CRLFsend %s: return buffer not terminated.\0A\00"
@.str.2 = [37 x i8] c"CRLFsend %s\0A\09expecting %s, got:\0A\09%s\0A\00"
define i32 @CRLFsend(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [4098 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%12 = load i32, i32* %7
%13 = load i8*, i8** %5
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
%16 = call i64 @SendBuffer(i32 %12, i8* %13, i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %3
%19 = load i8*, i8** %5
%20 = call i32 @perror(i8* %19)
store i32 1, i32* %4
br label %93
21: ; preds = %3
%22 = load i32, i32* %7
%23 = call i64 @SendBuffer(i32 %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i8*, i8** %5
%27 = call i32 @perror(i8* %26)
store i32 2, i32* %4
br label %93
28: ; preds = %21
br label %29
29: ; preds = %67, %28
%30 = load i32, i32* %7
%31 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 0
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32, i32* %9
%36 = sub nsw i32 4098, %35
%37 = call i32 @read(i32 %30, i8* %34, i32 %36)
store i32 %37, i32* %10
%38 = icmp sle i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %29
%40 = load i8*, i8** %5
%41 = call i32 @perror(i8* %40)
store i32 3, i32* %4
br label %93
42: ; preds = %29
%43 = load i32, i32* %10
%44 = load i32, i32* %9
%45 = add nsw i32 %44, %43
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = sub nsw i32 %46, 2
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 13
br i1 %52, label %53, label %66
53: ; preds = %42
%54 = load i32, i32* %9
%55 = sub nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 10
br i1 %60, label %61, label %66
61: ; preds = %53
%62 = load i32, i32* %9
%63 = sub nsw i32 %62, 2
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 %64
store i8 0, i8* %65
br label %75
66: ; preds = %53, %42
br label %67
67: ; preds = %66
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 4098
br i1 %69, label %29, label %70
70: ; preds = %67
%71 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 4097
store i8 0, i8* %71
%72 = load i32, i32* @stderr
%73 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 0
%74 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %70, %61
%76 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 0
%77 = load i8*, i8** %6
%78 = call i8* @strstr(i8* %76, i8* %77)
%79 = icmp eq i8* %78, null
br i1 %79, label %80, label %92
80: ; preds = %75
%81 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 0
%82 = call i8* @strstr(i8* %81, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %82, i8** %11
%83 = icmp ne i8* %82, null
br i1 %83, label %84, label %86
84: ; preds = %80
%85 = load i8*, i8** %11
store i8 0, i8* %85
br label %86
86: ; preds = %84, %80
%87 = load i32, i32* @stderr
%88 = load i8*, i8** %5
%89 = load i8*, i8** %6
%90 = getelementptr inbounds [4098 x i8], [4098 x i8]* %8, i64 0, i64 0
%91 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %87, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %88, i8* %89, i8* %90)
store i32 4, i32* %4
br label %93
92: ; preds = %75
store i32 0, i32* %4
br label %93
93: ; preds = %92, %86, %39, %25, %18
%94 = load i32, i32* %4
ret i32 %94
}

LLVM IR (-O2):
@.str = [3 x i8] c"\0D\0A\00"
@stderr = global i32 0
@.str.1 = [45 x i8] c"CRLFsend %s: return buffer not terminated.\0A\00"
@.str.2 = [37 x i8] c"CRLFsend %s\0A\09expecting %s, got:\0A\09%s\0A\00"
define i32 @CRLFsend(i8* %0, i8* %1, i32 %2) {
%4 = alloca [4098 x i8]
%5 = getelementptr inbounds [4098 x i8], [4098 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i64 @SendBuffer(i32 %2, i8* %0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 @perror(i8* %0)
br label %56
11: ; preds = %3
%12 = tail call i64 @SendBuffer(i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* %0)
br label %56
16: ; preds = %11, %40
%17 = phi i32 [ %26, %40 ], [ 0, %11 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [4098 x i8], [4098 x i8]* %4, i64 0, i64 %18
%20 = sub nsw i32 4098, %17
%21 = call i32 @read(i32 %2, i8* %19, i32 %20)
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = tail call i32 @perror(i8* %0)
br label %56
25: ; preds = %16
%26 = add nsw i32 %21, %17
%27 = add nsw i32 %26, -2
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [4098 x i8], [4098 x i8]* %4, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 13
br i1 %31, label %32, label %40
32: ; preds = %25
%33 = add nsw i32 %26, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [4098 x i8], [4098 x i8]* %4, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 10
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = getelementptr inbounds [4098 x i8], [4098 x i8]* %4, i64 0, i64 %28
store i8 0, i8* %39
br label %46
40: ; preds = %32, %25
%41 = icmp slt i32 %26, 4098
br i1 %41, label %16, label %42
42: ; preds = %40
%43 = getelementptr inbounds [4098 x i8], [4098 x i8]* %4, i64 0, i64 4097
store i8 0, i8* %43
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %46
46: ; preds = %38, %42
%47 = call i8* @strstr(i8* %5, i8* dereferenceable(1) %1)
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %56
49: ; preds = %46
%50 = call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%51 = icmp eq i8* %50, null
br i1 %51, label %53, label %52
52: ; preds = %49
store i8 0, i8* %50
br label %53
53: ; preds = %52, %49
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1, i8* %5)
br label %56
56: ; preds = %23, %46, %53, %14, %9
%57 = phi i32 [ 1, %9 ], [ 2, %14 ], [ 4, %53 ], [ 0, %46 ], [ 3, %23 ]
ret i32 %57
}