#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*/ socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int socket_desc, client_sock, c, read_size;
  struct sockaddr_in server, client;
  char buff[110];
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_desc == -1) {
    printf("Could not create socket");
    return 1;
  }
  puts("Socket created");
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(1235);
  if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("bind failed. Error");
    return 1;
  }
  puts("bind done");
  listen(socket_desc, 3);
  puts("Waiting for incoming connections...");
  c = sizeof(struct sockaddr_in);
  while (1) {
    client_sock =
        accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c);
    if (client_sock > 0) {
      puts("Connection accepted");
      while (1) {
        read_size = recv(client_sock, buff, sizeof(buff), 0);
        write(client_sock, buff, strlen(buff));
        memset(buff, 0, sizeof(buff));
        if (read_size == 0) {
          puts("Client disconnected");
          fflush(stdout);
          break;
        } else if (read_size == -1)
          perror("recv failed");
      }
    } else
      puts("Connection failed");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1220 <main+0x50>
lea    0xdf1(%rip),%rdi
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x1cb>
lea    0xdf1(%rip),%rdi
call   1040 <puts@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x4d3,%edi
call   1070 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    1284 <main+0xb4>
lea    0xdb4(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    139b <main+0x1cb>
lea    0xdaf(%rip),%rdi
call   1040 <puts@plt>
mov    -0x8(%rbp),%edi
mov    $0x3,%esi
call   10b0 <listen@plt>
lea    0xda0(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    138a <main+0x1ba>
lea    0xd94(%rip),%rdi
call   1040 <puts@plt>
mov    -0xc(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x6e,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x6e,%edx
call   1090 <memset@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1365 <main+0x195>
lea    0xd2f(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    1385 <main+0x1b5>
cmpl   $0xffffffff,-0x14(%rbp)
jne    137b <main+0x1ab>
lea    0xd1a(%rip),%rdi
call   10d0 <perror@plt>
jmp    1380 <main+0x1b0>
jmp    12d9 <main+0x109>
jmp    1396 <main+0x1c6>
lea    0xd0b(%rip),%rdi
call   1040 <puts@plt>
jmp    12b0 <main+0xe0>
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    $0xa8,%rsp
mov    0x2e00(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1333 <main+0x163>
mov    %eax,%ebp
lea    0xe0c(%rip),%rdi
call   1040 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x4d3,%edi
call   1070 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     1356 <main+0x186>
lea    0xde7(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    $0x3,%esi
call   10b0 <listen@plt>
lea    0xdd9(%rip),%rdi
call   1040 <puts@plt>
movl   $0xc,0xc(%rsp)
lea    0xc(%rsp),%r15
lea    0x30(%rsp),%rbx
lea    0xe07(%rip),%r13
jmp    129c <main+0xcc>
nopl   0x0(%rax,%rax,1)
lea    0xe05(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
lea    0x20(%rsp),%rsi
mov    %r15,%rdx
call   10e0 <accept@plt>
test   %eax,%eax
jle    1290 <main+0xc0>
mov    %eax,%r12d
lea    0xdaf(%rip),%rdi
call   1040 <puts@plt>
jmp    12d0 <main+0x100>
mov    %r13,%rdi
call   10d0 <perror@plt>
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x6e,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x6e,%edx
call   1090 <memset@plt>
cmp    $0xffffffff,%r14d
je     12c0 <main+0xf0>
test   %r14d,%r14d
jne    12d0 <main+0x100>
lea    0xd61(%rip),%rdi
call   1040 <puts@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    129c <main+0xcc>
lea    0xcc6(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcca(%rip),%rdi
call   10d0 <perror@plt>
jmp    133f <main+0x16f>

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 = [24 x i8] c"Could not create socket\00"
@.str.1 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.2 = [19 x i8] c"bind failed. Error\00"
@.str.3 = [10 x i8] c"bind done\00"
@.str.4 = [36 x i8] c"Waiting for incoming connections...\00"
@.str.5 = [20 x i8] c"Connection accepted\00"
@.str.6 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.7 = [12 x i8] c"recv failed\00"
@.str.8 = [18 x i8] c"Connection failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca [110 x i8]
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 eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %72
16: ; preds = %0
%17 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = call i32 @htons(i32 1235)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%27 = call i64 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %72
31: ; preds = %16
%32 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* %2
%34 = call i32 @listen(i32 %33, i32 3)
%35 = call i32 @puts(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %4
br label %36
36: ; preds = %31, %71
%37 = load i32, i32* %2
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %4)
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %69
42: ; preds = %36
%43 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %67
%45 = load i32, i32* %3
%46 = getelementptr inbounds [110 x i8], [110 x i8]* %8, i64 0, i64 0
%47 = call i32 @recv(i32 %45, i8* %46, i32 110, i32 0)
store i32 %47, i32* %5
%48 = load i32, i32* %3
%49 = getelementptr inbounds [110 x i8], [110 x i8]* %8, i64 0, i64 0
%50 = getelementptr inbounds [110 x i8], [110 x i8]* %8, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i32 @write(i32 %48, i8* %49, i32 %51)
%53 = getelementptr inbounds [110 x i8], [110 x i8]* %8, i64 0, i64 0
%54 = call i32 @memset(i8* %53, i32 0, i32 110)
%55 = load i32, i32* %5
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %44
%58 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%59 = load i32, i32* @stdout
%60 = call i32 @fflush(i32 %59)
br label %68
61: ; preds = %44
%62 = load i32, i32* %5
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %61
%65 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %66
66: ; preds = %64, %61
br label %67
67: ; preds = %66
br label %44
68: ; preds = %57
br label %71
69: ; preds = %36
%70 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %71
71: ; preds = %69, %68
br label %36
72: ; preds = %29, %14
%73 = load i32, i32* %1
ret i32 %73
}

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 = [24 x i8] c"Could not create socket\00"
@.str.1 = [15 x i8] c"Socket created\00"
@INADDR_ANY = global i32 0
@.str.2 = [19 x i8] c"bind failed. Error\00"
@.str.3 = [10 x i8] c"bind done\00"
@.str.4 = [36 x i8] c"Waiting for incoming connections...\00"
@.str.5 = [20 x i8] c"Connection accepted\00"
@.str.6 = [20 x i8] c"Client disconnected\00"
@stdout = global i32 0
@.str.7 = [12 x i8] c"recv failed\00"
@.str.8 = [18 x i8] c"Connection failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca [110 x i8]
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = getelementptr inbounds [110 x i8], [110 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %53
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 1235)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i64 @bind(i32 %11, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %53
28: ; preds = %15
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @listen(i32 %11, i32 3)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
store i32 12, i32* %1
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %33
33: ; preds = %52, %28
%34 = call i32 @accept(i32 %11, %struct.sockaddr* %32, i32* %1)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %38
38: ; preds = %43, %36
%39 = call i32 @recv(i32 %34, i8* %8, i32 110, i32 0)
%40 = call i32 @strlen(i8* %8)
%41 = call i32 @write(i32 %34, i8* %8, i32 %40)
%42 = call i32 @memset(i8* %8, i32 0, i32 110)
switch i32 %39, label %43 [
i32 0, label %44
i32 -1, label %48
]
43: ; preds = %38, %48
br label %38
44: ; preds = %38
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
%46 = load i32, i32* @stdout
%47 = call i32 @fflush(i32 %46)
br label %52
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %43
50: ; preds = %33
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %52
52: ; preds = %50, %44
br label %33
53: ; preds = %26, %13
ret i32 1
}