#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*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, int);
int read_line(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ write_line(int, char *, int);
void run_server(int port) {
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(port);
  int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
  listen(listen_fd, 1024);
  printf("Socket %d now listening at port %d\n", listen_fd, port);
  bool quit = false;
  while (!quit) {
    int fd = accept(listen_fd, NULL, NULL);
    char buffer[512];
    int n = read_line(fd, buffer, 512);
    quit = strncmp("exit", buffer, strlen("exit")) == 0;
    write_line(fd, buffer, n);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<run_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
call   10a0 <listen@plt>
mov    -0x14(%rbp),%esi
mov    -0x4(%rbp),%edx
lea    0xda3(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    127f <run_server+0xaf>
jmp    130b <run_server+0x13b>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   10c0 <read_line@plt>
mov    %eax,-0x224(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
lea    0xd64(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x230(%rbp),%rsi
mov    %eax,%edx
lea    0xd4f(%rip),%rdi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   10e0 <write_line@plt>
mov    -0x1c(%rbp),%edi
call   1090 <close@plt>
jmp    1269 <run_server+0x99>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edi,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2dec(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %r14d,%edi
mov    $0x400,%esi
call   10a0 <listen@plt>
lea    0xdaa(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1060 <printf@plt>
lea    0x10(%rsp),%r12
lea    0xdb8(%rip),%r15
nopl   0x0(%rax)
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x200,%edx
call   10c0 <read_line@plt>
mov    %eax,%r13d
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r13d,%edx
call   10e0 <write_line@plt>
mov    %ebx,%edi
call   1090 <close@plt>
test   %rbp,%rbp
jne    1270 <run_server+0xa0>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"Socket %d now listening at port %d\0A\00"
@.str.1 = [5 x i8] c"exit\00"
define void @run_server(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca i32
store i32 %0, i32* %2
%9 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* %2
%17 = call i32 @htons(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i32 @bind(i32 %22, %struct.sockaddr* %23, i32 12)
%25 = load i32, i32* %4
%26 = call i32 @listen(i32 %25, i32 1024)
%27 = load i32, i32* %4
%28 = load i32, i32* %2
%29 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %28)
store i32 0, i32* %5
br label %30
30: ; preds = %34, %1
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %34, label %51
34: ; preds = %30
%35 = load i32, i32* %4
%36 = call i32 @accept(i32 %35, i32* null, i32* null)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%39 = call i32 @read_line(i32 %37, i8* %38, i32 512)
store i32 %39, i32* %8
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%41 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%42 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %40, i32 %41)
%43 = icmp eq i64 %42, 0
%44 = zext i1 %43 to i32
store i32 %44, i32* %5
%45 = load i32, i32* %6
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%47 = load i32, i32* %8
%48 = call i32 @write_line(i32 %45, i8* %46, i32 %47)
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
br label %30
51: ; preds = %30
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [36 x i8] c"Socket %d now listening at port %d\0A\00"
@.str.1 = [5 x i8] c"exit\00"
define void @run_server(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [512 x i8]
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = call i32 @htonl(i32 %8)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = call i32 @htons(i32 %0)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%17 = call i32 @bind(i32 %15, %struct.sockaddr* %16, i32 12)
%18 = call i32 @listen(i32 %15, i32 1024)
%19 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %0)
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
br label %21
21: ; preds = %1, %21
%22 = call i32 @accept(i32 %15, i32* null, i32* null)
%23 = call i32 @read_line(i32 %22, i8* %20, i32 512)
%24 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %24)
%26 = icmp eq i64 %25, 0
%27 = call i32 @write_line(i32 %22, i8* %20, i32 %23)
%28 = call i32 @close(i32 %22)
br i1 %26, label %29, label %21
29: ; preds = %21
ret void
}