#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memmove(char *, char *, unsigned int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int getsmtp(int socket, char *data) {
  int cc;
  static unsigned int bytesinbuf = 0;
  static char buffer[1024 * 2], *p;
  for (;;) {
    if (bytesinbuf > 0 && (p = strchr(buffer, '\r')) && *(p + 1) == '\n') {
      *p = '\0';
      strncpy(data, buffer, 1024);
      data[1024 - 1] = '\0';
      cc = strlen(buffer);
      bytesinbuf -= cc + 2;
      if (bytesinbuf > 0) {
        memmove(buffer, buffer + cc + 2, bytesinbuf);
      }
      return cc;
    }
    if (bytesinbuf >= sizeof(buffer)) {
      fprintf(stderr, "Buffer overflow in getsmtp()!\n");
      exit(1);
    }
    memset(buffer + bytesinbuf, 0, sizeof(buffer) - bytesinbuf);
    cc = read(socket, buffer + bytesinbuf, sizeof(buffer) - bytesinbuf);
    if (cc < 0) {
      fprintf(stderr, "Read failed: %s\n", strerror(errno));
      exit(1);
    }
    if (cc == 0)
      return -1;
    bytesinbuf += cc;
  }
}


x86 assembly (clang, -O0):
<getsmtp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,0x2eda(%rip)
jbe    126b <getsmtp+0xdb>
lea    0x2edd(%rip),%rdi
mov    $0xd,%esi
call   1050 <strchr@plt>
mov    %rax,0x36cc(%rip)
cmp    $0x0,%rax
je     126b <getsmtp+0xdb>
mov    0x36bb(%rip),%rax
movsbl 0x1(%rax),%eax
cmp    $0xa,%eax
jne    126b <getsmtp+0xdb>
mov    0x36a7(%rip),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0x2e99(%rip),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x3ff(%rax)
lea    0x2e7d(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
add    $0x2,%ecx
mov    0x2e59(%rip),%eax
sub    %ecx,%eax
mov    %eax,0x2e51(%rip)
cmpl   $0x0,0x2e4a(%rip)
jbe    1260 <getsmtp+0xd0>
movslq -0x14(%rbp),%rax
lea    0x2e49(%rip),%rsi
add    %rax,%rsi
add    $0x2,%rsi
mov    0x2e2c(%rip),%edx
lea    0x2e35(%rip),%rdi
call   1090 <memmove@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1360 <getsmtp+0x1d0>
mov    0x2e0f(%rip),%eax
cmp    $0x800,%rax
jb     129e <getsmtp+0x10e>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2ddc(%rip),%eax
lea    0x2de5(%rip),%rdi
add    %rax,%rdi
mov    0x2dcc(%rip),%eax
mov    %eax,%ecx
mov    $0x800,%eax
sub    %rcx,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
mov    0x2db0(%rip),%eax
lea    0x2db9(%rip),%rsi
add    %rax,%rsi
mov    0x2da0(%rip),%eax
mov    %eax,%ecx
mov    $0x800,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1336 <getsmtp+0x1a6>
mov    0x2cdb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xcfa(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    134c <getsmtp+0x1bc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1360 <getsmtp+0x1d0>
mov    -0x14(%rbp),%eax
add    0x2d2b(%rip),%eax
mov    %eax,0x2d25(%rip)
jmp    119f <getsmtp+0xf>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsmtp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2edd(%rip),%ebp
lea    0x2ee6(%rip),%rbx
test   %ebp,%ebp
je     11db <getsmtp+0x4b>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0xd,%esi
call   1050 <strchr@plt>
mov    %rax,0x36cc(%rip)
test   %rax,%rax
je     11cf <getsmtp+0x3f>
cmpb   $0xa,0x1(%rax)
je     1232 <getsmtp+0xa2>
cmp    $0x800,%ebp
jae    12ba <getsmtp+0x12a>
mov    %ebp,%edi
add    %rbx,%rdi
mov    $0x800,%edx
sub    %ebp,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2e8c(%rip),%esi
mov    $0x800,%edx
sub    %esi,%edx
add    %rbx,%rsi
mov    %r15d,%edi
call   1070 <read@plt>
test   %eax,%eax
js     1286 <getsmtp+0xf6>
je     1220 <getsmtp+0x90>
mov    %eax,%ebp
add    0x2e6c(%rip),%ebp
mov    %ebp,0x2e66(%rip)
test   %ebp,%ebp
jne    11b0 <getsmtp+0x20>
jmp    11db <getsmtp+0x4b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%rax)
lea    0x2e54(%rip),%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3ff(%r14)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    0x2e1c(%rip),%edx
sub    %eax,%edx
add    $0xfffffffe,%edx
mov    %edx,0x2e11(%rip)
je     1225 <getsmtp+0x95>
movslq %ebx,%rax
lea    (%rax,%rbp,1),%rsi
add    $0x2,%rsi
mov    %rbp,%rdi
call   1090 <memmove@plt>
jmp    1225 <getsmtp+0x95>
mov    0x2d53(%rip),%rax
mov    (%rax),%ebx
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd7b(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd36(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@getsmtp.bytesinbuf = internal global i32 0
@getsmtp.buffer = internal global [2048 x i8] zeroinitializer
@getsmtp.p = internal global i8* null
@stderr = global i32 0
@.str = [31 x i8] c"Buffer overflow in getsmtp()!\0A\00"
@.str.1 = [17 x i8] c"Read failed: %s\0A\00"
@errno = global i32 0
define i32 @getsmtp(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
br label %7
7: ; preds = %79, %2
%8 = load i32, i32* @getsmtp.bytesinbuf
%9 = icmp ugt i32 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %7
%11 = call i8* @strchr(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i8 signext 13)
store i8* %11, i8** @getsmtp.p
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %41
13: ; preds = %10
%14 = load i8*, i8** @getsmtp.p
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 10
br i1 %18, label %19, label %41
19: ; preds = %13
%20 = load i8*, i8** @getsmtp.p
store i8 0, i8* %20
%21 = load i8*, i8** %5
%22 = call i32 @strncpy(i8* %21, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i32 1024)
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i64 1023
store i8 0, i8* %24
%25 = call i32 @strlen(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0))
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = add nsw i32 %26, 2
%28 = load i32, i32* @getsmtp.bytesinbuf
%29 = sub i32 %28, %27
store i32 %29, i32* @getsmtp.bytesinbuf
%30 = load i32, i32* @getsmtp.bytesinbuf
%31 = icmp ugt i32 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %19
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i64 %34
%36 = getelementptr inbounds i8, i8* %35, i64 2
%37 = load i32, i32* @getsmtp.bytesinbuf
%38 = call i32 @memmove(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i8* %36, i32 %37)
br label %39
39: ; preds = %32, %19
%40 = load i32, i32* %6
store i32 %40, i32* %3
br label %83
41: ; preds = %13, %10, %7
%42 = load i32, i32* @getsmtp.bytesinbuf
%43 = zext i32 %42 to i64
%44 = icmp uge i64 %43, 2048
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = load i32, i32* @getsmtp.bytesinbuf
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i64 %51
%53 = load i32, i32* @getsmtp.bytesinbuf
%54 = zext i32 %53 to i64
%55 = sub i64 2048, %54
%56 = trunc i64 %55 to i32
%57 = call i32 @memset(i8* %52, i32 0, i32 %56)
%58 = load i32, i32* %4
%59 = load i32, i32* @getsmtp.bytesinbuf
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i64 %60
%62 = load i32, i32* @getsmtp.bytesinbuf
%63 = zext i32 %62 to i64
%64 = sub i64 2048, %63
%65 = trunc i64 %64 to i32
%66 = call i32 @read(i32 %58, i8* %61, i32 %65)
store i32 %66, i32* %6
%67 = load i32, i32* %6
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %49
%70 = load i32, i32* @stderr
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %72)
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %49
%76 = load i32, i32* %6
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %75
store i32 -1, i32* %3
br label %83
79: ; preds = %75
%80 = load i32, i32* %6
%81 = load i32, i32* @getsmtp.bytesinbuf
%82 = add i32 %81, %80
store i32 %82, i32* @getsmtp.bytesinbuf
br label %7
83: ; preds = %78, %39
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@getsmtp.bytesinbuf = internal unnamed_addr global i32 0
@getsmtp.buffer = internal global [2048 x i8] zeroinitializer
@getsmtp.p = internal unnamed_addr global i8* null
@stderr = global i32 0
@.str = [31 x i8] c"Buffer overflow in getsmtp()!\0A\00"
@.str.1 = [17 x i8] c"Read failed: %s\0A\00"
@errno = global i32 0
define i32 @getsmtp(i32 %0, i8* %1) {
%3 = load i32, i32* @getsmtp.bytesinbuf
br label %4
4: ; preds = %52, %2
%5 = phi i32 [ %54, %52 ], [ %3, %2 ]
%6 = icmp eq i32 %5, 0
br i1 %6, label %33, label %7
7: ; preds = %4
%8 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i8 signext 13)
store i8* %8, i8** @getsmtp.p
%9 = icmp eq i8* %8, null
br i1 %9, label %27, label %10
10: ; preds = %7
%11 = getelementptr inbounds i8, i8* %8, i64 1
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 10
br i1 %13, label %14, label %27
14: ; preds = %10
store i8 0, i8* %8
%15 = tail call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i32 1024)
%16 = getelementptr inbounds i8, i8* %1, i64 1023
store i8 0, i8* %16
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0))
%18 = load i32, i32* @getsmtp.bytesinbuf
%19 = sub i32 -2, %17
%20 = add i32 %19, %18
store i32 %20, i32* @getsmtp.bytesinbuf
%21 = icmp eq i32 %20, 0
br i1 %21, label %55, label %22
22: ; preds = %14
%23 = sext i32 %17 to i64
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 %23
%25 = getelementptr inbounds i8, i8* %24, i64 2
%26 = tail call i32 @memmove(i8* getelementptr inbounds ([2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 0), i8* %25, i32 %20)
br label %55
27: ; preds = %10, %7
%28 = icmp ugt i32 %5, 2047
br i1 %28, label %29, label %33
29: ; preds = %27
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %4, %27
%34 = zext i32 %5 to i64
%35 = getelementptr inbounds [2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 %34
%36 = sub nuw nsw i32 2048, %5
%37 = tail call i32 @memset(i8* %35, i32 0, i32 %36)
%38 = load i32, i32* @getsmtp.bytesinbuf
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds [2048 x i8], [2048 x i8]* @getsmtp.buffer, i64 0, i64 %39
%41 = sub i32 2048, %38
%42 = tail call i32 @read(i32 %0, i8* %40, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = tail call i8* @strerror(i32 %46)
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %47)
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %33
%51 = icmp eq i32 %42, 0
br i1 %51, label %55, label %52
52: ; preds = %50
%53 = load i32, i32* @getsmtp.bytesinbuf
%54 = add i32 %53, %42
store i32 %54, i32* @getsmtp.bytesinbuf
br label %4
55: ; preds = %50, %14, %22
%56 = phi i32 [ %17, %22 ], [ %17, %14 ], [ -1, %50 ]
ret i32 %56
}