#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*/ *recvline(char *, int, int);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, size_t);
int getheaders(int fd, char *buf, size_t len) {
  char line[256], *cp;
  *line = *buf = '\0';
  while (!(*line == '\n' || (line[0] == '\r' && line[1] == '\n'))) {
    if (recvline(line, 256, fd) == NULL)
      return -1;
    ;
    strncat(buf, line, len - strlen(line) - 5);
    buf[len - 1] = '\0';
  }
  cp = buf + strlen(buf);
  while (cp > buf && (*(cp - 1) == '\r' || *(cp - 1) == '\n'))
    cp--;
  strcpy(cp, "\r\n\r\n");
  return 0;
}


x86 assembly (clang, -O0):
<getheaders>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movb   $0x0,-0x120(%rbp)
movsbl -0x120(%rbp),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x129(%rbp)
je     11b3 <getheaders+0x73>
movsbl -0x120(%rbp),%ecx
xor    %eax,%eax
cmp    $0xd,%ecx
mov    %al,-0x12a(%rbp)
jne    11a7 <getheaders+0x67>
movsbl -0x11f(%rbp),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x12a(%rbp)
mov    -0x12a(%rbp),%al
mov    %al,-0x129(%rbp)
mov    -0x129(%rbp),%al
xor    $0xff,%al
test   $0x1,%al
jne    11c8 <getheaders+0x88>
jmp    1258 <getheaders+0x118>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    $0x100,%esi
call   1060 <recvline@plt>
cmp    $0x0,%rax
jne    11f2 <getheaders+0xb2>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <getheaders+0x1dc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x140(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x138(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x148(%rbp),%rdx
mov    -0x140(%rbp),%rdi
mov    -0x138(%rbp),%rsi
sub    %rax,%rdx
sub    $0x5,%rdx
call   1050 <strncat@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1164 <getheaders+0x24>
mov    -0x10(%rbp),%rax
mov    %rax,-0x150(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x150(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x151(%rbp)
jbe    12d8 <getheaders+0x198>
mov    -0x128(%rbp),%rax
movsbl -0x1(%rax),%ecx
mov    $0x1,%al
cmp    $0xd,%ecx
mov    %al,-0x152(%rbp)
je     12cc <getheaders+0x18c>
mov    -0x128(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x152(%rbp)
mov    -0x152(%rbp),%al
mov    %al,-0x151(%rbp)
mov    -0x151(%rbp),%al
test   $0x1,%al
jne    12eb <getheaders+0x1ab>
jmp    1302 <getheaders+0x1c2>
mov    -0x128(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x128(%rbp)
jmp    1280 <getheaders+0x140>
mov    -0x128(%rbp),%rdi
lea    0xcf0(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getheaders>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
movb   $0x0,(%rsi)
movb   $0x0,(%rsp)
lea    -0x5(%rdx),%r12
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %r15d,%edx
call   1060 <recvline@plt>
test   %rax,%rax
je     11f5 <getheaders+0xb5>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r12,%rdx
sub    %rax,%rdx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <strncat@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
movzbl (%rsp),%eax
cmp    $0xa,%al
je     11b7 <getheaders+0x77>
cmp    $0xd,%al
jne    1170 <getheaders+0x30>
cmpb   $0xa,0x1(%rsp)
jne    1170 <getheaders+0x30>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%rdi
test   %rax,%rax
jg     11d9 <getheaders+0x99>
jmp    11e5 <getheaders+0xa5>
nopw   0x0(%rax,%rax,1)
add    $0xffffffffffffffff,%rdi
cmp    %rbx,%rdi
jbe    11e5 <getheaders+0xa5>
movzbl -0x1(%rdi),%eax
cmp    $0xd,%al
je     11d0 <getheaders+0x90>
cmp    $0xa,%al
je     11d0 <getheaders+0x90>
lea    0xe14(%rip),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
jmp    11fa <getheaders+0xba>
mov    $0xffffffff,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
define i32 @getheaders(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [256 x i8]
%9 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
store i8 0, i8* %10
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
store i8 0, i8* %11
br label %12
12: ; preds = %38, %3
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 10
br i1 %16, label %29, label %17
17: ; preds = %12
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 13
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 1
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 10
br label %27
27: ; preds = %22, %17
%28 = phi i1 [ false, %17 ], [ %26, %22 ]
br label %29
29: ; preds = %27, %12
%30 = phi i1 [ true, %12 ], [ %28, %27 ]
%31 = xor i1 %30, true
br i1 %31, label %32, label %51
32: ; preds = %29
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%34 = load i32, i32* %5
%35 = call i32* @recvline(i8* %33, i32 256, i32 %34)
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -1, i32* %4
br label %82
38: ; preds = %32
%39 = load i8*, i8** %6
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%41 = load i64, i64* %7
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%43 = call i64 @strlen(i8* %42)
%44 = sub i64 %41, %43
%45 = sub i64 %44, 5
%46 = call i32 @strncat(i8* %39, i8* %40, i64 %45)
%47 = load i8*, i8** %6
%48 = load i64, i64* %7
%49 = sub i64 %48, 1
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8 0, i8* %50
br label %12
51: ; preds = %29
%52 = load i8*, i8** %6
%53 = load i8*, i8** %6
%54 = call i64 @strlen(i8* %53)
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8* %55, i8** %9
br label %56
56: ; preds = %76, %51
%57 = load i8*, i8** %9
%58 = load i8*, i8** %6
%59 = icmp ugt i8* %57, %58
br i1 %59, label %60, label %74
60: ; preds = %56
%61 = load i8*, i8** %9
%62 = getelementptr inbounds i8, i8* %61, i64 -1
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp eq i32 %64, 13
br i1 %65, label %72, label %66
66: ; preds = %60
%67 = load i8*, i8** %9
%68 = getelementptr inbounds i8, i8* %67, i64 -1
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp eq i32 %70, 10
br label %72
72: ; preds = %66, %60
%73 = phi i1 [ true, %60 ], [ %71, %66 ]
br label %74
74: ; preds = %72, %56
%75 = phi i1 [ false, %56 ], [ %73, %72 ]
br i1 %75, label %76, label %79
76: ; preds = %74
%77 = load i8*, i8** %9
%78 = getelementptr inbounds i8, i8* %77, i32 -1
store i8* %78, i8** %9
br label %56
79: ; preds = %74
%80 = load i8*, i8** %9
%81 = call i32 @strcpy(i8* %80, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %82
82: ; preds = %79, %37
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
@.str = [5 x i8] c"\0D\0A\0D\0A\00"
define i32 @getheaders(i32 %0, i8* %1, i64 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
store i8 0, i8* %1
store i8 0, i8* %5
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 1
%7 = add i64 %2, -5
%8 = add i64 %2, -1
%9 = getelementptr inbounds i8, i8* %1, i64 %8
br label %10
10: ; preds = %3, %19
%11 = call i32* @recvline(i8* %5, i32 256, i32 %0)
%12 = icmp eq i32* %11, null
br i1 %12, label %37, label %13
13: ; preds = %10
%14 = call i64 @strlen(i8* %5)
%15 = sub i64 %7, %14
%16 = call i32 @strncat(i8* %1, i8* %5, i64 %15)
store i8 0, i8* %9
%17 = load i8, i8* %5
%18 = icmp eq i8 %17, 10
br i1 %18, label %24, label %19
19: ; preds = %13
%20 = load i8, i8* %6
%21 = icmp eq i8 %17, 13
%22 = icmp eq i8 %20, 10
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %10
24: ; preds = %13, %19
%25 = call i64 @strlen(i8* %1)
%26 = getelementptr inbounds i8, i8* %1, i64 %25
%27 = icmp sgt i64 %25, 0
br i1 %27, label %28, label %34
28: ; preds = %24, %32
%29 = phi i8* [ %30, %32 ], [ %26, %24 ]
%30 = getelementptr inbounds i8, i8* %29, i64 -1
%31 = load i8, i8* %30
switch i8 %31, label %34 [
i8 13, label %32
i8 10, label %32
]
32: ; preds = %28, %28
%33 = icmp ugt i8* %30, %1
br i1 %33, label %28, label %34
34: ; preds = %32, %28, %24
%35 = phi i8* [ %26, %24 ], [ %29, %28 ], [ %30, %32 ]
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %10, %34
%38 = phi i32 [ 0, %34 ], [ -1, %10 ]
ret i32 %38
}