#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 fdgets(int, char *, int);
int /*<<< orphan*/ printf(char *, int);
int strlen(char *);
scalar_t__ tcp_wait_for_socket(int, int, int);
int pop3_response(int fd, char *out, int len, int wait) {
  int ret;
  int lindex;
  if (tcp_wait_for_socket(fd, 10, 1) <= 0)
    return (-1);
  ret = fdgets(fd, out, len - 1);
  if (ret <= 0) {
    printf("Got back %d from ret!\n", ret);
    return (-1);
  }
  lindex = (strlen(out) - 1);
  if (out[lindex] == '\n' && out[lindex - 1] == '\r') {
    out[lindex - 1] = '\n';
    out[lindex] = 0;
  }
  return (0);
}


x86 assembly (clang, -O0):
<pop3_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
mov    $0x1,%edx
call   1030 <tcp_wait_for_socket@plt>
cmp    $0x0,%rax
jg     117d <pop3_response+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <pop3_response+0xd9>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1060 <fdgets@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11b7 <pop3_response+0x77>
mov    -0x1c(%rbp),%esi
lea    0xe5a(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <pop3_response+0xd9>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1212 <pop3_response+0xd2>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1212 <pop3_response+0xd2>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movslq -0x20(%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):
<pop3_response>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%ebx
mov    $0xa,%esi
mov    $0x1,%edx
call   1030 <tcp_wait_for_socket@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jle    11b4 <pop3_response+0x74>
add    $0xffffffff,%ebp
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1060 <fdgets@plt>
test   %eax,%eax
jle    11a6 <pop3_response+0x66>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
xor    %r14d,%r14d
cmpb   $0xa,-0x1(%rax,%r15,1)
jne    11b4 <pop3_response+0x74>
cmpb   $0xd,-0x2(%rax,%r15,1)
jne    11b4 <pop3_response+0x74>
movb   $0xa,-0x2(%r15,%rax,1)
movb   $0x0,-0x1(%r15,%rax,1)
jmp    11b4 <pop3_response+0x74>
lea    0xe53(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [23 x i8] c"Got back %d from ret!\0A\00"
define i32 @pop3_response(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i64 @tcp_wait_for_socket(i32 %0, i32 10, i32 1)
%6 = icmp slt i64 %5, 1
br i1 %6, label %27, label %7
7: ; preds = %4
%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 %11, label %13
11: ; preds = %7
%12 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %27
13: ; preds = %7
%14 = tail call i32 @strlen(i8* %1)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %20, label %27
20: ; preds = %13
%21 = add nsw i32 %14, -2
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 13
br i1 %25, label %26, label %27
26: ; preds = %20
store i8 10, i8* %23
store i8 0, i8* %17
br label %27
27: ; preds = %13, %20, %26, %4, %11
%28 = phi i32 [ -1, %11 ], [ -1, %4 ], [ 0, %26 ], [ 0, %20 ], [ 0, %13 ]
ret i32 %28
}