#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 O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *, ...);
int strlen(char *);
int write(int, char *, int);
void logger(int type, char *s1, char *s2, int socket_fd) {
  int fd, tmp;
  char logbuffer[8096 * 2];
  switch (type) {
  case 42:
    (void)sprintf(logbuffer, "ERROR: %s:%s Errno=%d exiting pid=%d", s1, s2,
                  errno, getpid());
    break;
  case 403:
    tmp = write(socket_fd,
                "HTTP/1.1 403 Forbidden\nContent-Length: 185\nConnection: "
                "close\nContent-Type: text/html\n\n<html><head>\n<title>403 "
                "Forbidden</title>\n</head><body>\n<h1>Forbidden</h1>\nThe "
                "requested URL, file type or operation is not allowed on this "
                "simple static file webserver.\n</body></html>\n",
                271);
    (void)sprintf(logbuffer, "FORBIDDEN: %s:%s", s1, s2);
    break;
  case 404:
    tmp = write(socket_fd,
                "HTTP/1.1 404 Not Found\nContent-Length: 136\nConnection: "
                "close\nContent-Type: text/html\n\n<html><head>\n<title>404 "
                "Not Found</title>\n</head><body>\n<h1>Not Found</h1>\nThe "
                "requested URL was not found on this server.\n</body></html>\n",
                224);
    (void)sprintf(logbuffer, "NOT FOUND: %s:%s", s1, s2);
    break;
  case 44:
    (void)sprintf(logbuffer, " INFO: %s:%s:%d", s1, s2, socket_fd);
    break;
  }
  if ((fd = open("nweb.log", O_CREAT | O_WRONLY | O_APPEND, 0644)) >= 0) {
    tmp = write(fd, logbuffer, strlen(logbuffer));
    tmp = write(fd, "\n", 1);
    (void)close(fd);
  }
  if (type == 42 || type == 404 || type == 403)
    exit(3);
}


