#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fdprintf(int, char *, ...);
int /*<<< orphan*/ fstat(int, struct stat *);
scalar_t__ getenv(char *);
void http_err(int, int, char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ http_serve_none(int, char *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ sendfile(int, int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, scalar_t__);
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *strrchr(char const *, char);
void http_serve_file(int fd, const char *pn) {
  int filefd;
  off_t len = 0;
  if (getenv("PATH_INFO")) {
    char buf[1024];
    snprintf(buf, 1024, "%s%s", pn, getenv("PATH_INFO"));
    http_serve_none(fd, buf);
    return;
  }
  if ((filefd = open(pn, O_RDONLY)) < 0)
    return http_err(fd, 500, "open %s: %s", pn, strerror(errno));
  const char *ext = strrchr(pn, '.');
  const char *mimetype = "text/html";
  if (ext && !strcmp(ext, ".css"))
    mimetype = "text/css";
  if (ext && !strcmp(ext, ".jpg"))
    mimetype = "image/jpeg";
  fdprintf(fd, "HTTP/1.0 200 OK\r\n");
  fdprintf(fd, "Content-Type: %s\r\n", mimetype);
  fdprintf(fd, "\r\n");
  struct stat st;
  if (!fstat(filefd, &st))
    len = st.st_size;
  if (sendfile(fd, filefd, 0, len) < 0)
    err(1, "sendfile");
  close(filefd);
}


x86 assembly (clang, -O0):
<http_serve_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xe10(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     125a <http_serve_file+0x8a>
lea    -0x420(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    0xde1(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x448(%rbp),%rdi
mov    -0x440(%rbp),%rcx
mov    %rax,%r8
mov    $0x400,%esi
lea    0xdc9(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
call   10b0 <http_serve_none@plt>
jmp    13e1 <http_serve_file+0x211>
mov    -0x10(%rbp),%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12c0 <http_serve_file+0xf0>
mov    -0x4(%rbp),%eax
mov    %eax,-0x454(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rcx
mov    %eax,%r8d
mov    $0x1f4,%esi
lea    0xd59(%rip),%rdx
call   10d0 <http_err@plt>
jmp    13e1 <http_serve_file+0x211>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x428(%rbp)
lea    0xd3f(%rip),%rax
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     131b <http_serve_file+0x14b>
mov    -0x428(%rbp),%rdi
lea    0xd26(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    131b <http_serve_file+0x14b>
lea    0xd16(%rip),%rax
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     1353 <http_serve_file+0x183>
mov    -0x428(%rbp),%rdi
lea    0xcfc(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    1353 <http_serve_file+0x183>
lea    0xcec(%rip),%rax
mov    %rax,-0x430(%rbp)
mov    -0x4(%rbp),%edi
lea    0xce6(%rip),%rsi
mov    $0x0,%al
call   1040 <fdprintf@plt>
mov    -0x4(%rbp),%edi
mov    -0x430(%rbp),%rdx
lea    0xce0(%rip),%rsi
mov    $0x0,%al
call   1040 <fdprintf@plt>
mov    -0x4(%rbp),%edi
lea    0xccc(%rip),%rsi
mov    $0x0,%al
call   1040 <fdprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x438(%rbp),%rsi
call   10f0 <fstat@plt>
cmp    $0x0,%eax
jne    13ae <http_serve_file+0x1de>
mov    -0x438(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%ecx
xor    %edx,%edx
call   10a0 <sendfile@plt>
cmp    $0x0,%rax
jge    13d9 <http_serve_file+0x209>
mov    $0x1,%edi
lea    0xc94(%rip),%rsi
call   1090 <err@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_serve_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbp
mov    %edi,%r15d
lea    0xe16(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1221 <http_serve_file+0x51>
lea    0xe0f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rbp,%rcx
mov    %rax,%r8
call   1050 <snprintf@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   10b0 <http_serve_none@plt>
jmp    1343 <http_serve_file+0x173>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     128f <http_serve_file+0xbf>
mov    %eax,%r14d
mov    %rbp,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     12c4 <http_serve_file+0xf4>
mov    %rax,%rbp
lea    0xdd0(%rip),%rsi
mov    %rax,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
lea    0xdc4(%rip),%rax
lea    0xdae(%rip),%rbx
cmove  %rax,%rbx
lea    0xdbb(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
lea    0xdaf(%rip),%rbp
cmovne %rbx,%rbp
jmp    12cb <http_serve_file+0xfb>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd6b(%rip),%rdx
mov    %r15d,%edi
mov    $0x1f4,%esi
mov    %rbp,%rcx
mov    %eax,%r8d
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <http_err@plt>
lea    0xd50(%rip),%rbp
lea    0xd71(%rip),%rsi
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <fdprintf@plt>
lea    0xd72(%rip),%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <fdprintf@plt>
lea    0xd5b(%rip),%rsi
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <fdprintf@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
call   10f0 <fstat@plt>
mov    $0x0,%ecx
test   %eax,%eax
jne    1318 <http_serve_file+0x148>
mov    (%rsp),%ecx
mov    %r15d,%edi
mov    %r14d,%esi
xor    %edx,%edx
call   10a0 <sendfile@plt>
test   %rax,%rax
jns    133b <http_serve_file+0x16b>
lea    0xd37(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c"PATH_INFO\00"
@.str.1 = [5 x i8] c"%s%s\00"
@O_RDONLY = global i32 0
@.str.2 = [12 x i8] c"open %s: %s\00"
@errno = global i32 0
@.str.3 = [10 x i8] c"text/html\00"
@.str.4 = [5 x i8] c".css\00"
@.str.5 = [9 x i8] c"text/css\00"
@.str.6 = [5 x i8] c".jpg\00"
@.str.7 = [11 x i8] c"image/jpeg\00"
@.str.8 = [18 x i8] c"HTTP/1.0 200 OK\0D\0A\00"
@.str.9 = [19 x i8] c"Content-Type: %s\0D\0A\00"
@.str.10 = [3 x i8] c"\0D\0A\00"
@.str.11 = [9 x i8] c"sendfile\00"
define void @http_serve_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca %struct.stat
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %6
%11 = call i64 @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = call i64 @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @snprintf(i8* %14, i32 1024, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %15, i64 %16)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%20 = call i32 @http_serve_none(i32 %18, i8* %19)
br label %74
21: ; preds = %2
%22 = load i8*, i8** %4
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %5
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i32, i32* %3
%28 = load i8*, i8** %4
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
br label %74
31: ; preds = %21
%32 = load i8*, i8** %4
%33 = call i8* @strrchr(i8* %32, i8 signext 46)
store i8* %33, i8** %8
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8** %9
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i8*, i8** %8
%38 = call i32 @strcmp(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %36
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8** %9
br label %41
41: ; preds = %40, %36, %31
%42 = load i8*, i8** %8
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %49
44: ; preds = %41
%45 = load i8*, i8** %8
%46 = call i32 @strcmp(i8* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %44
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8** %9
br label %49
49: ; preds = %48, %44, %41
%50 = load i32, i32* %3
%51 = call i32 (i32, i8*, ...) @fdprintf(i32 %50, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%52 = load i32, i32* %3
%53 = load i8*, i8** %9
%54 = call i32 (i32, i8*, ...) @fdprintf(i32 %52, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %3
%56 = call i32 (i32, i8*, ...) @fdprintf(i32 %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0))
%57 = load i32, i32* %5
%58 = call i32 @fstat(i32 %57, %struct.stat* %10)
%59 = icmp ne i32 %58, 0
br i1 %59, label %63, label %60
60: ; preds = %49
%61 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%62 = load i32, i32* %61
store i32 %62, i32* %6
br label %63
63: ; preds = %60, %49
%64 = load i32, i32* %3
%65 = load i32, i32* %5
%66 = load i32, i32* %6
%67 = call i64 @sendfile(i32 %64, i32 %65, i32 0, i32 %66)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %63
%70 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
br label %71
71: ; preds = %69, %63
%72 = load i32, i32* %5
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %71, %26, %13
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"PATH_INFO\00"
@.str.1 = [5 x i8] c"%s%s\00"
@O_RDONLY = global i32 0
@.str.2 = [12 x i8] c"open %s: %s\00"
@errno = global i32 0
@.str.3 = [10 x i8] c"text/html\00"
@.str.4 = [5 x i8] c".css\00"
@.str.5 = [9 x i8] c"text/css\00"
@.str.6 = [5 x i8] c".jpg\00"
@.str.7 = [11 x i8] c"image/jpeg\00"
@.str.8 = [18 x i8] c"HTTP/1.0 200 OK\0D\0A\00"
@.str.9 = [19 x i8] c"Content-Type: %s\0D\0A\00"
@.str.10 = [3 x i8] c"\0D\0A\00"
@.str.11 = [9 x i8] c"sendfile\00"
define void @http_serve_file(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = tail call i64 @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = call i32 @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %1, i64 %5)
%10 = call i32 @http_serve_none(i32 %0, i8* %8)
br label %45
11: ; preds = %2
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %1, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
tail call void @http_err(i32 %0, i32 500, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %17)
br label %45
18: ; preds = %11
%19 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 46)
%20 = icmp eq i8* %19, null
br i1 %20, label %28, label %21
21: ; preds = %18
%22 = tail call i32 @strcmp(i8* %19, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32 %22, 0
%24 = select i1 %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0)
%25 = tail call i32 @strcmp(i8* %19, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%26 = icmp eq i32 %25, 0
%27 = select i1 %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %24
br label %28
28: ; preds = %18, %21
%29 = phi i8* [ %27, %21 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %18 ]
%30 = tail call i32 (i32, i8*, ...) @fdprintf(i32 %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%31 = tail call i32 (i32, i8*, ...) @fdprintf(i32 %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i8* %29)
%32 = tail call i32 (i32, i8*, ...) @fdprintf(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0))
%33 = bitcast %struct.stat* %4 to i8*
%34 = call i32 @fstat(i32 %13, %struct.stat* %4)
%35 = icmp eq i32 %34, 0
%36 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%37 = load i32, i32* %36
%38 = select i1 %35, i32 %37, i32 0
%39 = tail call i64 @sendfile(i32 %0, i32 %13, i32 0, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %28
%42 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
br label %43
43: ; preds = %41, %28
%44 = tail call i32 @close(i32 %13)
br label %45
45: ; preds = %43, %15, %7
ret void
}