#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*/ bzero(char *, int);
int fdgets(int, char *, int);
int strlen(char *);
scalar_t__ tcp_wait_for_socket(int, int, int);
int smtp_get_response(int fd, char *response, int len) {
  int lindex;
  int ret;
  if (tcp_wait_for_socket(fd, 5, 1) <= 0)
    return (-1);
  bzero(response, len);
  ret = fdgets(fd, response, len - 1);
  if (ret <= 0)
    return (-1);
  lindex = (strlen(response) - 1);
  if (response[lindex] == '\n' && response[lindex - 1] == '\r') {
    response[lindex - 1] = '\n';
    response[lindex] = 0;
  }
  return (0);
}


x86 assembly (clang, -O0):
<smtp_get_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
mov    $0x1,%edx
call   1030 <tcp_wait_for_socket@plt>
cmp    $0x0,%rax
jg     117a <smtp_get_response+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <smtp_get_response+0xd3>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <bzero@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1050 <fdgets@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11b1 <smtp_get_response+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <smtp_get_response+0xd3>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    120c <smtp_get_response+0xcc>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    120c <smtp_get_response+0xcc>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smtp_get_response>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%ebx
mov    $0x5,%esi
mov    $0x1,%edx
call   1030 <tcp_wait_for_socket@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jle    11ae <smtp_get_response+0x6e>
mov    %r15,%rdi
mov    %ebp,%esi
call   1060 <bzero@plt>
add    $0xffffffff,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <fdgets@plt>
test   %eax,%eax
jle    11ae <smtp_get_response+0x6e>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
xor    %r14d,%r14d
cmpb   $0xa,-0x1(%rax,%r15,1)
jne    11ae <smtp_get_response+0x6e>
cmpb   $0xd,-0x2(%rax,%r15,1)
jne    11ae <smtp_get_response+0x6e>
movb   $0xa,-0x2(%r15,%rax,1)
movb   $0x0,-0x1(%r15,%rax,1)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @smtp_get_response(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = call i64 @tcp_wait_for_socket(i32 %10, i32 5, i32 1)
%12 = icmp sle i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %57
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @bzero(i8* %15, i32 %16)
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = sub nsw i32 %20, 1
%22 = call i32 @fdgets(i32 %18, i8* %19, i32 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %14
store i32 -1, i32* %4
br label %57
26: ; preds = %14
%27 = load i8*, i8** %6
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %28, 1
store i32 %29, i32* %8
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %37, label %56
37: ; preds = %26
%38 = load i8*, i8** %6
%39 = load i32, i32* %8
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 13
br i1 %45, label %46, label %56
46: ; preds = %37
%47 = load i8*, i8** %6
%48 = load i32, i32* %8
%49 = sub nsw i32 %48, 1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 10, i8* %51
%52 = load i8*, i8** %6
%53 = load i32, i32* %8
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
br label %56
56: ; preds = %46, %37, %26
store i32 0, i32* %4
br label %57
57: ; preds = %56, %25, %13
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
define i32 @smtp_get_response(i32 %0, i8* %1, i32 %2) {
%4 = tail call i64 @tcp_wait_for_socket(i32 %0, i32 5, i32 1)
%5 = icmp slt i64 %4, 1
br i1 %5, label %25, label %6
6: ; preds = %3
%7 = tail call i32 @bzero(i8* %1, i32 %2)
%8 = add nsw i32 %2, -1
%9 = tail call i32 @fdgets(i32 %0, i8* %1, i32 %8)
%10 = icmp slt i32 %9, 1
br i1 %10, label %25, label %11
11: ; preds = %6
%12 = tail call i32 @strlen(i8* %1)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %25
18: ; preds = %11
%19 = add nsw i32 %12, -2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 13
br i1 %23, label %24, label %25
24: ; preds = %18
store i8 10, i8* %21
store i8 0, i8* %15
br label %25
25: ; preds = %11, %18, %24, %6, %3
%26 = phi i32 [ -1, %3 ], [ -1, %6 ], [ 0, %24 ], [ 0, %18 ], [ 0, %11 ]
ret i32 %26
}