#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 */
char *findheadend(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ isdigit(char);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *strstr(char *, char *);
scalar_t__ strtol(char *, char **, int);
int parsehead(char *resp, int size) {
  char *startnum, *end;
  int fullsize;
  int i;
  char *temp = malloc(sizeof(char) * (size + 1));
  for (i = 0; i < size; i++) {
    temp[i] = resp[i];
  }
  temp[size] = 0;
  printf("---------------------------------------\n%s\n------------------------"
         "------------------\n",
         temp);
  startnum = strstr(temp, "Content-Length");
  if (startnum != NULL) {
    printf("Content length is provided\n");
    while (!isdigit(*startnum)) {
      startnum++;
    }
    end = startnum;
    while (*(end++) != '\n')
      ;
    fullsize = (int)strtol(startnum, &end, 10);
    end = findheadend(temp);
    free(temp);
    return (fullsize + sizeof(char) * (resp - end));
  } else {
    printf("Length is not provided\n");
    free(temp);
    return 0;
  }
}


x86 assembly (clang, -O0):
<parsehead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11cd <parsehead+0x5d>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x38(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    119d <parsehead+0x2d>
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rsi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12bb <parsehead+0x14b>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1239 <parsehead+0xc9>
jmp    124a <parsehead+0xda>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1218 <parsehead+0xa8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1272 <parsehead+0x102>
jmp    1252 <parsehead+0xe2>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <findheadend@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movslq -0x2c(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x28(%rbp),%rdx
sub    %rdx,%rcx
shl    $0x0,%rcx
add    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    12d9 <parsehead+0x169>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parsehead>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x1(%rbx),%edi
call   10a0 <malloc@plt>
mov    %rax,%r15
test   %ebx,%ebx
jle    11b8 <parsehead+0x28>
mov    %ebx,%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <memcpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
lea    0xe39(%rip),%rdi
xor    %ebp,%ebp
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe7d(%rip),%rsi
mov    %r15,%rdi
call   10b0 <strstr@plt>
test   %rax,%rax
je     125e <parsehead+0xce>
mov    %rax,%rbx
lea    0xe8c(%rip),%rdi
call   1050 <puts@plt>
add    $0xffffffffffffffff,%rbx
nopw   0x0(%rax,%rax,1)
movsbl 0x1(%rbx),%edi
add    $0x1,%rbx
call   1040 <isdigit@plt>
test   %eax,%eax
je     1200 <parsehead+0x70>
mov    %rbx,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmpb   $0xa,(%rax)
lea    0x1(%rax),%rax
jne    1220 <parsehead+0x90>
mov    %rax,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1070 <findheadend@plt>
mov    %rax,%rbp
mov    %r15,%rdi
call   1030 <free@plt>
add    %ebx,%r14d
sub    %ebp,%r14d
mov    %r14d,%ebp
jmp    1272 <parsehead+0xe2>
lea    0xe01(%rip),%rdi
call   1050 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [87 x i8] c"---------------------------------------\0A%s\0A------------------------------------------\0A\00"
@.str.1 = [15 x i8] c"Content-Length\00"
@.str.2 = [28 x i8] c"Content length is provided\0A\00"
@.str.3 = [24 x i8] c"Length is not provided\0A\00"
define i32 @parsehead(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = trunc i64 %14 to i32
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %10
store i32 0, i32* %9
br label %17
17: ; preds = %31, %2
%18 = load i32, i32* %9
%19 = load i32, i32* %5
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = load i8*, i8** %4
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = load i8*, i8** %10
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 %26, i8* %30
br label %31
31: ; preds = %21
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
br label %17
34: ; preds = %17
%35 = load i8*, i8** %10
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i8*, i8** %10
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %10
%42 = call i8* @strstr(i8* %41, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* %42, i8** %6
%43 = load i8*, i8** %6
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %83
45: ; preds = %34
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %53, %45
%48 = load i8*, i8** %6
%49 = load i8, i8* %48
%50 = call i32 @isdigit(i8 signext %49)
%51 = icmp ne i32 %50, 0
%52 = xor i1 %51, true
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = load i8*, i8** %6
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %6
br label %47
56: ; preds = %47
%57 = load i8*, i8** %6
store i8* %57, i8** %7
br label %58
58: ; preds = %64, %56
%59 = load i8*, i8** %7
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %7
%61 = load i8, i8* %59
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 10
br i1 %63, label %64, label %65
64: ; preds = %58
br label %58
65: ; preds = %58
%66 = load i8*, i8** %6
%67 = call i64 @strtol(i8* %66, i8** %7, i32 10)
%68 = trunc i64 %67 to i32
store i32 %68, i32* %8
%69 = load i8*, i8** %10
%70 = call i8* @findheadend(i8* %69)
store i8* %70, i8** %7
%71 = load i8*, i8** %10
%72 = call i32 @free(i8* %71)
%73 = load i32, i32* %8
%74 = sext i32 %73 to i64
%75 = load i8*, i8** %4
%76 = load i8*, i8** %7
%77 = ptrtoint i8* %75 to i64
%78 = ptrtoint i8* %76 to i64
%79 = sub i64 %77, %78
%80 = mul i64 1, %79
%81 = add i64 %74, %80
%82 = trunc i64 %81 to i32
store i32 %82, i32* %3
br label %87
83: ; preds = %34
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%85 = load i8*, i8** %10
%86 = call i32 @free(i8* %85)
store i32 0, i32* %3
br label %87
87: ; preds = %83, %65
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@.str = [87 x i8] c"---------------------------------------\0A%s\0A------------------------------------------\0A\00"
@.str.1 = [15 x i8] c"Content-Length\00"
@str = [23 x i8] c"Length is not provided\00"
@str.4 = [27 x i8] c"Content length is provided\00"
define i32 @parsehead(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = add nsw i32 %1, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %2
%11 = sext i32 %1 to i64
%12 = getelementptr inbounds i8, i8* %6, i64 %11
store i8 0, i8* %12
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([87 x i8], [87 x i8]* @.str, i64 0, i64 0), i8* %6)
%14 = tail call i8* @strstr(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %38, label %16
16: ; preds = %10
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.4, i64 0, i64 0))
br label %18
18: ; preds = %18, %16
%19 = phi i8* [ %14, %16 ], [ %23, %18 ]
%20 = load i8, i8* %19
%21 = tail call i32 @isdigit(i8 signext %20)
%22 = icmp eq i32 %21, 0
%23 = getelementptr inbounds i8, i8* %19, i64 1
br i1 %22, label %18, label %24
24: ; preds = %18, %24
%25 = phi i8* [ %26, %24 ], [ %19, %18 ]
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %25
%28 = icmp eq i8 %27, 10
br i1 %28, label %29, label %24
29: ; preds = %24
store i8* %26, i8** %3
%30 = call i64 @strtol(i8* %19, i8** %3, i32 10)
%31 = tail call i8* @findheadend(i8* %6)
%32 = tail call i32 @free(i8* %6)
%33 = ptrtoint i8* %0 to i64
%34 = ptrtoint i8* %31 to i64
%35 = add i64 %30, %33
%36 = sub i64 %35, %34
%37 = trunc i64 %36 to i32
br label %41
38: ; preds = %10
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%40 = tail call i32 @free(i8* %6)
br label %41
41: ; preds = %38, %29
%42 = phi i32 [ %37, %29 ], [ 0, %38 ]
ret i32 %42
}