#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 */
short htons(int);
int /*<<< orphan*/ memcpy(unsigned char *, short *, int);
int /*<<< orphan*/ write(int, unsigned char *, int);
void process_request(unsigned char *pkg, int *plen, void *sig, int fd) {
  unsigned char *pkg_end = pkg + *plen, *p = pkg;
  *plen = 0;
  while (p != pkg_end) {
    if (*(p++) == '$') {
      if (*p == 1) {
        short tmp = htons(555);
        memcpy(pkg, &tmp, sizeof tmp);
        tmp = htons(((555 + 10000) + 1));
        memcpy(pkg + 2, &tmp, sizeof tmp);
        *plen = 2 * (2 * sizeof tmp);
      } else if (*p == 0) {
        *(char *)sig = 2;
        write(fd, p - 1, 1);
        p++;
      } else if (*p == 2) {
        write(fd, p + 3, 1);
        p += 4;
      } else if (*p == 3) {
        *(p + 2) = ' ';
        write(fd, p + 2, 3);
        p += 5;
      }
    }
  }
}


x86 assembly (clang, -O0):
<process_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq (%rcx),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     12bc <process_request+0x18c>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movzbl (%rax),%eax
cmp    $0x24,%eax
jne    12b7 <process_request+0x187>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x1,%eax
jne    11f7 <process_request+0xc7>
mov    $0x22b,%edi
call   1040 <htons@plt>
mov    %ax,-0x32(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x32(%rbp),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    $0x293c,%edi
call   1040 <htons@plt>
mov    %ax,-0x32(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x2,%rdi
lea    -0x32(%rbp),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rax
movl   $0x8,(%rax)
jmp    12b2 <process_request+0x182>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    1234 <process_request+0x104>
mov    -0x18(%rbp),%rax
movb   $0x2,(%rax)
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
add    $0xffffffffffffffff,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    12ad <process_request+0x17d>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x2,%eax
jne    126a <process_request+0x13a>
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
add    $0x3,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
jmp    12a8 <process_request+0x178>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x3,%eax
jne    12a3 <process_request+0x173>
mov    -0x30(%rbp),%rax
movb   $0x20,0x2(%rax)
mov    -0x1c(%rbp),%edi
mov    -0x30(%rbp),%rsi
add    $0x2,%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x30(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x30(%rbp)
jmp    12a8 <process_request+0x178>
jmp    12ad <process_request+0x17d>
jmp    12b2 <process_request+0x182>
jmp    12b7 <process_request+0x187>
jmp    116b <process_request+0x3b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %rdi,0x8(%rsp)
movslq (%rsi),%r13
mov    %rsi,0x18(%rsp)
movl   $0x0,(%rsi)
test   %r13,%r13
je     1253 <process_request+0x123>
mov    %ecx,%ebx
mov    0x8(%rsp),%r12
add    %r12,%r13
lea    0x2(%r12),%rax
mov    %rax,0x10(%rsp)
lea    0xe86(%rip),%r14
jmp    11a6 <process_request+0x76>
lea    0x3(%r12),%rsi
movb   $0x20,0x3(%r12)
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
add    $0x6,%r12
mov    %r12,%r15
mov    %r15,%r12
cmp    %r13,%r15
je     1253 <process_request+0x123>
lea    0x1(%r12),%r15
cmpb   $0x24,(%r12)
jne    119a <process_request+0x6a>
movzbl (%r15),%eax
cmp    $0x3,%rax
ja     119a <process_request+0x6a>
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
mov    0x20(%rsp),%rax
movb   $0x2,(%rax)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x2,%r12
jmp    1197 <process_request+0x67>
mov    $0x22b,%edi
call   1040 <htons@plt>
mov    %ax,0x6(%rsp)
mov    0x8(%rsp),%rdi
lea    0x6(%rsp),%rbp
mov    %rbp,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    $0x293c,%edi
call   1040 <htons@plt>
mov    %ax,0x6(%rsp)
mov    0x10(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    0x18(%rsp),%rax
movl   $0x8,(%rax)
jmp    119a <process_request+0x6a>
lea    0x4(%r12),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0x5,%r12
jmp    1197 <process_request+0x67>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @process_request(i8* %0, i32* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i16
store i8* %0, i8** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = load i32*, i32** %6
%14 = load i32, i32* %13
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %12, i64 %15
store i8* %16, i8** %9
%17 = load i8*, i8** %5
store i8* %17, i8** %10
%18 = load i32*, i32** %6
store i32 0, i32* %18
br label %19
19: ; preds = %86, %4
%20 = load i8*, i8** %10
%21 = load i8*, i8** %9
%22 = icmp ne i8* %20, %21
br i1 %22, label %23, label %87
23: ; preds = %19
%24 = load i8*, i8** %10
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %10
%26 = load i8, i8* %24
%27 = zext i8 %26 to i32
%28 = icmp eq i32 %27, 36
br i1 %28, label %29, label %86
29: ; preds = %23
%30 = load i8*, i8** %10
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %43
34: ; preds = %29
%35 = call signext i16 @htons(i32 555)
store i16 %35, i16* %11
%36 = load i8*, i8** %5
%37 = call i32 @memcpy(i8* %36, i16* %11, i32 2)
%38 = call signext i16 @htons(i32 10556)
store i16 %38, i16* %11
%39 = load i8*, i8** %5
%40 = getelementptr inbounds i8, i8* %39, i64 2
%41 = call i32 @memcpy(i8* %40, i16* %11, i32 2)
%42 = load i32*, i32** %6
store i32 8, i32* %42
br label %85
43: ; preds = %29
%44 = load i8*, i8** %10
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %43
%49 = load i8*, i8** %7
store i8 2, i8* %49
%50 = load i32, i32* %8
%51 = load i8*, i8** %10
%52 = getelementptr inbounds i8, i8* %51, i64 -1
%53 = call i32 @write(i32 %50, i8* %52, i32 1)
%54 = load i8*, i8** %10
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %10
br label %84
56: ; preds = %43
%57 = load i8*, i8** %10
%58 = load i8, i8* %57
%59 = zext i8 %58 to i32
%60 = icmp eq i32 %59, 2
br i1 %60, label %61, label %68
61: ; preds = %56
%62 = load i32, i32* %8
%63 = load i8*, i8** %10
%64 = getelementptr inbounds i8, i8* %63, i64 3
%65 = call i32 @write(i32 %62, i8* %64, i32 1)
%66 = load i8*, i8** %10
%67 = getelementptr inbounds i8, i8* %66, i64 4
store i8* %67, i8** %10
br label %83
68: ; preds = %56
%69 = load i8*, i8** %10
%70 = load i8, i8* %69
%71 = zext i8 %70 to i32
%72 = icmp eq i32 %71, 3
br i1 %72, label %73, label %82
73: ; preds = %68
%74 = load i8*, i8** %10
%75 = getelementptr inbounds i8, i8* %74, i64 2
store i8 32, i8* %75
%76 = load i32, i32* %8
%77 = load i8*, i8** %10
%78 = getelementptr inbounds i8, i8* %77, i64 2
%79 = call i32 @write(i32 %76, i8* %78, i32 3)
%80 = load i8*, i8** %10
%81 = getelementptr inbounds i8, i8* %80, i64 5
store i8* %81, i8** %10
br label %82
82: ; preds = %73, %68
br label %83
83: ; preds = %82, %61
br label %84
84: ; preds = %83, %48
br label %85
85: ; preds = %84, %34
br label %86
86: ; preds = %85, %23
br label %19
87: ; preds = %19
ret void
}

LLVM IR (-O2):
define void @process_request(i8* %0, i32* nocapture %1, i8* nocapture writeonly %2, i32 %3) {
%5 = alloca i16
%6 = load i32, i32* %1
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
store i32 0, i32* %1
%9 = icmp eq i32 %6, 0
br i1 %9, label %39, label %10
10: ; preds = %4
%11 = bitcast i16* %5 to i8*
%12 = getelementptr inbounds i8, i8* %0, i64 2
br label %13
13: ; preds = %10, %36
%14 = phi i8* [ %0, %10 ], [ %37, %36 ]
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %14
%17 = icmp eq i8 %16, 36
br i1 %17, label %18, label %36
18: ; preds = %13
%19 = load i8, i8* %15
switch i8 %19, label %36 [
i8 1, label %20
i8 0, label %25
i8 2, label %28
i8 3, label %32
]
20: ; preds = %18
%21 = call signext i16 @htons(i32 555)
store i16 %21, i16* %5
%22 = call i32 @memcpy(i8* %0, i16* %5, i32 2)
%23 = call signext i16 @htons(i32 10556)
store i16 %23, i16* %5
%24 = call i32 @memcpy(i8* %12, i16* %5, i32 2)
store i32 8, i32* %1
br label %36
25: ; preds = %18
store i8 2, i8* %2
%26 = call i32 @write(i32 %3, i8* %14, i32 1)
%27 = getelementptr inbounds i8, i8* %14, i64 2
br label %36
28: ; preds = %18
%29 = getelementptr inbounds i8, i8* %14, i64 4
%30 = call i32 @write(i32 %3, i8* %29, i32 1)
%31 = getelementptr inbounds i8, i8* %14, i64 5
br label %36
32: ; preds = %18
%33 = getelementptr inbounds i8, i8* %14, i64 3
store i8 32, i8* %33
%34 = call i32 @write(i32 %3, i8* %33, i32 3)
%35 = getelementptr inbounds i8, i8* %14, i64 6
br label %36
36: ; preds = %18, %20, %28, %32, %25, %13
%37 = phi i8* [ %15, %20 ], [ %27, %25 ], [ %31, %28 ], [ %35, %32 ], [ %15, %13 ], [ %15, %18 ]
%38 = icmp eq i8* %37, %8
br i1 %38, label %39, label %13
39: ; preds = %36, %4
ret void
}