#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 /*<<< orphan*/ write(int, char *, int);
void logger(int type, char *s1, char *s2, int socket_fd) {
  int fd;
  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:
    (void)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:
    (void)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;
  }
  if ((fd = open("nweb.log", O_CREAT | O_WRONLY | O_APPEND, 0644)) >= 0) {
    (void)write(fd, logbuffer, strlen(logbuffer));
    (void)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    $0x3f90,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x3f64(%rbp)
sub    $0x2a,%eax
je     11cc <logger+0x5c>
jmp    11a0 <logger+0x30>
mov    -0x3f64(%rbp),%eax
sub    $0x193,%eax
je     1238 <logger+0xc8>
jmp    11b6 <logger+0x46>
mov    -0x3f64(%rbp),%eax
sub    $0x194,%eax
je     126e <logger+0xfe>
jmp    129f <logger+0x12f>
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3f80(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x3f78(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x3f70(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3f68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3f80(%rbp),%rdi
mov    -0x3f78(%rbp),%rdx
mov    -0x3f70(%rbp),%rcx
mov    -0x3f68(%rbp),%r8d
mov    %eax,%r9d
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    129f <logger+0x12f>
mov    -0x1c(%rbp),%edi
lea    0xde3(%rip),%rsi
mov    $0x10f,%edx
call   1030 <write@plt>
lea    -0x3f60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xed3(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    129f <logger+0x12f>
mov    -0x1c(%rbp),%edi
lea    0xece(%rip),%rsi
mov    $0xe0,%edx
call   1030 <write@plt>
lea    -0x3f60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xf8f(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    0x2d19(%rip),%rax
or     (%rax),%esi
mov    0x2d08(%rip),%rax
or     (%rax),%esi
lea    0xf77(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jl     132a <logger+0x1ba>
mov    -0x20(%rbp),%eax
mov    %eax,-0x3f8c(%rbp)
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3f88(%rbp)
lea    -0x3f60(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3f8c(%rbp),%edi
mov    -0x3f88(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
lea    0xf0d(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x2a,-0x4(%rbp)
je     134e <logger+0x1de>
cmpl   $0x194,-0x4(%rbp)
je     134e <logger+0x1de>
cmpl   $0x193,-0x4(%rbp)
jne    1358 <logger+0x1e8>
mov    $0x3,%edi
call   1090 <exit@plt>
add    $0x3f90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logger>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3f48,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
cmp    $0x194,%edi
je     11c9 <logger+0x59>
cmp    $0x193,%ebx
je     11e5 <logger+0x75>
cmp    $0x2a,%ebx
jne    120f <logger+0x9f>
mov    0x2e37(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe4f(%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    120f <logger+0x9f>
lea    0xf76(%rip),%rsi
mov    %ecx,%edi
mov    $0xe0,%edx
call   1030 <write@plt>
lea    0x1044(%rip),%rsi
jmp    11ff <logger+0x8f>
lea    0xe39(%rip),%rsi
mov    %ecx,%edi
mov    $0x10f,%edx
call   1030 <write@plt>
lea    0xf36(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    0x2de2(%rip),%rax
mov    0x2dab(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d98(%rip),%rax
or     (%rax),%esi
lea    0x1007(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1272 <logger+0x102>
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    0xfc6(%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     1290 <logger+0x120>
cmp    $0x2a,%ebx
je     1290 <logger+0x120>
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 = [9 x i8] c"nweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.6 = [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 [16192 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i32, i32* %5
switch i32 %11, label %33 [
i32 42, label %12
i32 403, label %19
i32 404, label %26
]
12: ; preds = %4
%13 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%14 = load i8*, i8** %6
%15 = load i8*, i8** %7
%16 = load i32, i32* @errno
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15, i32 %16, i32 %17)
br label %33
19: ; preds = %4
%20 = load i32, i32* %8
%21 = call i32 @write(i32 %20, i8* getelementptr inbounds ([272 x i8], [272 x i8]* @.str.1, i64 0, i64 0), i32 271)
%22 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%23 = load i8*, i8** %6
%24 = load i8*, i8** %7
%25 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %23, i8* %24)
br label %33
26: ; preds = %4
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([225 x i8], [225 x i8]* @.str.3, i64 0, i64 0), i32 224)
%29 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%30 = load i8*, i8** %6
%31 = load i8*, i8** %7
%32 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %30, i8* %31)
br label %33
33: ; preds = %4, %26, %19, %12
%34 = load i32, i32* @O_CREAT
%35 = load i32, i32* @O_WRONLY
%36 = or i32 %34, %35
%37 = load i32, i32* @O_APPEND
%38 = or i32 %36, %37
%39 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %38, i32 420)
store i32 %39, i32* %9
%40 = icmp sge i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %33
%42 = load i32, i32* %9
%43 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%44 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%45 = call i32 @strlen(i8* %44)
%46 = call i32 @write(i32 %42, i8* %43, i32 %45)
%47 = load i32, i32* %9
%48 = call i32 @write(i32 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %41, %33
%52 = load i32, i32* %5
%53 = icmp eq i32 %52, 42
br i1 %53, label %60, label %54
54: ; preds = %51
%55 = load i32, i32* %5
%56 = icmp eq i32 %55, 404
br i1 %56, label %60, label %57
57: ; preds = %54
%58 = load i32, i32* %5
%59 = icmp eq i32 %58, 403
br i1 %59, label %60, label %62
60: ; preds = %57, %54, %51
%61 = call i32 @exit(i32 3)
unreachable
62: ; preds = %57
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 = [9 x i8] c"nweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.6 = [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 %17 [
i32 42, label %7
i32 403, label %11
i32 404, label %14
]
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 %17
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 %17
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 %17
17: ; preds = %4, %14, %11, %7
%18 = load i32, i32* @O_CREAT
%19 = load i32, i32* @O_WRONLY
%20 = or i32 %19, %18
%21 = load i32, i32* @O_APPEND
%22 = or i32 %20, %21
%23 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 420)
%24 = icmp sgt i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = call i32 @strlen(i8* %6)
%27 = call i32 @write(i32 %23, i8* %6, i32 %26)
%28 = call i32 @write(i32 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32 1)
%29 = call i32 @close(i32 %23)
br label %30
30: ; preds = %25, %17
switch i32 %0, label %33 [
i32 404, label %31
i32 403, label %31
i32 42, label %31
]
31: ; preds = %30, %30, %30
%32 = call i32 @exit(i32 3)
unreachable
33: ; preds = %30
ret void
}