x86 assembly (clang, -O0):
<logger>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3fa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x3f74(%rbp)
sub    $0x2a,%eax
je     11e0 <logger+0x70>
jmp    11a0 <logger+0x30>
mov    -0x3f74(%rbp),%eax
sub    $0x2c,%eax
je     12be <logger+0x14e>
jmp    11b4 <logger+0x44>
mov    -0x3f74(%rbp),%eax
sub    $0x193,%eax
je     124c <logger+0xdc>
jmp    11ca <logger+0x5a>
mov    -0x3f74(%rbp),%eax
sub    $0x194,%eax
je     1285 <logger+0x115>
jmp    12df <logger+0x16f>
lea    -0x3f70(%rbp),%rax
mov    %rax,-0x3f90(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x3f88(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x3f80(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3f78(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3f90(%rbp),%rdi
mov    -0x3f88(%rbp),%rdx
mov    -0x3f80(%rbp),%rcx
mov    -0x3f78(%rbp),%r8d
mov    %eax,%r9d
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    12df <logger+0x16f>
mov    -0x1c(%rbp),%edi
lea    0xdcf(%rip),%rsi
mov    $0x10f,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
lea    -0x3f70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xebc(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    12df <logger+0x16f>
mov    -0x1c(%rbp),%edi
lea    0xeb7(%rip),%rsi
mov    $0xe0,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
lea    -0x3f70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xf75(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    12df <logger+0x16f>
lea    -0x3f70(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    0xf60(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%esi
mov    0x2cd9(%rip),%rax
or     (%rax),%esi
mov    0x2cc8(%rip),%rax
or     (%rax),%esi
lea    0xf47(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jl     1370 <logger+0x200>
mov    -0x20(%rbp),%eax
mov    %eax,-0x3f9c(%rbp)
lea    -0x3f70(%rbp),%rax
mov    %rax,-0x3f98(%rbp)
lea    -0x3f70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3f9c(%rbp),%edi
mov    -0x3f98(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
lea    0xeca(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x2a,-0x4(%rbp)
je     1394 <logger+0x224>
cmpl   $0x194,-0x4(%rbp)
je     1394 <logger+0x224>
cmpl   $0x193,-0x4(%rbp)
jne    139e <logger+0x22e>
mov    $0x3,%edi
call   1090 <exit@plt>
add    $0x3fa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logger>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3f48,%rsp
mov    %ecx,%r8d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
cmp    $0x192,%edi
jg     11ba <logger+0x4a>
cmp    $0x2a,%ebx
je     11e7 <logger+0x77>
cmp    $0x2c,%ebx
jne    1241 <logger+0xd1>
lea    0x1093(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    1241 <logger+0xd1>
cmp    $0x193,%ebx
je     1216 <logger+0xa6>
cmp    $0x194,%ebx
jne    1241 <logger+0xd1>
lea    0xf75(%rip),%rsi
mov    %r8d,%edi
mov    $0xe0,%edx
call   1030 <write@plt>
lea    0x1042(%rip),%rsi
jmp    1231 <logger+0xc1>
mov    0x2dea(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe02(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %ebp,%r8d
mov    %eax,%r9d
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    1241 <logger+0xd1>
lea    0xe08(%rip),%rsi
mov    %r8d,%edi
mov    $0x10f,%edx
call   1030 <write@plt>
lea    0xf04(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    0x2db0(%rip),%rax
mov    0x2d79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d66(%rip),%rax
or     (%rax),%esi
lea    0xfe5(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     12a4 <logger+0x134>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xf94(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    -0x193(%rbx),%eax
cmp    $0x2,%eax
jb     12c2 <logger+0x152>
cmp    $0x2a,%ebx
je     12c2 <logger+0x152>
add    $0x3f48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"ERROR: %s:%s Errno=%d exiting pid=%d\00"
@errno = global i32 0
@.str.1 = [272 x i8] c"HTTP/1.1 403 Forbidden\0AContent-Length: 185\0AConnection: close\0AContent-Type: text/html\0A\0A<html><head>\0A<title>403 Forbidden</title>\0A</head><body>\0A<h1>Forbidden</h1>\0AThe requested URL, file type or operation is not allowed on this simple static file webserver.\0A</body></html>\0A\00"
@.str.2 = [17 x i8] c"FORBIDDEN: %s:%s\00"
@.str.3 = [225 x i8] c"HTTP/1.1 404 Not Found\0AContent-Length: 136\0AConnection: close\0AContent-Type: text/html\0A\0A<html><head>\0A<title>404 Not Found</title>\0A</head><body>\0A<h1>Not Found</h1>\0AThe requested URL was not found on this server.\0A</body></html>\0A\00"
@.str.4 = [17 x i8] c"NOT FOUND: %s:%s\00"
@.str.5 = [16 x i8] c" INFO: %s:%s:%d\00"
@.str.6 = [9 x i8] c"nweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.7 = [2 x i8] c"\0A\00"
define void @logger(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [16192 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = load i32, i32* %5
switch i32 %12, label %40 [
i32 42, label %13
i32 403, label %20
i32 404, label %27
i32 44, label %34
]
13: ; preds = %4
%14 = getelementptr inbounds [16192 x i8], [16192 x i8]* %11, i64 0, i64 0
%15 = load i8*, i8** %6
%16 = load i8*, i8** %7
%17 = load i32, i32* @errno
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16, i32 %17, i32 %18)
br label %40
20: ; preds = %4
%21 = load i32, i32* %8
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([272 x i8], [272 x i8]* @.str.1, i64 0, i64 0), i32 271)
store i32 %22, i32* %10
%23 = getelementptr inbounds [16192 x i8], [16192 x i8]* %11, i64 0, i64 0
%24 = load i8*, i8** %6
%25 = load i8*, i8** %7
%26 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %24, i8* %25)
br label %40
27: ; preds = %4
%28 = load i32, i32* %8
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([225 x i8], [225 x i8]* @.str.3, i64 0, i64 0), i32 224)
store i32 %29, i32* %10
%30 = getelementptr inbounds [16192 x i8], [16192 x i8]* %11, i64 0, i64 0
%31 = load i8*, i8** %6
%32 = load i8*, i8** %7
%33 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %31, i8* %32)
br label %40
34: ; preds = %4
%35 = getelementptr inbounds [16192 x i8], [16192 x i8]* %11, i64 0, i64 0
%36 = load i8*, i8** %6
%37 = load i8*, i8** %7
%38 = load i32, i32* %8
%39 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %36, i8* %37, i32 %38)
br label %40
40: ; preds = %4, %34, %27, %20, %13
%41 = load i32, i32* @O_CREAT
%42 = load i32, i32* @O_WRONLY
%43 = or i32 %41, %42
%44 = load i32, i32* @O_APPEND
%45 = or i32 %43, %44
%46 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %45, i32 420)
store i32 %46, i32* %9
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %58
48: ; preds = %40
%49 = load i32, i32* %9
%50 = getelementptr inbounds [16192 x i8], [16192 x i8]* %11, i64 0, i64 0
%51 = getelementptr inbounds [16192 x i8], [16192 x i8]* %11, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @write(i32 %49, i8* %50, i32 %52)
store i32 %53, i32* %10
%54 = load i32, i32* %9
%55 = call i32 @write(i32 %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
store i32 %55, i32* %10
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %48, %40
%59 = load i32, i32* %5
%60 = icmp eq i32 %59, 42
br i1 %60, label %67, label %61
61: ; preds = %58
%62 = load i32, i32* %5
%63 = icmp eq i32 %62, 404
br i1 %63, label %67, label %64
64: ; preds = %61
%65 = load i32, i32* %5
%66 = icmp eq i32 %65, 403
br i1 %66, label %67, label %69
67: ; preds = %64, %61, %58
%68 = call i32 @exit(i32 3)
unreachable
69: ; preds = %64
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"ERROR: %s:%s Errno=%d exiting pid=%d\00"
@errno = global i32 0
@.str.1 = [272 x i8] c"HTTP/1.1 403 Forbidden\0AContent-Length: 185\0AConnection: close\0AContent-Type: text/html\0A\0A<html><head>\0A<title>403 Forbidden</title>\0A</head><body>\0A<h1>Forbidden</h1>\0AThe requested URL, file type or operation is not allowed on this simple static file webserver.\0A</body></html>\0A\00"
@.str.2 = [17 x i8] c"FORBIDDEN: %s:%s\00"
@.str.3 = [225 x i8] c"HTTP/1.1 404 Not Found\0AContent-Length: 136\0AConnection: close\0AContent-Type: text/html\0A\0A<html><head>\0A<title>404 Not Found</title>\0A</head><body>\0A<h1>Not Found</h1>\0AThe requested URL was not found on this server.\0A</body></html>\0A\00"
@.str.4 = [17 x i8] c"NOT FOUND: %s:%s\00"
@.str.5 = [16 x i8] c" INFO: %s:%s:%d\00"
@.str.6 = [9 x i8] c"nweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.7 = [2 x i8] c"\0A\00"
define void @logger(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [16192 x i8]
%6 = getelementptr inbounds [16192 x i8], [16192 x i8]* %5, i64 0, i64 0
switch i32 %0, label %19 [
i32 42, label %7
i32 403, label %11
i32 404, label %14
i32 44, label %17
]
7: ; preds = %4
%8 = load i32, i32* @errno
%9 = tail call i32 (...) @getpid()
%10 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %2, i32 %8, i32 %9)
br label %19
11: ; preds = %4
%12 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([272 x i8], [272 x i8]* @.str.1, i64 0, i64 0), i32 271)
%13 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2)
br label %19
14: ; preds = %4
%15 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([225 x i8], [225 x i8]* @.str.3, i64 0, i64 0), i32 224)
%16 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %2)
br label %19
17: ; preds = %4
%18 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %1, i8* %2, i32 %3)
br label %19
19: ; preds = %4, %17, %14, %11, %7
%20 = load i32, i32* @O_CREAT
%21 = load i32, i32* @O_WRONLY
%22 = or i32 %21, %20
%23 = load i32, i32* @O_APPEND
%24 = or i32 %22, %23
%25 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i32 %24, i32 420)
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %19
%28 = call i32 @strlen(i8* %6)
%29 = call i32 @write(i32 %25, i8* %6, i32 %28)
%30 = call i32 @write(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0), i32 1)
%31 = call i32 @close(i32 %25)
br label %32
32: ; preds = %27, %19
switch i32 %0, label %35 [
i32 404, label %33
i32 403, label %33
i32 42, label %33
]
33: ; preds = %32, %32, %32
%34 = call i32 @exit(i32 3)
unreachable
35: ; preds = %32
ret void
}