#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *, char *,
                           int);
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int, char *, char *, ...);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void log_message(int type, char *label, char *message, int socket_fd) {
  int fd;
  char logbuffer[8096 * 2];
  switch (type) {
  case 42:
    (void)sprintf(logbuffer, "[ERROR] %d %s:%s Errno=%d exiting pid=%d",
                  getpid(), label, message, errno, getpid());
    (void)fprintf(stderr, "[ERROR] %d %s:%s Errno=%d\n", getpid(), label,
                  message, errno);
    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] %d %s:%s", getpid(), label, message);
    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] %d %s:%s", getpid(), label, message);
    break;
  case 44:
    (void)sprintf(logbuffer, "[INFO] %d %s:%s:%d", getpid(), label, message,
                  socket_fd);
    break;
  }
  if ((fd = open("nanoweb.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):
<log_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3fc0,%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     11f0 <log_message+0x70>
jmp    11b0 <log_message+0x30>
mov    -0x3f64(%rbp),%eax
sub    $0x2c,%eax
je     1345 <log_message+0x1c5>
jmp    11c4 <log_message+0x44>
mov    -0x3f64(%rbp),%eax
sub    $0x193,%eax
je     12ab <log_message+0x12b>
jmp    11da <log_message+0x5a>
mov    -0x3f64(%rbp),%eax
sub    $0x194,%eax
je     12f8 <log_message+0x178>
jmp    137d <log_message+0x1fd>
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3f90(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x3f84(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x3f80(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x3f78(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3f6c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3f90(%rbp),%rdi
mov    -0x3f84(%rbp),%edx
mov    -0x3f80(%rbp),%rcx
mov    -0x3f78(%rbp),%r8
mov    -0x3f6c(%rbp),%r9d
lea    0xda0(%rip),%rsi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3f68(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3f68(%rbp),%edi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    0x2d39(%rip),%rax
mov    (%rax),%r9d
lea    0xd88(%rip),%rsi
call   1070 <fprintf@plt>
jmp    137d <log_message+0x1fd>
mov    -0x1c(%rbp),%edi
lea    0xd8f(%rip),%rsi
mov    $0x10f,%edx
call   1030 <write@plt>
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3f98(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3f98(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
jmp    137d <log_message+0x1fd>
mov    -0x1c(%rbp),%edi
lea    0xe67(%rip),%rsi
mov    $0xe0,%edx
call   1030 <write@plt>
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3fa0(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3fa0(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xf11(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
jmp    137d <log_message+0x1fd>
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3fa8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x3fa8(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x1c(%rbp),%r9d
lea    0xee9(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%esi
mov    0x2c33(%rip),%rax
or     (%rax),%esi
mov    0x2c22(%rip),%rax
or     (%rax),%esi
lea    0xed3(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jl     1408 <log_message+0x288>
mov    -0x20(%rbp),%eax
mov    %eax,-0x3fb4(%rbp)
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3fb0(%rbp)
lea    -0x3f60(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3fb4(%rbp),%edi
mov    -0x3fb0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
lea    0xe52(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x2a,-0x4(%rbp)
je     142c <log_message+0x2ac>
cmpl   $0x194,-0x4(%rbp)
je     142c <log_message+0x2ac>
cmpl   $0x193,-0x4(%rbp)
jne    1436 <log_message+0x2b6>
mov    $0x3,%edi
call   10a0 <exit@plt>
add    $0x3fc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<log_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f58,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r13d
cmp    $0x192,%edi
jg     11de <log_message+0x5e>
cmp    $0x2a,%r13d
je     121e <log_message+0x9e>
cmp    $0x2c,%r13d
jne    12b9 <log_message+0x139>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0x109d(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %eax,%edx
mov    %r15,%rcx
mov    %r14,%r8
mov    %ebp,%r9d
xor    %eax,%eax
call   1090 <sprintf@plt>
jmp    12b9 <log_message+0x139>
cmp    $0x193,%r13d
je     1284 <log_message+0x104>
cmp    $0x194,%r13d
jne    12b9 <log_message+0x139>
lea    0xf6a(%rip),%rsi
mov    %ebp,%edi
mov    $0xe0,%edx
call   1030 <write@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0x1031(%rip),%rsi
jmp    12a5 <log_message+0x125>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    0x2da2(%rip),%rbx
mov    (%rbx),%r12d
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,(%rsp)
lea    0xdbe(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebp,%edx
mov    %r15,%rcx
mov    %r14,%r8
mov    %r12d,%r9d
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    (%rbx),%r9d
lea    0xdb6(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <fprintf@plt>
jmp    12b9 <log_message+0x139>
lea    0xdb9(%rip),%rsi
mov    %ebp,%edi
mov    $0x10f,%edx
call   1030 <write@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeaf(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %eax,%edx
mov    %r15,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    0x2d38(%rip),%rax
mov    0x2cf9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ce6(%rip),%rax
or     (%rax),%esi
lea    0xf97(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     131e <log_message+0x19e>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xf3d(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    -0x193(%r13),%eax
cmp    $0x2,%eax
jb     1342 <log_message+0x1c2>
cmp    $0x2a,%r13d
je     1342 <log_message+0x1c2>
add    $0x3f58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"[ERROR] %d %s:%s Errno=%d exiting pid=%d\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"[ERROR] %d %s:%s Errno=%d\0A\00"
@.str.2 = [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.3 = [21 x i8] c"[FORBIDDEN] %d %s:%s\00"
@.str.4 = [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.5 = [21 x i8] c"[NOT FOUND] %d %s:%s\00"
@.str.6 = [19 x i8] c"[INFO] %d %s:%s:%d\00"
@.str.7 = [12 x i8] c"nanoweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.8 = [2 x i8] c"\0A\00"
define void @log_message(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 %49 [
i32 42, label %12
i32 403, label %26
i32 404, label %34
i32 44, label %42
]
12: ; preds = %4
%13 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = load i8*, i8** %6
%16 = load i8*, i8** %7
%17 = load i32, i32* @errno
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %13, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %14, i8* %15, i8* %16, i32 %17, i32 %18)
%20 = load i32, i32* @stderr
%21 = call i32 (...) @getpid()
%22 = load i8*, i8** %6
%23 = load i8*, i8** %7
%24 = load i32, i32* @errno
%25 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %22, i8* %23, i32 %24)
br label %49
26: ; preds = %4
%27 = load i32, i32* %8
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([272 x i8], [272 x i8]* @.str.2, i64 0, i64 0), i32 271)
%29 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%30 = call i32 (...) @getpid()
%31 = load i8*, i8** %6
%32 = load i8*, i8** %7
%33 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %30, i8* %31, i8* %32)
br label %49
34: ; preds = %4
%35 = load i32, i32* %8
%36 = call i32 @write(i32 %35, i8* getelementptr inbounds ([225 x i8], [225 x i8]* @.str.4, i64 0, i64 0), i32 224)
%37 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%38 = call i32 (...) @getpid()
%39 = load i8*, i8** %6
%40 = load i8*, i8** %7
%41 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %38, i8* %39, i8* %40)
br label %49
42: ; preds = %4
%43 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%44 = call i32 (...) @getpid()
%45 = load i8*, i8** %6
%46 = load i8*, i8** %7
%47 = load i32, i32* %8
%48 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %43, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %44, i8* %45, i8* %46, i32 %47)
br label %49
49: ; preds = %4, %42, %34, %26, %12
%50 = load i32, i32* @O_CREAT
%51 = load i32, i32* @O_WRONLY
%52 = or i32 %50, %51
%53 = load i32, i32* @O_APPEND
%54 = or i32 %52, %53
%55 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %54, i32 420)
store i32 %55, i32* %9
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %67
57: ; preds = %49
%58 = load i32, i32* %9
%59 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%60 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%61 = call i32 @strlen(i8* %60)
%62 = call i32 @write(i32 %58, i8* %59, i32 %61)
%63 = load i32, i32* %9
%64 = call i32 @write(i32 %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%65 = load i32, i32* %9
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %57, %49
%68 = load i32, i32* %5
%69 = icmp eq i32 %68, 42
br i1 %69, label %76, label %70
70: ; preds = %67
%71 = load i32, i32* %5
%72 = icmp eq i32 %71, 404
br i1 %72, label %76, label %73
73: ; preds = %70
%74 = load i32, i32* %5
%75 = icmp eq i32 %74, 403
br i1 %75, label %76, label %78
76: ; preds = %73, %70, %67
%77 = call i32 @exit(i32 3)
unreachable
78: ; preds = %73
ret void
}

LLVM IR (-O2):
@.str = [41 x i8] c"[ERROR] %d %s:%s Errno=%d exiting pid=%d\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"[ERROR] %d %s:%s Errno=%d\0A\00"
@.str.2 = [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.3 = [21 x i8] c"[FORBIDDEN] %d %s:%s\00"
@.str.4 = [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.5 = [21 x i8] c"[NOT FOUND] %d %s:%s\00"
@.str.6 = [19 x i8] c"[INFO] %d %s:%s:%d\00"
@.str.7 = [12 x i8] c"nanoweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.8 = [2 x i8] c"\0A\00"
define void @log_message(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 %27 [
i32 42, label %7
i32 403, label %16
i32 404, label %20
i32 44, label %24
]
7: ; preds = %4
%8 = tail call i32 (...) @getpid()
%9 = load i32, i32* @errno
%10 = tail call i32 (...) @getpid()
%11 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %8, i8* %1, i8* %2, i32 %9, i32 %10)
%12 = load i32, i32* @stderr
%13 = tail call i32 (...) @getpid()
%14 = load i32, i32* @errno
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %1, i8* %2, i32 %14)
br label %27
16: ; preds = %4
%17 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([272 x i8], [272 x i8]* @.str.2, i64 0, i64 0), i32 271)
%18 = tail call i32 (...) @getpid()
%19 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %18, i8* %1, i8* %2)
br label %27
20: ; preds = %4
%21 = tail call i32 @write(i32 %3, i8* getelementptr inbounds ([225 x i8], [225 x i8]* @.str.4, i64 0, i64 0), i32 224)
%22 = tail call i32 (...) @getpid()
%23 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %22, i8* %1, i8* %2)
br label %27
24: ; preds = %4
%25 = tail call i32 (...) @getpid()
%26 = call i32 (i8*, i8*, i32, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %25, i8* %1, i8* %2, i32 %3)
br label %27
27: ; preds = %4, %24, %20, %16, %7
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_WRONLY
%30 = or i32 %29, %28
%31 = load i32, i32* @O_APPEND
%32 = or i32 %30, %31
%33 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %32, i32 420)
%34 = icmp sgt i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %27
%36 = call i32 @strlen(i8* %6)
%37 = call i32 @write(i32 %33, i8* %6, i32 %36)
%38 = call i32 @write(i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0), i32 1)
%39 = call i32 @close(i32 %33)
br label %40
40: ; preds = %35, %27
switch i32 %0, label %43 [
i32 404, label %41
i32 403, label %41
i32 42, label %41
]
41: ; preds = %40, %40, %40
%42 = call i32 @exit(i32 3)
unreachable
43: ; preds = %40
ret void
}