#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ req;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ sscanf(char *, char *, unsigned short *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  unsigned short port;
  if (sscanf(argv[1], "%hd", &port) < 0) {
    exit(255);
  }
  int server = socket(PF_INET, SOCK_STREAM, 0);
  if (server < 0) {
    perror("socket ");
  }
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = PF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(port);
  if (bind(server, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind ");
  }
  if (listen(server, 128) < 0) {
    perror("listen ");
  }
  while (1) {
    int conn = accept(server, (struct sockaddr *)0, 0);
    char req[4096];
    read(conn, req, sizeof(req));
    write(conn, "HTTP/1.1 200 OK\r\n", 17);
    write(conn, "Content-Type: text/plain; charset=UTF-8\r\n", 41);
    write(conn, "Content-Length: 14\r\n", 20);
    write(conn, "Connection: close\r\n", 19);
    write(conn, "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n", 36);
    write(conn, "Server-Status: OK\r\n", 19);
    write(conn, "\r\n", 2);
    write(conn, "Hello from C!\n", 14);
    close(conn);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe08(%rip),%rsi
lea    -0x12(%rbp),%rdx
call   10a0 <sscanf@plt>
cmp    $0x0,%rax
jge    1215 <main+0x45>
mov    $0xff,%edi
call   10e0 <exit@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1247 <main+0x77>
lea    0xdc2(%rip),%rdi
call   10c0 <perror@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
movzwl -0x12(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12a7 <main+0xd7>
lea    0xd6a(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x18(%rbp),%edi
mov    $0x80,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12ca <main+0xfa>
lea    0xd4d(%rip),%rdi
call   10c0 <perror@plt>
jmp    12cf <main+0xff>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    -0x2c(%rbp),%edi
lea    0xd1c(%rip),%rsi
mov    $0x11,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    0xd1a(%rip),%rsi
mov    $0x29,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    0xd30(%rip),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    0xd31(%rip),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    0xd31(%rip),%rsi
mov    $0x24,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    0xd42(%rip),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    0xcf2(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
lea    0xd2e(%rip),%rsi
mov    $0xe,%edx
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
jmp    12cf <main+0xff>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    0x8(%rsi),%rdi
lea    0xe14(%rip),%rsi
lea    0xe(%rsp),%rdx
call   10a0 <sscanf@plt>
test   %rax,%rax
js     1395 <main+0x1c5>
mov    0x2dca(%rip),%r14
mov    (%r14),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1362 <main+0x192>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
movzwl 0xe(%rsp),%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     1373 <main+0x1a3>
mov    %ebx,%edi
mov    $0x80,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     1384 <main+0x1b4>
lea    0xded(%rip),%r14
lea    0xe0b(%rip),%r15
lea    0xdc8(%rip),%r12
lea    0xe11(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebp
mov    %eax,%edi
lea    0x20(%rsp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %ebp,%edi
lea    0xd43(%rip),%rsi
mov    $0x11,%edx
call   1030 <write@plt>
mov    %ebp,%edi
lea    0xd42(%rip),%rsi
mov    $0x29,%edx
call   1030 <write@plt>
mov    %ebp,%edi
lea    0xd59(%rip),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    %ebp,%edi
lea    0xd5b(%rip),%rsi
mov    $0x13,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x24,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x13,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0xe,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12b0 <main+0xe0>
lea    0xc9b(%rip),%rdi
call   10c0 <perror@plt>
jmp    1223 <main+0x53>
lea    0xc92(%rip),%rdi
call   10c0 <perror@plt>
jmp    1276 <main+0xa6>
lea    0xc87(%rip),%rdi
call   10c0 <perror@plt>
jmp    128b <main+0xbb>
mov    $0xff,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"%hd\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"socket \00"
@INADDR_ANY = global i32 0
@.str.2 = [6 x i8] c"bind \00"
@.str.3 = [8 x i8] c"listen \00"
@.str.4 = [18 x i8] c"HTTP/1.1 200 OK\0D\0A\00"
@.str.5 = [42 x i8] c"Content-Type: text/plain; charset=UTF-8\0D\0A\00"
@.str.6 = [21 x i8] c"Content-Length: 14\0D\0A\00"
@.str.7 = [20 x i8] c"Connection: close\0D\0A\00"
@.str.8 = [37 x i8] c"Date: Thu, 1 Jan 1970 00:00:00 GMT\0D\0A\00"
@.str.9 = [20 x i8] c"Server-Status: OK\0D\0A\00"
@.str.10 = [3 x i8] c"\0D\0A\00"
@.str.11 = [15 x i8] c"Hello from C!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @sscanf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i16* %6)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @exit(i32 255)
unreachable
18: ; preds = %2
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%28 = load i32, i32* @PF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = call i32 @htonl(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i16, i16* %6
%35 = call i32 @htons(i16 zeroext %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %7
%38 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %26
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %26
%44 = load i32, i32* %7
%45 = call i64 @listen(i32 %44, i32 128)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
br label %50
50: ; preds = %49, %50
%51 = load i32, i32* %7
%52 = call i32 @accept(i32 %51, %struct.sockaddr* null, i32 0)
store i32 %52, i32* %9
%53 = load i32, i32* %9
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 4096)
%56 = load i32, i32* %9
%57 = call i32 @write(i32 %56, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 17)
%58 = load i32, i32* %9
%59 = call i32 @write(i32 %58, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 41)
%60 = load i32, i32* %9
%61 = call i32 @write(i32 %60, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 20)
%62 = load i32, i32* %9
%63 = call i32 @write(i32 %62, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 19)
%64 = load i32, i32* %9
%65 = call i32 @write(i32 %64, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i32 36)
%66 = load i32, i32* %9
%67 = call i32 @write(i32 %66, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 19)
%68 = load i32, i32* %9
%69 = call i32 @write(i32 %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i32 2)
%70 = load i32, i32* %9
%71 = call i32 @write(i32 %70, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0), i32 14)
%72 = load i32, i32* %9
%73 = call i32 @close(i32 %72)
br label %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"%hd\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [8 x i8] c"socket \00"
@INADDR_ANY = global i32 0
@.str.2 = [6 x i8] c"bind \00"
@.str.3 = [8 x i8] c"listen \00"
@.str.4 = [18 x i8] c"HTTP/1.1 200 OK\0D\0A\00"
@.str.5 = [42 x i8] c"Content-Type: text/plain; charset=UTF-8\0D\0A\00"
@.str.6 = [21 x i8] c"Content-Length: 14\0D\0A\00"
@.str.7 = [20 x i8] c"Connection: close\0D\0A\00"
@.str.8 = [37 x i8] c"Date: Thu, 1 Jan 1970 00:00:00 GMT\0D\0A\00"
@.str.9 = [20 x i8] c"Server-Status: OK\0D\0A\00"
@.str.10 = [3 x i8] c"\0D\0A\00"
@.str.11 = [15 x i8] c"Hello from C!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = alloca %struct.sockaddr_in
%5 = alloca [4096 x i8]
%6 = bitcast i16* %3 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i64 @sscanf(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i16* %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @exit(i32 255)
unreachable
13: ; preds = %2
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = bitcast %struct.sockaddr_in* %4 to i8*
%22 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%23 = load i32, i32* @PF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = load i16, i16* %3
%29 = call i32 @htons(i16 zeroext %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %29, i32* %30
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i64 @bind(i32 %16, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %20
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %20
%37 = call i64 @listen(i32 %16, i32 128)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
br label %43
43: ; preds = %43, %41
%44 = call i32 @accept(i32 %16, %struct.sockaddr* null, i32 0)
%45 = call i32 @read(i32 %44, i8* %42, i32 4096)
%46 = call i32 @write(i32 %44, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 17)
%47 = call i32 @write(i32 %44, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 41)
%48 = call i32 @write(i32 %44, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 20)
%49 = call i32 @write(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 19)
%50 = call i32 @write(i32 %44, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0), i32 36)
%51 = call i32 @write(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 19)
%52 = call i32 @write(i32 %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i32 2)
%53 = call i32 @write(i32 %44, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0), i32 14)
%54 = call i32 @close(i32 %44)
br label %43
}