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

/* Variables and functions */
scalar_t__ http_read_line(int, char *, int);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ touch(char *);
char toupper(char);
int /*<<< orphan*/ url_decode(char *, char *, int);
const char *http_request_headers(int fd) {
  static char buf[8192];
  int i;
  char value[512];
  char envvar[512];
  touch("http_request_headers");
  for (;;) {
    if (http_read_line(fd, buf, sizeof(buf)) < 0)
      return "Socket IO error";
    if (buf[0] == '\0')
      break;
    char *sp = strchr(buf, ' ');
    if (!sp)
      return "Header parse error (1)";
    *sp = '\0';
    sp++;
    if (strlen(buf) == 0)
      return "Header parse error (2)";
    char *colon = &buf[strlen(buf) - 1];
    if (*colon != ':')
      return "Header parse error (3)";
    *colon = '\0';
    for (i = 0; i < strlen(buf); i++) {
      buf[i] = toupper(buf[i]);
      if (buf[i] == '-')
        buf[i] = '_';
    }
    url_decode(value, sp, 512);
    if (strcmp(buf, "CONTENT_TYPE") != 0 &&
        strcmp(buf, "CONTENT_LENGTH") != 0) {
      sprintf(envvar, "HTTP_%s", buf);
      setenv(envvar, value, 1);
    } else {
      setenv(buf, value, 1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<http_request_headers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0xc(%rbp)
lea    0xe5b(%rip),%rdi
call   1030 <touch@plt>
mov    -0xc(%rbp),%edi
lea    0x2ecc(%rip),%rsi
mov    $0x2000,%edx
call   10a0 <http_read_line@plt>
cmp    $0x0,%rax
jge    11d8 <http_request_headers+0x48>
lea    0xe46(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13ec <http_request_headers+0x25c>
movsbl 0x2ea1(%rip),%eax
cmp    $0x0,%eax
jne    11ed <http_request_headers+0x5d>
jmp    13e4 <http_request_headers+0x254>
lea    0x2e8c(%rip),%rdi
mov    $0x20,%esi
call   1070 <strchr@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    1223 <http_request_headers+0x93>
lea    0xe0b(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13ec <http_request_headers+0x25c>
mov    -0x418(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x418(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x418(%rbp)
lea    0x2e3a(%rip),%rdi
call   1060 <strlen@plt>
cmp    $0x0,%eax
jne    1264 <http_request_headers+0xd4>
lea    0xde1(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13ec <http_request_headers+0x25c>
lea    0x2e15(%rip),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
movslq %eax,%rcx
lea    0x2e03(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x420(%rbp)
mov    -0x420(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
je     12aa <http_request_headers+0x11a>
lea    0xdb2(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    13ec <http_request_headers+0x25c>
mov    -0x420(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x424(%rbp)
lea    0x2db5(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x424(%rbp),%eax
cmp    %ecx,%eax
jge    133e <http_request_headers+0x1ae>
movslq -0x10(%rbp),%rcx
lea    0x2d95(%rip),%rax
movsbl (%rax,%rcx,1),%edi
call   1040 <toupper@plt>
mov    %al,%dl
movslq -0x10(%rbp),%rcx
lea    0x2d7f(%rip),%rax
mov    %dl,(%rax,%rcx,1)
movslq -0x10(%rbp),%rcx
lea    0x2d71(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0x2d,%eax
jne    132b <http_request_headers+0x19b>
movslq -0x10(%rbp),%rcx
lea    0x2d59(%rip),%rax
movb   $0x5f,(%rax,%rcx,1)
jmp    1330 <http_request_headers+0x1a0>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12bb <http_request_headers+0x12b>
lea    -0x210(%rbp),%rdi
mov    -0x418(%rbp),%rsi
mov    $0x200,%edx
call   1080 <url_decode@plt>
lea    0x2d23(%rip),%rdi
lea    0xd06(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     13c7 <http_request_headers+0x237>
lea    0x2d06(%rip),%rdi
lea    0xcf6(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     13c7 <http_request_headers+0x237>
lea    -0x410(%rbp),%rdi
lea    0xce8(%rip),%rsi
lea    0x2cdb(%rip),%rdx
call   10b0 <sprintf@plt>
lea    -0x410(%rbp),%rdi
lea    -0x210(%rbp),%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
jmp    13df <http_request_headers+0x24f>
lea    -0x210(%rbp),%rsi
lea    0x2cab(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
jmp    11aa <http_request_headers+0x1a>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_request_headers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r15d
lea    0xe55(%rip),%rdi
call   1030 <touch@plt>
lea    0x2ec9(%rip),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   10a0 <http_read_line@plt>
lea    0xe47(%rip),%rcx
test   %rax,%rax
js     1336 <http_request_headers+0x1a6>
mov    $0x5f,%r14d
lea    0x200(%rsp),%rbp
jmp    1219 <http_request_headers+0x89>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   10a0 <http_read_line@plt>
test   %rax,%rax
js     1314 <http_request_headers+0x184>
cmpb   $0x0,0x2e60(%rip)
je     1310 <http_request_headers+0x180>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     131d <http_request_headers+0x18d>
mov    %rax,%r13
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
je     1326 <http_request_headers+0x196>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x3a,-0x1(%rax,%rbx,1)
jne    132f <http_request_headers+0x19f>
add    $0x1,%r13
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    12aa <http_request_headers+0x11a>
xor    %r12d,%r12d
nop
movsbl (%r12,%rbx,1),%edi
call   1040 <toupper@plt>
cmp    $0x2d,%al
movzbl %al,%eax
cmove  %r14d,%eax
mov    %al,(%r12,%rbx,1)
add    $0x1,%r12
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmp    %rax,%r12
jl     1280 <http_request_headers+0xf0>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x200,%edx
call   1080 <url_decode@plt>
mov    %rbx,%rdi
lea    0xda6(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     11f0 <http_request_headers+0x60>
mov    %rbx,%rdi
lea    0xd9b(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     11f0 <http_request_headers+0x60>
mov    %rbp,%r12
mov    %rsp,%rbp
mov    %rbp,%rdi
lea    0xd8c(%rip),%rsi
mov    %rbx,%rdx
call   10b0 <sprintf@plt>
mov    %rbp,%rdi
mov    %r12,%rbp
mov    %r12,%rsi
jmp    11f6 <http_request_headers+0x66>
xor    %ecx,%ecx
jmp    1336 <http_request_headers+0x1a6>
lea    0xcfa(%rip),%rcx
jmp    1336 <http_request_headers+0x1a6>
lea    0xd01(%rip),%rcx
jmp    1336 <http_request_headers+0x1a6>
lea    0xd0f(%rip),%rcx
jmp    1336 <http_request_headers+0x1a6>
lea    0xd1d(%rip),%rcx
mov    %rcx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@http_request_headers.buf = internal global [8192 x i8] zeroinitializer
@.str = [21 x i8] c"http_request_headers\00"
@.str.1 = [16 x i8] c"Socket IO error\00"
@.str.2 = [23 x i8] c"Header parse error (1)\00"
@.str.3 = [23 x i8] c"Header parse error (2)\00"
@.str.4 = [23 x i8] c"Header parse error (3)\00"
@.str.5 = [13 x i8] c"CONTENT_TYPE\00"
@.str.6 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.7 = [8 x i8] c"HTTP_%s\00"
define i8* @http_request_headers(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca [512 x i8]
%7 = alloca i8*
%8 = alloca i8*
store i32 %0, i32* %3
%9 = call i32 @touch(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %89, %1
%11 = load i32, i32* %3
%12 = call i64 @http_read_line(i32 %11, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i32 8192)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %91
15: ; preds = %10
%16 = load i8, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0)
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
br label %90
20: ; preds = %15
%21 = call i8* @strchr(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8 signext 32)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %20
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %91
25: ; preds = %20
%26 = load i8*, i8** %7
store i8 0, i8* %26
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %7
%29 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8** %2
br label %91
32: ; preds = %25
%33 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 %35
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 58
br i1 %40, label %41, label %42
41: ; preds = %32
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %2
br label %91
42: ; preds = %32
%43 = load i8*, i8** %8
store i8 0, i8* %43
store i32 0, i32* %4
br label %44
44: ; preds = %68, %42
%45 = load i32, i32* %4
%46 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %71
48: ; preds = %44
%49 = load i32, i32* %4
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = call signext i8 @toupper(i8 signext %52)
%54 = load i32, i32* %4
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 %55
store i8 %53, i8* %56
%57 = load i32, i32* %4
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 %58
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 45
br i1 %62, label %63, label %67
63: ; preds = %48
%64 = load i32, i32* %4
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 %65
store i8 95, i8* %66
br label %67
67: ; preds = %63, %48
br label %68
68: ; preds = %67
%69 = load i32, i32* %4
%70 = add nsw i32 %69, 1
store i32 %70, i32* %4
br label %44
71: ; preds = %44
%72 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%73 = load i8*, i8** %7
%74 = call i32 @url_decode(i8* %72, i8* %73, i32 512)
%75 = call i64 @strcmp(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %86
77: ; preds = %71
%78 = call i64 @strcmp(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %86
80: ; preds = %77
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%82 = call i32 @sprintf(i8* %81, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%83 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%84 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%85 = call i32 @setenv(i8* %83, i8* %84, i32 1)
br label %89
86: ; preds = %77, %71
%87 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%88 = call i32 @setenv(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8* %87, i32 1)
br label %89
89: ; preds = %86, %80
br label %10
90: ; preds = %19
store i8* null, i8** %2
br label %91
91: ; preds = %90, %41, %31, %24, %14
%92 = load i8*, i8** %2
ret i8* %92
}

LLVM IR (-O2):
@http_request_headers.buf = internal global [8192 x i8] zeroinitializer
@.str = [21 x i8] c"http_request_headers\00"
@.str.1 = [16 x i8] c"Socket IO error\00"
@.str.2 = [23 x i8] c"Header parse error (1)\00"
@.str.3 = [23 x i8] c"Header parse error (2)\00"
@.str.4 = [23 x i8] c"Header parse error (3)\00"
@.str.5 = [13 x i8] c"CONTENT_TYPE\00"
@.str.6 = [15 x i8] c"CONTENT_LENGTH\00"
@.str.7 = [8 x i8] c"HTTP_%s\00"
define i8* @http_request_headers(i32 %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @touch(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%7 = call i64 @http_read_line(i32 %0, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i32 8192)
%8 = icmp slt i64 %7, 0
br i1 %8, label %55, label %9
9: ; preds = %1, %52
%10 = load i8, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0)
%11 = icmp eq i8 %10, 0
br i1 %11, label %55, label %12
12: ; preds = %9
%13 = call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8 signext 32)
%14 = icmp eq i8* %13, null
br i1 %14, label %55, label %15
15: ; preds = %12
store i8 0, i8* %13
%16 = getelementptr inbounds i8, i8* %13, i64 1
%17 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%18 = icmp eq i32 %17, 0
br i1 %18, label %55, label %19
19: ; preds = %15
%20 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 58
br i1 %25, label %26, label %55
26: ; preds = %19
store i8 0, i8* %23
%27 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %26, %29
%30 = phi i64 [ %36, %29 ], [ 0, %26 ]
%31 = getelementptr inbounds [8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = call signext i8 @toupper(i8 signext %32)
%34 = icmp eq i8 %33, 45
%35 = select i1 %34, i8 95, i8 %33
store i8 %35, i8* %31
%36 = add nuw nsw i64 %30, 1
%37 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%38 = sext i32 %37 to i64
%39 = icmp slt i64 %36, %38
br i1 %39, label %29, label %40
40: ; preds = %29, %26
%41 = call i32 @url_decode(i8* %4, i8* %16, i32 512)
%42 = call i64 @strcmp(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %40
%45 = call i64 @strcmp(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0))
%49 = call i32 @setenv(i8* %5, i8* %4, i32 1)
br label %52
50: ; preds = %44, %40
%51 = call i32 @setenv(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i8* %4, i32 1)
br label %52
52: ; preds = %50, %47
%53 = call i64 @http_read_line(i32 %0, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @http_request_headers.buf, i64 0, i64 0), i32 8192)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %9
55: ; preds = %52, %9, %12, %15, %19, %1
%56 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), %19 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), %15 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %12 ], [ null, %9 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %52 ]
ret i8* %56
}