#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 */
struct lws {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int lws_add_http_header_by_name(struct lws *wsi, const unsigned char *name,
                                const unsigned char *value, int length,
                                unsigned char **p, unsigned char *end) {
  (void)wsi;
  if (name) {
    while (*p < end && *name)
      *((*p)++) = *name++;
    if (*p == end)
      return 1;
    *((*p)++) = ' ';
  }
  if (*p + length + 3 >= end)
    return 1;
  memcpy(*p, value, length);
  *p += length;
  *((*p)++) = '\x0d';
  *((*p)++) = '\x0a';
  return 0;
}


x86 assembly (clang, -O0):
<lws_add_http_header_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cf <lws_add_http_header_by_name+0xbf>
jmp    113f <lws_add_http_header_by_name+0x2f>
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
xor    %eax,%eax
cmp    -0x38(%rbp),%rcx
mov    %al,-0x39(%rbp)
jae    1165 <lws_add_http_header_by_name+0x55>
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1175 <lws_add_http_header_by_name+0x65>
jmp    119e <lws_add_http_header_by_name+0x8e>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%cl
mov    -0x30(%rbp),%rdx
mov    (%rdx),%rax
mov    %rax,%rsi
add    $0x1,%rsi
mov    %rsi,(%rdx)
mov    %cl,(%rax)
jmp    113f <lws_add_http_header_by_name+0x2f>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
cmp    -0x38(%rbp),%rax
jne    11bb <lws_add_http_header_by_name+0xab>
movl   $0x1,-0x4(%rbp)
jmp    124c <lws_add_http_header_by_name+0x13c>
mov    -0x30(%rbp),%rcx
mov    (%rcx),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,(%rcx)
movb   $0x20,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0x3,%rax
cmp    -0x38(%rbp),%rax
jb     11f7 <lws_add_http_header_by_name+0xe7>
movl   $0x1,-0x4(%rbp)
jmp    124c <lws_add_http_header_by_name+0x13c>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    (%rcx),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,(%rcx)
movb   $0xd,(%rax)
mov    -0x30(%rbp),%rcx
mov    (%rcx),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,(%rcx)
movb   $0xa,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lws_add_http_header_by_name>:
push   %r15
push   %r14
push   %rbx
mov    %r8,%r15
test   %rsi,%rsi
je     114d <lws_add_http_header_by_name+0x3d>
nopl   (%rax)
mov    (%r15),%rax
cmp    %r9,%rax
jae    113e <lws_add_http_header_by_name+0x2e>
movzbl (%rsi),%ebx
test   %bl,%bl
je     113e <lws_add_http_header_by_name+0x2e>
add    $0x1,%rsi
lea    0x1(%rax),%rdi
mov    %rdi,(%r15)
mov    %bl,(%rax)
jmp    1120 <lws_add_http_header_by_name+0x10>
cmp    %r9,%rax
je     1194 <lws_add_http_header_by_name+0x84>
lea    0x1(%rax),%rsi
mov    %rsi,(%r15)
movb   $0x20,(%rax)
mov    (%r15),%rdi
movslq %ecx,%r14
lea    (%rdi,%r14,1),%rcx
add    $0x3,%rcx
mov    $0x1,%eax
cmp    %r9,%rcx
jae    1199 <lws_add_http_header_by_name+0x89>
mov    %rdx,%rsi
mov    %r14d,%edx
call   1030 <memcpy@plt>
mov    (%r15),%rax
lea    (%rax,%r14,1),%rcx
add    $0x1,%rcx
mov    %rcx,(%r15)
movb   $0xd,(%rax,%r14,1)
mov    (%r15),%rax
lea    0x1(%rax),%rcx
mov    %rcx,(%r15)
movb   $0xa,(%rax)
xor    %eax,%eax
jmp    1199 <lws_add_http_header_by_name+0x89>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.lws = type { i32 }
define i32 @lws_add_http_header_by_name(%struct.lws* %0, i8* %1, i8* %2, i32 %3, i8** %4, i8* %5) {
%7 = alloca i32
%8 = alloca %struct.lws*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8**
%13 = alloca i8*
store %struct.lws* %0, %struct.lws** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i8** %4, i8*** %12
store i8* %5, i8** %13
%14 = load %struct.lws*, %struct.lws** %8
%15 = load i8*, i8** %9
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %47
17: ; preds = %6
br label %18
18: ; preds = %30, %17
%19 = load i8**, i8*** %12
%20 = load i8*, i8** %19
%21 = load i8*, i8** %13
%22 = icmp ult i8* %20, %21
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i8*, i8** %9
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br label %28
28: ; preds = %23, %18
%29 = phi i1 [ false, %18 ], [ %27, %23 ]
br i1 %29, label %30, label %37
30: ; preds = %28
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %9
%33 = load i8, i8* %31
%34 = load i8**, i8*** %12
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %34
store i8 %33, i8* %35
br label %18
37: ; preds = %28
%38 = load i8**, i8*** %12
%39 = load i8*, i8** %38
%40 = load i8*, i8** %13
%41 = icmp eq i8* %39, %40
br i1 %41, label %42, label %43
42: ; preds = %37
store i32 1, i32* %7
br label %74
43: ; preds = %37
%44 = load i8**, i8*** %12
%45 = load i8*, i8** %44
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %44
store i8 32, i8* %45
br label %47
47: ; preds = %43, %6
%48 = load i8**, i8*** %12
%49 = load i8*, i8** %48
%50 = load i32, i32* %11
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = getelementptr inbounds i8, i8* %52, i64 3
%54 = load i8*, i8** %13
%55 = icmp uge i8* %53, %54
br i1 %55, label %56, label %57
56: ; preds = %47
store i32 1, i32* %7
br label %74
57: ; preds = %47
%58 = load i8**, i8*** %12
%59 = load i8*, i8** %58
%60 = load i8*, i8** %10
%61 = load i32, i32* %11
%62 = call i32 @memcpy(i8* %59, i8* %60, i32 %61)
%63 = load i32, i32* %11
%64 = load i8**, i8*** %12
%65 = load i8*, i8** %64
%66 = sext i32 %63 to i64
%67 = getelementptr inbounds i8, i8* %65, i64 %66
store i8* %67, i8** %64
%68 = load i8**, i8*** %12
%69 = load i8*, i8** %68
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %68
store i8 13, i8* %69
%71 = load i8**, i8*** %12
%72 = load i8*, i8** %71
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %71
store i8 10, i8* %72
store i32 0, i32* %7
br label %74
74: ; preds = %57, %56, %42
%75 = load i32, i32* %7
ret i32 %75
}

LLVM IR (-O2):
%struct.lws = type { i32 }
define i32 @lws_add_http_header_by_name(%struct.lws* nocapture readnone %0, i8* %1, i8* %2, i32 %3, i8** nocapture %4, i8* readnone %5) {
%7 = icmp eq i8* %1, null
br i1 %7, label %26, label %8
8: ; preds = %6
%9 = load i8*, i8** %4
%10 = icmp ult i8* %9, %5
br i1 %10, label %11, label %21
11: ; preds = %8, %16
%12 = phi i8* [ %19, %16 ], [ %9, %8 ]
%13 = phi i8* [ %17, %16 ], [ %1, %8 ]
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = getelementptr inbounds i8, i8* %13, i64 1
%18 = getelementptr inbounds i8, i8* %12, i64 1
store i8* %18, i8** %4
store i8 %14, i8* %12
%19 = load i8*, i8** %4
%20 = icmp ult i8* %19, %5
br i1 %20, label %11, label %21
21: ; preds = %11, %16, %8
%22 = phi i8* [ %9, %8 ], [ %19, %16 ], [ %12, %11 ]
%23 = icmp eq i8* %22, %5
br i1 %23, label %39, label %24
24: ; preds = %21
%25 = getelementptr inbounds i8, i8* %22, i64 1
store i8* %25, i8** %4
store i8 32, i8* %22
br label %26
26: ; preds = %24, %6
%27 = load i8*, i8** %4
%28 = sext i32 %3 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = getelementptr inbounds i8, i8* %29, i64 3
%31 = icmp ult i8* %30, %5
br i1 %31, label %32, label %39
32: ; preds = %26
%33 = tail call i32 @memcpy(i8* %27, i8* %2, i32 %3)
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 %28
%36 = getelementptr inbounds i8, i8* %35, i64 1
store i8* %36, i8** %4
store i8 13, i8* %35
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8* %38, i8** %4
store i8 10, i8* %37
br label %39
39: ; preds = %26, %21, %32
%40 = phi i32 [ 0, %32 ], [ 1, %21 ], [ 1, %26 ]
ret i32 %40
}