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

/* Variables and functions */
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, scalar_t__);
scalar_t__ strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int recv_reply(int sd, char *rbuf, int len, int flags) {
  char line[1024];
  FILE *fp;
  int nread;
  int done;
  char status[4] = {0};
  if ((fp = fdopen(sd, "r")) == NULL)
    return -1;
  nread = 0;
  done = 0;
  do {
    fgets(line, 1020, fp);
    if (line[3] == '-' && status[0] == 0)
      strncpy(status, line, 3);
    if (line[3] == ' ') {
      if (status[0] != 0) {
        if (strncmp(status, line, 3) == 0)
          done = 1;
      } else
        done = 1;
    }
    memcpy(rbuf + nread, line, strlen(line));
    nread += strlen(line);
  } while (done != 1 && nread < 1022);
  return nread;
}


x86 assembly (clang, -O0):
<recv_reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x434(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    0xe5b(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jne    11c7 <recv_reply+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <recv_reply+0x1be>
movl   $0x0,-0x42c(%rbp)
movl   $0x0,-0x430(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x3fc,%esi
call   1070 <fgets@plt>
movsbl -0x41d(%rbp),%eax
cmp    $0x2d,%eax
jne    122b <recv_reply+0xbb>
movsbl -0x434(%rbp),%eax
cmp    $0x0,%eax
jne    122b <recv_reply+0xbb>
lea    -0x434(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
movsbl -0x41d(%rbp),%eax
cmp    $0x20,%eax
jne    128b <recv_reply+0x11b>
movsbl -0x434(%rbp),%eax
cmp    $0x0,%eax
je     127c <recv_reply+0x10c>
lea    -0x434(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1277 <recv_reply+0x107>
movl   $0x1,-0x430(%rbp)
jmp    1286 <recv_reply+0x116>
movl   $0x1,-0x430(%rbp)
jmp    128b <recv_reply+0x11b>
mov    -0x10(%rbp),%rax
movslq -0x42c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x448(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x448(%rbp),%rdi
mov    -0x440(%rbp),%rsi
mov    %rax,%rdx
call   1080 <memcpy@plt>
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
movslq -0x42c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x42c(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x430(%rbp)
mov    %al,-0x449(%rbp)
je     1317 <recv_reply+0x1a7>
cmpl   $0x3fe,-0x42c(%rbp)
setl   %al
mov    %al,-0x449(%rbp)
mov    -0x449(%rbp),%al
test   $0x1,%al
jne    11db <recv_reply+0x6b>
mov    -0x42c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_reply>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r14
movl   $0x0,0xc(%rsp)
lea    0xe7d(%rip),%rsi
call   1080 <fdopen@plt>
test   %rax,%rax
je     124e <recv_reply+0xee>
mov    %rax,%r15
xor    %ebx,%ebx
lea    0x10(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x3fc,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
movzbl 0x13(%rsp),%eax
cmp    $0x2d,%al
jne    11d7 <recv_reply+0x77>
cmpb   $0x0,0xc(%rsp)
jne    11d7 <recv_reply+0x77>
lea    0xc(%rsp),%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
movzbl 0x13(%rsp),%eax
mov    $0x1,%bpl
cmp    $0x20,%al
jne    1210 <recv_reply+0xb0>
cmpb   $0x0,0xc(%rsp)
je     1200 <recv_reply+0xa0>
lea    0xc(%rsp),%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
setne  %bpl
jmp    1210 <recv_reply+0xb0>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebx,%r12
lea    (%r14,%r12,1),%rbx
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %rax,%rdx
call   1070 <memcpy@plt>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbx
add    %r12,%rbx
test   %bpl,%bpl
je     1253 <recv_reply+0xf3>
cmp    $0x3fe,%ebx
jl     11a0 <recv_reply+0x40>
jmp    1253 <recv_reply+0xf3>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @recv_reply(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 [1024 x i8]
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca [4 x i8]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%15 = bitcast [4 x i8]* %14 to i8*
%16 = load i32, i32* %6
%17 = call i32* @fdopen(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %11
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %4
store i32 -1, i32* %5
br label %82
20: ; preds = %4
store i32 0, i32* %12
store i32 0, i32* %13
br label %21
21: ; preds = %78, %20
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%23 = load i32*, i32** %11
%24 = call i32 @fgets(i8* %22, i32 1020, i32* %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 3
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 45
br i1 %28, label %29, label %38
29: ; preds = %21
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%37 = call i32 @strncpy(i8* %35, i8* %36, i32 3)
br label %38
38: ; preds = %34, %29, %21
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 3
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 32
br i1 %42, label %43, label %57
43: ; preds = %38
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %14, i64 0, i64 0
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%51 = call i64 @strncmp(i8* %49, i8* %50, i32 3)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %48
store i32 1, i32* %13
br label %54
54: ; preds = %53, %48
br label %56
55: ; preds = %43
store i32 1, i32* %13
br label %56
56: ; preds = %55, %54
br label %57
57: ; preds = %56, %38
%58 = load i8*, i8** %7
%59 = load i32, i32* %12
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%64 = call i64 @strlen(i8* %63)
%65 = call i32 @memcpy(i8* %61, i8* %62, i64 %64)
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%67 = call i64 @strlen(i8* %66)
%68 = load i32, i32* %12
%69 = sext i32 %68 to i64
%70 = add nsw i64 %69, %67
%71 = trunc i64 %70 to i32
store i32 %71, i32* %12
br label %72
72: ; preds = %57
%73 = load i32, i32* %13
%74 = icmp ne i32 %73, 1
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32, i32* %12
%77 = icmp slt i32 %76, 1022
br label %78
78: ; preds = %75, %72
%79 = phi i1 [ false, %72 ], [ %77, %75 ]
br i1 %79, label %21, label %80
80: ; preds = %78
%81 = load i32, i32* %12
store i32 %81, i32* %5
br label %82
82: ; preds = %80, %19
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @recv_reply(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%9 = tail call i32* @fdopen(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %47, label %11
11: ; preds = %4
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 3
br label %13
13: ; preds = %11, %33
%14 = phi i64 [ %41, %33 ], [ 0, %11 ]
%15 = call i32 @fgets(i8* %7, i32 1020, i32* %9)
%16 = load i8, i8* %12
%17 = icmp eq i8 %16, 45
%18 = load i8, i8* %8
%19 = icmp eq i8 %18, 0
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = call i32 @strncpy(i8* %8, i8* %7, i32 3)
%23 = load i8, i8* %12
br label %24
24: ; preds = %21, %13
%25 = phi i8 [ %23, %21 ], [ %16, %13 ]
%26 = icmp eq i8 %25, 32
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i8, i8* %8
%29 = icmp eq i8 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i64 @strncmp(i8* %8, i8* %7, i32 3)
%32 = icmp ne i64 %31, 0
br label %33
33: ; preds = %30, %27, %24
%34 = phi i1 [ true, %24 ], [ %32, %30 ], [ false, %27 ]
%35 = shl i64 %14, 32
%36 = ashr exact i64 %35, 32
%37 = getelementptr inbounds i8, i8* %1, i64 %36
%38 = call i64 @strlen(i8* %7)
%39 = call i32 @memcpy(i8* %37, i8* %7, i64 %38)
%40 = call i64 @strlen(i8* %7)
%41 = add nsw i64 %40, %36
%42 = trunc i64 %41 to i32
%43 = icmp slt i32 %42, 1022
%44 = select i1 %34, i1 %43, i1 false
br i1 %44, label %13, label %45
45: ; preds = %33
%46 = trunc i64 %41 to i32
br label %47
47: ; preds = %45, %4
%48 = phi i32 [ -1, %4 ], [ %46, %45 ]
ret i32 %48
}