#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 addrinfo {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int close(int);
int /*<<< orphan*/ example_bind(int, struct addrinfo *);
struct addrinfo *example_getaddrinfo(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ example_listen(int, int);
int /*<<< orphan*/ example_setsockopt(int);
int example_socket(struct addrinfo *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(void) {
  char *port = "8080";
  struct addrinfo *socketaddrinfo = example_getaddrinfo(NULL, port);
  int sock = example_socket(socketaddrinfo);
  example_setsockopt(sock);
  example_bind(sock, socketaddrinfo);
  example_listen(sock, 128);
  freeaddrinfo(socketaddrinfo);
  printf("Press Ctrl+C to exit server.\n");
  printf("Run a web browser on the same computer as you run the server and "
         "point it to:\n");
  printf("http://127.0.0.1:%s\n", port);
  while (1) {
    int newsock = accept(sock, NULL, NULL);
    if (newsock == -1) {
      perror("accept(): ");
      continue;
    }
    char http_body[] = "hello world";
    char http_headers[1024];
    snprintf(http_headers, 1024,
             "HTTP/1.0 200 OK\r\n"
             "Content-Length: %zu\r\n"
             "Content-Type: text/html\r\n\r\n",
             strlen(http_body));
    if (send(newsock, http_headers, strlen(http_headers), 0) == -1) {
      perror("send");
      close(newsock);
      exit(EXIT_FAILURE);
    }
    if (send(newsock, http_body, strlen(http_body), 0) == -1) {
      perror("send");
      close(newsock);
      exit(EXIT_FAILURE);
    }
    if (close(newsock) == -1) {
      perror("close");
      exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe07(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   10f0 <example_getaddrinfo@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   10b0 <example_socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <example_setsockopt@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <example_bind@plt>
mov    -0x1c(%rbp),%edi
mov    $0x80,%esi
call   1030 <example_listen@plt>
mov    -0x18(%rbp),%rdi
call   1100 <freeaddrinfo@plt>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    129f <main+0xbf>
lea    0xdf2(%rip),%rdi
call   10c0 <perror@plt>
jmp    1272 <main+0x92>
mov    0xdec(%rip),%rax
mov    %rax,-0x2c(%rbp)
mov    0xdea(%rip),%eax
mov    %eax,-0x24(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x448(%rbp),%rdi
mov    %eax,%ecx
mov    $0x400,%esi
lea    0xdbf(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x430(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    1348 <main+0x168>
lea    0xdb3(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1090 <close@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x454(%rbp)
lea    -0x2c(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    13a6 <main+0x1c6>
lea    0xd55(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x20(%rbp),%edi
call   1090 <close@plt>
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1090 <close@plt>
cmp    $0xffffffff,%eax
jne    13d1 <main+0x1f1>
lea    0xd27(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c16(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
jmp    1272 <main+0x92>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xdf8(%rip),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1100 <example_getaddrinfo@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   10c0 <example_socket@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   10b0 <example_setsockopt@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1050 <example_bind@plt>
mov    %r12d,%edi
mov    $0x80,%esi
call   1030 <example_listen@plt>
mov    %rbp,%rdi
call   1110 <freeaddrinfo@plt>
lea    0xe30(%rip),%rdi
call   1040 <puts@plt>
lea    0xe41(%rip),%rdi
call   1040 <puts@plt>
lea    0xd9f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
movabs $0x6f77206f6c6c6568,%r14
20 77 6f
mov    %rsp,%r13
lea    0xdad(%rip),%r15
lea    0x10(%rsp),%rbp
jmp    12a0 <main+0xb0>
lea    0xd88(%rip),%rdi
call   10d0 <perror@plt>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
cmp    $0xffffffff,%eax
je     128b <main+0x9b>
mov    %eax,%ebx
mov    %r14,(%rsp)
movl   $0x646c72,0x8(%rsp)
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
mov    %eax,%ecx
call   1090 <snprintf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     1329 <main+0x139>
mov    %r13,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     1329 <main+0x139>
mov    %ebx,%edi
call   10a0 <close@plt>
cmp    $0xffffffff,%eax
jne    12a0 <main+0xb0>
lea    0xd56(%rip),%rdi
call   10d0 <perror@plt>
jmp    133c <main+0x14c>
lea    0xd43(%rip),%rdi
call   10d0 <perror@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32 }
@.str = [5 x i8] c"8080\00"
@.str.1 = [30 x i8] c"Press Ctrl+C to exit server.\0A\00"
@.str.2 = [79 x i8] c"Run a web browser on the same computer as you run the server and point it to:\0A\00"
@.str.3 = [21 x i8] c"http://127.0.0.1:%s\0A\00"
@.str.4 = [11 x i8] c"accept(): \00"
@__const.main.http_body = [12 x i8] c"hello world\00"
@.str.5 = [66 x i8] c"HTTP/1.0 200 OK\0D\0AContent-Length: %zu\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
@.str.6 = [5 x i8] c"send\00"
@EXIT_FAILURE = global i32 0
@.str.7 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [12 x i8]
%7 = alloca [1024 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %2
%8 = load i8*, i8** %2
%9 = call %struct.addrinfo* @example_getaddrinfo(i32* null, i8* %8)
store %struct.addrinfo* %9, %struct.addrinfo** %3
%10 = load %struct.addrinfo*, %struct.addrinfo** %3
%11 = call i32 @example_socket(%struct.addrinfo* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = call i32 @example_setsockopt(i32 %12)
%14 = load i32, i32* %4
%15 = load %struct.addrinfo*, %struct.addrinfo** %3
%16 = call i32 @example_bind(i32 %14, %struct.addrinfo* %15)
%17 = load i32, i32* %4
%18 = call i32 @example_listen(i32 %17, i32 128)
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
%20 = call i32 @freeaddrinfo(%struct.addrinfo* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.2, i64 0, i64 0))
%23 = load i8*, i8** %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %0, %30, %71
%26 = load i32, i32* %4
%27 = call i32 @accept(i32 %26, i32* null, i32* null)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %25
32: ; preds = %25
%33 = bitcast [12 x i8]* %6 to i8*
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @snprintf(i8* %34, i32 1024, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %5
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @send(i32 %38, i8* %39, i32 %41, i32 0)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %32
%45 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%46 = load i32, i32* %5
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %32
%51 = load i32, i32* %5
%52 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%53 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @send(i32 %51, i8* %52, i32 %54, i32 0)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %63
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%59 = load i32, i32* %5
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %50
%64 = load i32, i32* %5
%65 = call i32 @close(i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %71
67: ; preds = %63
%68 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %63
br label %25
}

LLVM IR (-O2):
%struct.addrinfo = type { i32 }
@.str = [5 x i8] c"8080\00"
@.str.3 = [21 x i8] c"http://127.0.0.1:%s\0A\00"
@.str.4 = [11 x i8] c"accept(): \00"
@__const.main.http_body = [12 x i8] c"hello world\00"
@.str.5 = [66 x i8] c"HTTP/1.0 200 OK\0D\0AContent-Length: %zu\0D\0AContent-Type: text/html\0D\0A\0D\0A\00"
@.str.6 = [5 x i8] c"send\00"
@EXIT_FAILURE = global i32 0
@.str.7 = [6 x i8] c"close\00"
@str = [29 x i8] c"Press Ctrl+C to exit server.\00"
@str.8 = [78 x i8] c"Run a web browser on the same computer as you run the server and point it to:\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = alloca [1024 x i8]
%3 = tail call %struct.addrinfo* @example_getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @example_socket(%struct.addrinfo* %3)
%5 = tail call i32 @example_setsockopt(i32 %4)
%6 = tail call i32 @example_bind(i32 %4, %struct.addrinfo* %3)
%7 = tail call i32 @example_listen(i32 %4, i32 128)
%8 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @str.8, i64 0, i64 0))
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %14
14: ; preds = %47, %0
%15 = call i32 @accept(i32 %4, i32* null, i32* null)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
br label %47
19: ; preds = %14
%20 = call i32 @strlen(i8* %12)
%21 = call i32 @snprintf(i8* %13, i32 1024, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0), i32 %20)
%22 = call i32 @strlen(i8* %13)
%23 = call i32 @send(i32 %15, i8* %13, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%27 = call i32 @close(i32 %15)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %19
%31 = call i32 @strlen(i8* %12)
%32 = call i32 @send(i32 %15, i8* %12, i32 %31, i32 0)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32 @close(i32 %15)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %30
%40 = call i32 @close(i32 %15)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %39
br label %47
47: ; preds = %46, %17
br label %14
}