#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 nweb_log(int type, char *s1, char *s2, int num) {
  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 43:
    (void)sprintf(
        logbuffer,
        "<HTML><BODY><H1>nweb Web Server Sorry: %s %s</H1></BODY></HTML>\r\n",
        s1, s2);
    (void)write(num, logbuffer, strlen(logbuffer));
    (void)sprintf(logbuffer, "SORRY: %s:%s", s1, s2);
    break;
  case 44:
    (void)sprintf(logbuffer, " INFO: %s:%s:%d", s1, s2, num);
    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 == 43)
    exit(3);
}


x86 assembly (clang, -O0):
<nweb_log>:
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,-0x3f64(%rbp)
sub    $0x2a,%eax
je     11c8 <nweb_log+0x58>
jmp    11a0 <nweb_log+0x30>
mov    -0x3f64(%rbp),%eax
sub    $0x2b,%eax
je     1234 <nweb_log+0xc4>
jmp    11b4 <nweb_log+0x44>
mov    -0x3f64(%rbp),%eax
sub    $0x2c,%eax
je     12aa <nweb_log+0x13a>
jmp    12cb <nweb_log+0x15b>
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    0x2de5(%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    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    12cb <nweb_log+0x15b>
lea    -0x3f60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x1c(%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>
lea    -0x3f60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
jmp    12cb <nweb_log+0x15b>
lea    -0x3f60(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x1c(%rbp),%r8d
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
mov    0x2ced(%rip),%rax
or     (%rax),%esi
mov    0x2cdc(%rip),%rax
or     (%rax),%esi
lea    0xd97(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jl     1356 <nweb_log+0x1e6>
mov    -0x20(%rbp),%eax
mov    %eax,-0x3f9c(%rbp)
lea    -0x3f60(%rbp),%rax
mov    %rax,-0x3f98(%rbp)
lea    -0x3f60(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3f9c(%rbp),%edi
mov    -0x3f98(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
lea    0xd21(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x2a,-0x4(%rbp)
je     136a <nweb_log+0x1fa>
cmpl   $0x2b,-0x4(%rbp)
jne    1374 <nweb_log+0x204>
mov    $0x3,%edi
call   1090 <exit@plt>
add    $0x3fa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nweb_log>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x3f40,%rsp
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
cmp    $0x2c,%edi
je     1216 <nweb_log+0xa6>
cmp    $0x2b,%ebx
je     11cf <nweb_log+0x5f>
cmp    $0x2a,%ebx
jne    1230 <nweb_log+0xc0>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe49(%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    1230 <nweb_log+0xc0>
lea    0xe4f(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe63(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <sprintf@plt>
jmp    1230 <nweb_log+0xc0>
lea    0xe57(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %ebp,%r8d
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    0x2dc1(%rip),%rax
mov    0x2d8a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d77(%rip),%rax
or     (%rax),%esi
lea    0xe32(%rip),%rdi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1293 <nweb_log+0x123>
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    0xde5(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
and    $0xfffffffe,%ebx
cmp    $0x2a,%ebx
je     12ab <nweb_log+0x13b>
add    $0x3f40,%rsp
pop    %rbx
pop    %r12
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 = [66 x i8] c"<HTML><BODY><H1>nweb Web Server Sorry: %s %s</H1></BODY></HTML>\0D\0A\00"
@.str.2 = [13 x i8] c"SORRY: %s:%s\00"
@.str.3 = [16 x i8] c" INFO: %s:%s:%d\00"
@.str.4 = [9 x i8] c"nweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
define void @nweb_log(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 %39 [
i32 42, label %12
i32 43, label %19
i32 44, label %33
]
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 %39
19: ; preds = %4
%20 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%21 = load i8*, i8** %6
%22 = load i8*, i8** %7
%23 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %22)
%24 = load i32, i32* %8
%25 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%26 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @write(i32 %24, i8* %25, i32 %27)
%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 ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %30, i8* %31)
br label %39
33: ; preds = %4
%34 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%35 = load i8*, i8** %6
%36 = load i8*, i8** %7
%37 = load i32, i32* %8
%38 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %34, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %35, i8* %36, i32 %37)
br label %39
39: ; preds = %4, %33, %19, %12
%40 = load i32, i32* @O_CREAT
%41 = load i32, i32* @O_WRONLY
%42 = or i32 %40, %41
%43 = load i32, i32* @O_APPEND
%44 = or i32 %42, %43
%45 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %44, i32 420)
store i32 %45, i32* %9
%46 = icmp sge i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %39
%48 = load i32, i32* %9
%49 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%50 = getelementptr inbounds [16192 x i8], [16192 x i8]* %10, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @write(i32 %48, i8* %49, i32 %51)
%53 = load i32, i32* %9
%54 = call i32 @write(i32 %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%55 = load i32, i32* %9
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %47, %39
%58 = load i32, i32* %5
%59 = icmp eq i32 %58, 42
br i1 %59, label %63, label %60
60: ; preds = %57
%61 = load i32, i32* %5
%62 = icmp eq i32 %61, 43
br i1 %62, label %63, label %65
63: ; preds = %60, %57
%64 = call i32 @exit(i32 3)
unreachable
65: ; preds = %60
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 = [66 x i8] c"<HTML><BODY><H1>nweb Web Server Sorry: %s %s</H1></BODY></HTML>\0D\0A\00"
@.str.2 = [13 x i8] c"SORRY: %s:%s\00"
@.str.3 = [16 x i8] c" INFO: %s:%s:%d\00"
@.str.4 = [9 x i8] c"nweb.log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.5 = [2 x i8] c"\0A\00"
define void @nweb_log(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 %18 [
i32 42, label %7
i32 43, label %11
i32 44, label %16
]
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 %18
11: ; preds = %4
%12 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %2)
%13 = call i32 @strlen(i8* %6)
%14 = call i32 @write(i32 %3, i8* %6, i32 %13)
%15 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2)
br label %18
16: ; preds = %4
%17 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %2, i32 %3)
br label %18
18: ; preds = %4, %16, %11, %7
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_WRONLY
%21 = or i32 %20, %19
%22 = load i32, i32* @O_APPEND
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %23, i32 420)
%25 = icmp sgt i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %18
%27 = call i32 @strlen(i8* %6)
%28 = call i32 @write(i32 %24, i8* %6, i32 %27)
%29 = call i32 @write(i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 1)
%30 = call i32 @close(i32 %24)
br label %31
31: ; preds = %26, %18
%32 = and i32 %0, -2
%33 = icmp eq i32 %32, 42
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @exit(i32 3)
unreachable
36: ; preds = %31
ret void
}