#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ adress;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ string_reverse(char *, int);
int write(int, char *, int);
int main() {
  int sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("failed to create socket");
    exit(1);
  }
  struct sockaddr_in adress;
  adress.sin_family = AF_INET;
  adress.sin_port = htons(19002);
  adress.sin_addr.s_addr = INADDR_ANY;
  if (bind(sd, (struct sockaddr *)&adress, sizeof(adress)) < 0) {
    perror("failed to bind");
    exit(1);
  }
  if (listen(sd, SOMAXCONN) < 0) {
    perror("failed to listen");
    exit(1);
  }
  while (1) {
    struct sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    int cd = accept(sd, (struct sockaddr *)&client_addr, &client_addr_len);
    if (cd < 0) {
      perror("Failed to accept");
      close(cd);
      continue;
    }
    char buffer[1024];
    int read_n = read(cd, buffer, 1024 - 1);
    if (read_n == -1) {
      perror("Failed to read");
      close(cd);
      continue;
    }
    buffer[read_n] = 0;
    string_reverse(buffer, read_n);
    if (write(cd, buffer, read_n) == -1) {
      perror("failed to write");
    }
    close(cd);
  }
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fe <main+0x4e>
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x4a3a,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1254 <main+0xa4>
lea    0xdd3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    1285 <main+0xd5>
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    128a <main+0xda>
movl   $0xc,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    12c7 <main+0x117>
lea    0xd83(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
jmp    128a <main+0xda>
mov    -0x30(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0xffffffff,-0x434(%rbp)
jne    1307 <main+0x157>
lea    0xd54(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
jmp    128a <main+0xda>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rdi
mov    -0x434(%rbp),%esi
call   1050 <string_reverse@plt>
mov    -0x30(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x434(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1352 <main+0x1a2>
lea    0xd0b(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x30(%rbp),%edi
call   1060 <close@plt>
jmp    128a <main+0xda>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    0x2e20(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12c9 <main+0x119>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x4a3a,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12d2 <main+0x122>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <listen@plt>
test   %rax,%rax
js     12db <main+0x12b>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x30(%rsp),%r13
jmp    1263 <main+0xb3>
nopl   0x0(%rax,%rax,1)
lea    0xde1(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1250 <main+0xa0>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
lea    0xdb5(%rip),%rdi
cmp    $0xffffffff,%eax
je     1257 <main+0xa7>
mov    %eax,%r12d
cltq
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r13,%rdi
mov    %r12d,%esi
call   1050 <string_reverse@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
lea    0xd96(%rip),%rdi
cmp    $0xffffffff,%eax
je     1257 <main+0xa7>
jmp    125c <main+0xac>
lea    0xd30(%rip),%rdi
jmp    12e2 <main+0x132>
lea    0xd3f(%rip),%rdi
jmp    12e2 <main+0x132>
lea    0xd45(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, 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"failed to create socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [15 x i8] c"failed to bind\00"
@SOMAXCONN = global i32 0
@.str.2 = [17 x i8] c"failed to listen\00"
@.str.3 = [17 x i8] c"Failed to accept\00"
@.str.4 = [15 x i8] c"Failed to read\00"
@.str.5 = [16 x i8] c"failed to write\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%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 ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 19002)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %3 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 %32
29: ; preds = %17
%30 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %17
%33 = load i32, i32* %2
%34 = load i32, i32* @SOMAXCONN
%35 = call i64 @listen(i32 %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %32
br label %41
41: ; preds = %40, %47, %57, %75
store i32 12, i32* %5
%42 = load i32, i32* %2
%43 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%44 = call i32 @accept(i32 %42, %struct.sockaddr* %43, i32* %5)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
br label %41
51: ; preds = %41
%52 = load i32, i32* %6
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 1023)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %61
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
br label %41
61: ; preds = %51
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%66 = load i32, i32* %8
%67 = call i32 @string_reverse(i8* %65, i32 %66)
%68 = load i32, i32* %6
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%70 = load i32, i32* %8
%71 = call i32 @write(i32 %68, i8* %69, i32 %70)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %61
%74 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %75
75: ; preds = %73, %61
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
br label %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, 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"failed to create socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [15 x i8] c"failed to bind\00"
@SOMAXCONN = global i32 0
@.str.2 = [17 x i8] c"failed to listen\00"
@.str.3 = [17 x i8] c"Failed to accept\00"
@.str.4 = [15 x i8] c"Failed to read\00"
@.str.5 = [16 x i8] c"failed to write\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = bitcast %struct.sockaddr_in* %1 to i8*
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 19002)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i64 @bind(i32 %7, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %12
%27 = load i32, i32* @SOMAXCONN
%28 = call i64 @listen(i32 %7, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = bitcast %struct.sockaddr_in* %2 to i8*
%32 = bitcast i32* %3 to i8*
%33 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
br label %38
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30, %58
store i32 12, i32* %3
%39 = call i32 @accept(i32 %7, %struct.sockaddr* %33, i32* %3)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @close(i32 %39)
br label %58
44: ; preds = %38
%45 = call i32 @read(i32 %39, i8* %34, i32 1023)
%46 = icmp eq i32 %45, -1
br i1 %46, label %53, label %47
47: ; preds = %44
%48 = sext i32 %45 to i64
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %48
store i8 0, i8* %49
%50 = call i32 @string_reverse(i8* %34, i32 %45)
%51 = call i32 @write(i32 %39, i8* %34, i32 %45)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %47, %44
%54 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), %44 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), %47 ]
%55 = call i32 @perror(i8* %54)
br label %56
56: ; preds = %53, %47
%57 = call i32 @close(i32 %39)
br label %58
58: ; preds = %56, %41
br label %38
}