#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 */
typedef int /*<<< orphan*/ val;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ raddr;
typedef int /*<<< orphan*/ laddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, void *, int *);
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, void *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ server_job(int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sd, newsd;
  struct sockaddr_in laddr, raddr;
  socklen_t raddr_len;
  char ipstr[1024];
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("socket()");
    exit(1);
  }
  int val = 1;
  if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
    perror("setsockopt()");
    exit(1);
  }
  laddr.sin_family = AF_INET;
  laddr.sin_port = htons(atoi("1989"));
  inet_pton(AF_INET, "0.0.0.0", &laddr.sin_addr);
  if (bind(sd, (void *)&laddr, sizeof(laddr)) < 0) {
    perror("bind()");
    exit(1);
  }
  if (listen(sd, 200) < 0) {
    perror("listen()");
    exit(1);
  }
  raddr_len = sizeof(raddr);
  while (1) {
    newsd = accept(sd, (void *)&raddr, &raddr_len);
    if (newsd < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      perror("accept()");
      exit(1);
    }
    inet_ntop(AF_INET, &raddr.sin_addr, ipstr, 1024);
    printf("-----MESSAGE FROM:[%s:%d]-----\n", ipstr, ntohs(raddr.sin_port));
    server_job(newsd);
    close(newsd);
  }
  close(sd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    123e <main+0x4e>
lea    0xdd1(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
movl   $0x1,-0x434(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
lea    -0x434(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    128f <main+0x9f>
lea    0xd89(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xd74(%rip),%rdi
call   10f0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
add    $0x4,%rdx
lea    0xd52(%rip),%rsi
call   1090 <inet_pton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    12ff <main+0x10f>
lea    0xd33(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0xc8,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
jge    132c <main+0x13c>
lea    0xd0d(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    139f <main+0x1af>
mov    0x2c71(%rip),%rax
mov    (%rax),%rax
mov    0x2c5f(%rip),%rcx
cmp    (%rcx),%rax
je     1384 <main+0x194>
mov    0x2c57(%rip),%rax
mov    (%rax),%rax
mov    0x2c2d(%rip),%rcx
cmp    (%rcx),%rax
jne    1389 <main+0x199>
jmp    1333 <main+0x143>
lea    0xca3(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2c3a(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
lea    -0x430(%rbp),%rdx
mov    $0x400,%ecx
call   1030 <inet_ntop@plt>
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x28(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
lea    0xc55(%rip),%rdi
call   1070 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <server_job@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
jmp    1333 <main+0x143>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    121a <main+0x3a>
lea    0xdeb(%rip),%rdi
jmp    136e <main+0x18e>
mov    %eax,%ebp
movl   $0x1,0x8(%rsp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    0x2d84(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    1259 <main+0x79>
lea    0xdb5(%rip),%rdi
jmp    136e <main+0x18e>
mov    0x2d80(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x7c5,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
lea    0x14(%rsp),%rdx
lea    0xd94(%rip),%rsi
call   1090 <inet_pton@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
jns    12a9 <main+0xc9>
lea    0xd7a(%rip),%rdi
jmp    136e <main+0x18e>
mov    %ebp,%edi
mov    $0xc8,%esi
call   10a0 <listen@plt>
test   %rax,%rax
jns    12c6 <main+0xe6>
lea    0xd64(%rip),%rdi
jmp    136e <main+0x18e>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0x30(%rsp),%rbx
lea    0xd53(%rip),%r13
jmp    1334 <main+0x154>
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%r14d
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0x24(%rsp),%rsi
mov    %rbx,%rdx
mov    $0x400,%ecx
call   1030 <inet_ntop@plt>
mov    0x20(%rsp),%edi
call   10b0 <ntohs@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <printf@plt>
mov    %r14d,%edi
call   1050 <server_job@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10e0 <accept@plt>
test   %eax,%eax
jns    12f0 <main+0x110>
mov    0x2c7c(%rip),%rax
mov    (%rax),%rax
mov    0x2c6a(%rip),%rcx
cmp    (%rcx),%rax
je     1334 <main+0x154>
mov    0x2c46(%rip),%rcx
cmp    (%rcx),%rax
je     1334 <main+0x154>
lea    0xcc0(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"setsockopt()\00"
@.str.2 = [5 x i8] c"1989\00"
@.str.3 = [8 x i8] c"0.0.0.0\00"
@.str.4 = [7 x i8] c"bind()\00"
@.str.5 = [9 x i8] c"listen()\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.6 = [9 x i8] c"accept()\00"
@.str.7 = [32 x i8] c"-----MESSAGE FROM:[%s:%d]-----\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
store i32 1, i32* %8
%18 = load i32, i32* %2
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i64 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %8, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @atoi(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%34 = call i32 @inet_pton(i32 %32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %33)
%35 = load i32, i32* %2
%36 = bitcast %struct.sockaddr_in* %4 to i8*
%37 = call i64 @bind(i32 %35, i8* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %26
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %26
%43 = load i32, i32* %2
%44 = call i64 @listen(i32 %43, i32 200)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %42
store i32 12, i32* %6
br label %50
50: ; preds = %49, %64, %68
%51 = load i32, i32* %2
%52 = bitcast %struct.sockaddr_in* %5 to i8*
%53 = call i32 @accept(i32 %51, i8* %52, i32* %6)
store i32 %53, i32* %3
%54 = load i32, i32* %3
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %68
56: ; preds = %50
%57 = load i64, i64* @errno
%58 = load i64, i64* @EINTR
%59 = icmp eq i64 %57, %58
br i1 %59, label %64, label %60
60: ; preds = %56
%61 = load i64, i64* @errno
%62 = load i64, i64* @EAGAIN
%63 = icmp eq i64 %61, %62
br i1 %63, label %64, label %65
64: ; preds = %60, %56
br label %50
65: ; preds = %60
%66 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %50
%69 = load i32, i32* @AF_INET
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%72 = call i32 @inet_ntop(i32 %69, i32* %70, i8* %71, i32 1024)
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i32 @ntohs(i32 %75)
%77 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %73, i32 %76)
%78 = load i32, i32* %3
%79 = call i32 @server_job(i32 %78)
%80 = load i32, i32* %3
%81 = call i32 @close(i32 %80)
br label %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"setsockopt()\00"
@.str.3 = [8 x i8] c"0.0.0.0\00"
@.str.4 = [7 x i8] c"bind()\00"
@.str.5 = [9 x i8] c"listen()\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@.str.6 = [9 x i8] c"accept()\00"
@.str.7 = [32 x i8] c"-----MESSAGE FROM:[%s:%d]-----\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i64 @setsockopt(i32 %12, i32 %19, i32 %20, i32* %5, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 1989)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%33 = call i32 @inet_pton(i32 %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* %32)
%34 = call i64 @bind(i32 %12, i8* %6, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %26
%40 = call i64 @listen(i32 %12, i32 200)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %39
store i32 12, i32* %3
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %48
48: ; preds = %69, %45
%49 = call i32 @accept(i32 %12, i8* %7, i32* %3)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %61
51: ; preds = %48
%52 = load i64, i64* @errno
%53 = load i64, i64* @EINTR
%54 = icmp eq i64 %52, %53
%55 = load i64, i64* @EAGAIN
%56 = icmp eq i64 %52, %55
%57 = select i1 %54, i1 true, i1 %56
br i1 %57, label %69, label %58
58: ; preds = %51
%59 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %48
%62 = load i32, i32* @AF_INET
%63 = call i32 @inet_ntop(i32 %62, i32* %46, i8* %9, i32 1024)
%64 = load i32, i32* %47
%65 = call i32 @ntohs(i32 %64)
%66 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i8* %9, i32 %65)
%67 = call i32 @server_job(i32 %49)
%68 = call i32 @close(i32 %49)
br label %69
69: ; preds = %61, %51
br label %48
}