#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*/ servAddr;
typedef int /*<<< orphan*/ cliAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(char *, int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int read_line(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sd, newSd, cliLen;
  struct sockaddr_in cliAddr, servAddr;
  char line[100];
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("cannot open socket ");
    return 1;
  }
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(1500);
  if (bind(sd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
    perror("cannot bind port ");
    return 1;
  }
  listen(sd, 5);
  while (1) {
    printf("%s: waiting for data on port TCP %u\n", argv[0], 1500);
    cliLen = sizeof(cliAddr);
    newSd = accept(sd, (struct sockaddr *)&cliAddr, &cliLen);
    if (newSd < 0) {
      perror("cannot accept connection ");
      return 1;
    }
    memset(line, 0x0, 100);
    while (read_line(newSd, line) != 1) {
      printf("%s: received from %s:TCP%d : %s\n", argv[0],
             inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line);
      memset(line, 0x0, 100);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1217 <main+0x57>
lea    0xdfa(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x1a9>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x34(%rbp)
mov    $0x5dc,%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1274 <main+0xb4>
lea    0xdb1(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x1a9>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd97(%rip),%rdi
mov    $0x5dc,%edx
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d7 <main+0x117>
lea    0xd85(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1369 <main+0x1a9>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0xa0(%rbp),%rsi
call   10b0 <read_line@plt>
cmp    $0x1,%eax
je     1364 <main+0x1a4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xb0(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0xa8(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0xb0(%rbp),%rsi
mov    -0xa8(%rbp),%rdx
mov    %eax,%ecx
lea    -0xa0(%rbp),%r8
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
jmp    12ea <main+0x12a>
jmp    1281 <main+0xc1>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rsi,%rbx
mov    0x2e0d(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    11fe <main+0x3e>
lea    0xe07(%rip),%rdi
jmp    133b <main+0x17b>
mov    %eax,%r14d
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x5dc,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    124b <main+0x8b>
lea    0xdce(%rip),%rdi
jmp    133b <main+0x17b>
mov    %r14d,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    (%rbx),%rsi
lea    0xdc4(%rip),%rdi
mov    $0x5dc,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0xc,0x8(%rsp)
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     1334 <main+0x174>
mov    %eax,%r12d
lea    0x30(%rsp),%rbp
lea    0xdc6(%rip),%r15
mov    %r14d,0xc(%rsp)
jmp    12dd <main+0x11d>
cs nopw 0x0(%rax,%rax,1)
mov    (%rbx),%r13
mov    0x24(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x20(%rsp),%edi
call   1090 <ntohs@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %eax,%ecx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   10b0 <read_line@plt>
cmp    $0x1,%eax
jne    12b0 <main+0xf0>
mov    (%rbx),%rsi
lea    0xd20(%rip),%rdi
mov    $0x5dc,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0xc,0x8(%rsp)
mov    0xc(%rsp),%edi
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   10d0 <accept@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    12dd <main+0x11d>
lea    0xd10(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
add    $0x98,%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
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"cannot open socket \00"
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"cannot bind port \00"
@.str.2 = [37 x i8] c"%s: waiting for data on port TCP %u\0A\00"
@.str.3 = [26 x i8] c"cannot accept connection \00"
@.str.4 = [33 x i8] c"%s: received from %s:TCP%d : %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %73
19: ; preds = %2
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = call i32 @htons(i32 1500)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %19
%33 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %73
34: ; preds = %19
%35 = load i32, i32* %6
%36 = call i32 @listen(i32 %35, i32 5)
br label %37
37: ; preds = %34, %72
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 0
%40 = load i8*, i8** %39
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 1500)
store i32 12, i32* %8
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%44 = call i32 @accept(i32 %42, %struct.sockaddr* %43, i32* %8)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %37
%48 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %73
49: ; preds = %37
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%51 = call i32 @memset(i8* %50, i32 0, i32 100)
br label %52
52: ; preds = %57, %49
%53 = load i32, i32* %7
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%55 = call i32 @read_line(i32 %53, i8* %54)
%56 = icmp ne i32 %55, 1
br i1 %56, label %57, label %72
57: ; preds = %52
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 0
%60 = load i8*, i8** %59
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i8* @inet_ntoa(i32 %63)
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call i32 @ntohs(i32 %66)
%68 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%69 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %60, i8* %64, i32 %67, i8* %68)
%70 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%71 = call i32 @memset(i8* %70, i32 0, i32 100)
br label %52
72: ; preds = %52
br label %37
73: ; preds = %47, %32, %17
%74 = load i32, i32* %3
ret i32 %74
}

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
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"cannot open socket \00"
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"cannot bind port \00"
@.str.2 = [37 x i8] c"%s: waiting for data on port TCP %u\0A\00"
@.str.3 = [26 x i8] c"cannot accept connection \00"
@.str.4 = [33 x i8] c"%s: received from %s:TCP%d : %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [100 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %62
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = tail call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 1500)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = call i64 @bind(i32 %13, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %62
30: ; preds = %17
%31 = call i32 @listen(i32 %13, i32 5)
%32 = load i8*, i8** %1
%33 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %32, i32 1500)
store i32 12, i32* %3
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @accept(i32 %13, %struct.sockaddr* %34, i32* %3)
%36 = icmp slt i32 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %30
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %47
40: ; preds = %52, %47
%41 = load i8*, i8** %1
%42 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 1500)
store i32 12, i32* %3
%43 = call i32 @accept(i32 %13, %struct.sockaddr* %34, i32* %3)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %40, %30
%46 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %62
47: ; preds = %37, %40
%48 = phi i32 [ %35, %37 ], [ %43, %40 ]
%49 = call i32 @memset(i8* %10, i32 0, i32 100)
%50 = call i32 @read_line(i32 %48, i8* %10)
%51 = icmp eq i32 %50, 1
br i1 %51, label %40, label %52
52: ; preds = %47, %52
%53 = load i8*, i8** %1
%54 = load i32, i32* %38
%55 = call i8* @inet_ntoa(i32 %54)
%56 = load i32, i32* %39
%57 = call i32 @ntohs(i32 %56)
%58 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %53, i8* %55, i32 %57, i8* %10)
%59 = call i32 @memset(i8* %10, i32 0, i32 100)
%60 = call i32 @read_line(i32 %48, i8* %10)
%61 = icmp eq i32 %60, 1
br i1 %61, label %40, label %52
62: ; preds = %45, %28, %15
ret i32 1
}