#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*/ serv_addr;
typedef int /*<<< orphan*/ recvBuff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(void) {
  int listenfd = 0;
  int connfd = 0;
  int n = 0;
  struct sockaddr_in serv_addr;
  char sendBuff[1024];
  char recvBuff[1024];
  listenfd = socket(AF_INET, SOCK_STREAM, 0);
  printf("socket retrieve success\n");
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(5000);
  bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (listen(listenfd, 10) == -1) {
    printf("Failed to listen\n");
    return -1;
  }
  while (1) {
    connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
    strcpy(sendBuff, "Message from server");
    write(connfd, sendBuff, strlen(sendBuff));
    while (strcmp(recvBuff, "exit") != 0) {
      memset(recvBuff, '\0', sizeof(recvBuff));
      n = read(connfd, recvBuff, 255);
      if (n < 0) {
        printf("Error\n");
      }
      printf("%s\n", recvBuff);
      if (n < 0) {
        printf("\n Read Error \n");
      }
    }
    printf("Closing connection\n");
    close(connfd);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1388,%edi
call   1060 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   10d0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12ae <main+0xbe>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0xffffffff,%eax
add    $0x830,%rsp
pop    %rbp
ret
jmp    12b3 <main+0xc3>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10f0 <accept@plt>
mov    %eax,-0xc(%rbp)
lea    -0x420(%rbp),%rdi
lea    0xd58(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    -0x820(%rbp),%rdi
lea    0xd22(%rip),%rsi
call   10c0 <strcmp@plt>
cmp    $0x0,%rax
je     13a0 <main+0x1b0>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0xff,%edx
call   10b0 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    136e <main+0x17e>
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x820(%rbp),%rsi
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jge    139b <main+0x1ab>
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    130f <main+0x11f>
lea    0xcb7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xc(%rbp),%edi
call   10a0 <close@plt>
mov    $0x1,%edi
call   1100 <sleep@plt>
jmp    12b3 <main+0xc3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    0x2de0(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
lea    0xdf6(%rip),%rdi
call   1040 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x1388,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10e0 <bind@plt>
mov    %ebp,0xc(%rsp)
mov    %ebp,%edi
mov    $0xa,%esi
call   10d0 <listen@plt>
cmp    $0xffffffff,%eax
je     1359 <main+0x169>
lea    0x420(%rsp),%r12
lea    0xd8d(%rip),%rbp
lea    0x20(%rsp),%rbx
lea    0xdbf(%rip),%r14
lea    0xdaa(%rip),%r13
jmp    12be <main+0xce>
nopl   0x0(%rax)
lea    0xd8a(%rip),%rdi
call   1040 <puts@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
mov    $0x1,%edi
call   1100 <sleep@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10f0 <accept@plt>
mov    %eax,%r15d
mov    %r12,%rdi
lea    0xd28(%rip),%rsi
call   1030 <strcpy@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    1318 <main+0x128>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10c0 <strcmp@plt>
test   %rax,%rax
je     12a0 <main+0xb0>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   10b0 <read@plt>
test   %eax,%eax
js     1300 <main+0x110>
mov    %rbx,%rdi
call   1040 <puts@plt>
jmp    1318 <main+0x128>
lea    0xcf8(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x828,%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 = [25 x i8] c"socket retrieve success\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"Failed to listen\0A\00"
@.str.2 = [20 x i8] c"Message from server\00"
@.str.3 = [5 x i8] c"exit\00"
@.str.4 = [7 x i8] c"Error\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [15 x i8] c"\0A Read Error \0A\00"
@.str.7 = [20 x i8] c"Closing connection\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 5000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = load i32, i32* %2
%24 = call i32 @listen(i32 %23, i32 10)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
ret i32 -1
28: ; preds = %0
br label %29
29: ; preds = %28, %61
%30 = load i32, i32* %2
%31 = call i32 @accept(i32 %30, %struct.sockaddr* null, i32* null)
store i32 %31, i32* %3
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* %3
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @write(i32 %34, i8* %35, i32 %37)
br label %39
39: ; preds = %60, %29
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %61
43: ; preds = %39
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%45 = call i32 @memset(i8* %44, i8 signext 0, i32 1024)
%46 = load i32, i32* %3
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 255)
store i32 %48, i32* %4
%49 = load i32, i32* %4
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %43
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %43
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %4
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %53
br label %39
61: ; preds = %39
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
%65 = call i32 @sleep(i32 1)
br label %29
}

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
@INADDR_ANY = global i32 0
@.str.2 = [20 x i8] c"Message from server\00"
@.str.3 = [5 x i8] c"exit\00"
@str = [24 x i8] c"socket retrieve success\00"
@str.8 = [19 x i8] c"Closing connection\00"
@str.9 = [14 x i8] c"\0A Read Error \00"
@str.10 = [6 x i8] c"Error\00"
@str.11 = [17 x i8] c"Failed to listen\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = tail call i32 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 5000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%19 = call i32 @bind(i32 %9, %struct.sockaddr* %18, i32 12)
%20 = call i32 @listen(i32 %9, i32 10)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
ret i32 -1
24: ; preds = %0, %44
%25 = call i32 @accept(i32 %9, %struct.sockaddr* null, i32* null)
%26 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @strlen(i8* %5)
%28 = call i32 @write(i32 %25, i8* %5, i32 %27)
%29 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %44, label %31
31: ; preds = %24, %41
%32 = call i32 @memset(i8* %6, i8 signext 0, i32 1024)
%33 = call i32 @read(i32 %25, i8* %6, i32 255)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.10, i64 0, i64 0))
%37 = call i32 @puts(i8* %6)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
br label %41
39: ; preds = %31
%40 = call i32 @puts(i8* %6)
br label %41
41: ; preds = %39, %35
%42 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %31
44: ; preds = %41, %24
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
%46 = call i32 @close(i32 %25)
%47 = call i32 @sleep(i32 1)
br label %24
}