#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 socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sprintf(char *, char *);
int write(int, char *, int);
int main(void) {
  char buffer[512] = {0};
  struct sockaddr_in server_address, client_address;
  int server_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (server_socket < 0) {
    perror("socket");
    exit(1);
  }
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = INADDR_ANY;
  server_address.sin_port = htons(1234);
  bind(server_socket, (struct sockaddr *)&server_address,
       sizeof(struct sockaddr_in));
  if (errno != 0) {
    perror("bind");
    exit(2);
  }
  listen(server_socket, 5);
  if (errno != 0) {
    perror("listen");
    exit(3);
  }
  socklen_t client_address_length = sizeof(struct sockaddr_in);
  int client_socket = accept(server_socket, (struct sockaddr *)&client_address,
                             &client_address_length);
  if (client_socket < 0) {
    perror("accept");
    exit(4);
  }
  int message_length = read(client_socket, buffer, 512 - 1);
  if (message_length < 0) {
    perror("read");
    exit(5);
  }
  printf("from client: %s\n", buffer);
  message_length = sprintf(buffer, "back to ya client");
  message_length = write(client_socket, buffer, message_length);
  if (message_length < 0) {
    perror("write");
    exit(6);
  }
  close(client_socket);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jge    1237 <main+0x67>
lea    0xdd8(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x218(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x21c(%rbp)
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,-0x220(%rbp)
mov    -0x234(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    0x2d45(%rip),%rax
cmpq   $0x0,(%rax)
je     12a3 <main+0xd3>
lea    0xd73(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    -0x234(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
mov    0x2d0e(%rip),%rax
cmpq   $0x0,(%rax)
je     12da <main+0x10a>
lea    0xd41(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
movl   $0xc,-0x238(%rbp)
mov    -0x234(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x238(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x23c(%rbp)
cmpl   $0x0,-0x23c(%rbp)
jge    1326 <main+0x156>
lea    0xcfc(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10e0 <exit@plt>
mov    -0x23c(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1ff,%edx
call   1080 <read@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0x0,-0x240(%rbp)
jge    1366 <main+0x196>
lea    0xcc3(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x5,%edi
call   10e0 <exit@plt>
lea    -0x210(%rbp),%rsi
lea    0xcab(%rip),%rdi
call   1050 <printf@plt>
lea    -0x210(%rbp),%rdi
lea    0xca9(%rip),%rsi
call   10d0 <sprintf@plt>
mov    %eax,-0x240(%rbp)
mov    -0x23c(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x240(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x240(%rbp)
cmpl   $0x0,-0x240(%rbp)
jge    13d3 <main+0x203>
lea    0xc7e(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x6,%edi
call   10e0 <exit@plt>
mov    -0x23c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x238,%rsp
lea    0x30(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2e06(%rip),%r14
mov    (%r14),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     12e7 <main+0x127>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    0x2d8f(%rip),%r14
cmpq   $0x0,(%r14)
jne    12fd <main+0x13d>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmpq   $0x0,(%r14)
jne    1313 <main+0x153>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10c0 <accept@plt>
test   %eax,%eax
js     1329 <main+0x169>
mov    %eax,%ebx
lea    0x30(%rsp),%rsi
mov    %eax,%edi
mov    $0x1ff,%edx
call   1080 <read@plt>
test   %eax,%eax
js     133f <main+0x17f>
lea    0xd83(%rip),%rdi
lea    0x30(%rsp),%r14
mov    %r14,%rsi
call   1050 <printf@plt>
movups 0xd80(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movw   $0x74,0x40(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x11,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1355 <main+0x195>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x238,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd12(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd03(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
lea    0xcf2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
lea    0xce3(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x4,%edi
call   10d0 <exit@plt>
lea    0xcd4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x5,%edi
call   10d0 <exit@plt>
lea    0xce6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x6,%edi
call   10d0 <exit@plt>

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 = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@errno = global i64 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [17 x i8] c"from client: %s\0A\00"
@.str.6 = [18 x i8] c"back to ya client\00"
@.str.7 = [6 x i8] c"write\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = bitcast [512 x i8]* %2 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 1234)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %5
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = load i64, i64* @errno
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %18
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %18
%35 = load i32, i32* %5
%36 = call i32 @listen(i32 %35, i32 5)
%37 = load i64, i64* @errno
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 3)
unreachable
42: ; preds = %34
store i32 12, i32* %6
%43 = load i32, i32* %5
%44 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %6)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 4)
unreachable
51: ; preds = %42
%52 = load i32, i32* %7
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 511)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 5)
unreachable
60: ; preds = %51
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%62 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %61)
%63 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%64 = call i32 @sprintf(i8* %63, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
store i32 %64, i32* %8
%65 = load i32, i32* %7
%66 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%67 = load i32, i32* %8
%68 = call i32 @write(i32 %65, i8* %66, i32 %67)
store i32 %68, i32* %8
%69 = load i32, i32* %8
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %60
%72 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%73 = call i32 @exit(i32 6)
unreachable
74: ; preds = %60
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
ret i32 0
}

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