#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 */
typedef int uint16_t;
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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SHUT_RD;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int readn(int, char *, size_t *);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, int);
int main(void) {
  int sockfd, newsockfd;
  uint16_t portno;
  unsigned int clilen;
  char buffer[256];
  struct sockaddr_in serv_addr, cli_addr;
  ssize_t n;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("ERROR opening socket");
    exit(1);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = 5001;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR on binding");
    exit(1);
  }
  listen(sockfd, 5);
  clilen = sizeof(cli_addr);
  newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
  shutdown(sockfd, SHUT_RDWR);
  close(sockfd);
  if (newsockfd < 0) {
    perror("ERROR on accept");
    exit(1);
  }
  size_t limit = 255;
  if (-1 == readn(newsockfd, buffer, &limit)) {
    fprintf(stderr, "Was read %ld bytes, but recv() failed:\n\t ", limit);
    perror("");
    exit(1);
  }
  shutdown(newsockfd, SHUT_RD);
  printf("Here is the message: %s\n", buffer);
  n = write(newsockfd, "I got your message", 18);
  shutdown(newsockfd, SHUT_WR);
  if (n < 0) {
    perror("ERROR writing to socket");
    exit(1);
  }
  close(newsockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    122e <main+0x4e>
lea    0xde1(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    -0x130(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
movl   $0x1389,-0x10(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x10(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12a6 <main+0xc6>
lea    0xd7e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
cmpl   $0x0,-0xc(%rbp)
jge    1309 <main+0x129>
lea    0xd2c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
movq   $0xff,-0x150(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x150(%rbp),%rdx
call   1070 <readn@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    136b <main+0x18b>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
mov    -0x150(%rbp),%rdx
lea    0xce6(%rip),%rsi
call   1090 <fprintf@plt>
lea    0xd1c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2c5b(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
lea    -0x120(%rbp),%rsi
lea    0xcd6(%rip),%rdi
call   1060 <printf@plt>
mov    -0xc(%rbp),%edi
lea    0xce0(%rip),%rsi
mov    $0x12,%edx
call   1030 <write@plt>
mov    %rax,-0x148(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2c24(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
cmpq   $0x0,-0x148(%rbp)
jge    13df <main+0x1ff>
lea    0xcbc(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x130,%rsp
mov    0x2dee(%rip),%r14
mov    (%r14),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1321 <main+0x141>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x1389,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     132a <main+0x14a>
mov    %ebp,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x20(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   10e0 <accept@plt>
mov    %eax,%ebx
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <shutdown@plt>
mov    %ebp,%edi
call   1080 <close@plt>
test   %ebx,%ebx
js     1333 <main+0x153>
movq   $0xff,0x8(%rsp)
lea    0x30(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1070 <readn@plt>
cmp    $0xffffffff,%eax
je     133c <main+0x15c>
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
lea    0xd89(%rip),%rdi
lea    0x30(%rsp),%rsi
call   1060 <printf@plt>
lea    0xd91(%rip),%rsi
mov    %ebx,%edi
mov    $0x12,%edx
call   1030 <write@plt>
mov    %rax,%rbp
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <shutdown@plt>
test   %rbp,%rbp
js     135f <main+0x17f>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcd8(%rip),%rdi
jmp    1366 <main+0x186>
lea    0xce4(%rip),%rdi
jmp    1366 <main+0x186>
lea    0xcec(%rip),%rdi
jmp    1366 <main+0x186>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rsp),%rdx
lea    0xce5(%rip),%rsi
call   1090 <fprintf@plt>
lea    0xd1b(%rip),%rdi
jmp    1366 <main+0x186>
lea    0xd26(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10f0 <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 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@SHUT_RDWR = global i32 0
@.str.2 = [16 x i8] c"ERROR on accept\00"
@stderr = global i32 0
@.str.3 = [42 x i8] c"Was read %ld bytes, but recv() failed:\0A\09 \00"
@.str.4 = [1 x i8] zeroinitializer
@SHUT_RD = global i32 0
@.str.5 = [25 x i8] c"Here is the message: %s\0A\00"
@.str.6 = [19 x i8] c"I got your message\00"
@SHUT_WR = global i32 0
@.str.7 = [24 x i8] c"ERROR writing to socket\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %1
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = bitcast %struct.sockaddr_in* %7 to i8*
%21 = call i32 @bzero(i8* %20, i32 12)
store i32 5001, i32* %4
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %4
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %2
%31 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %19
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %19
%38 = load i32, i32* %2
%39 = call i32 @listen(i32 %38, i32 5)
store i32 12, i32* %5
%40 = load i32, i32* %2
%41 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%42 = call i32 @accept(i32 %40, %struct.sockaddr* %41, i32* %5)
store i32 %42, i32* %3
%43 = load i32, i32* %2
%44 = load i32, i32* @SHUT_RDWR
%45 = call i32 @shutdown(i32 %43, i32 %44)
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %3
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %37
%51 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %37
store i64 255, i64* %10
%54 = load i32, i32* %3
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%56 = call i32 @readn(i32 %54, i8* %55, i64* %10)
%57 = icmp eq i32 -1, %56
br i1 %57, label %58, label %64
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = load i64, i64* %10
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %60)
%62 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %53
%65 = load i32, i32* %3
%66 = load i32, i32* @SHUT_RD
%67 = call i32 @shutdown(i32 %65, i32 %66)
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%69 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %68)
%70 = load i32, i32* %3
%71 = call i64 @write(i32 %70, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 18)
store i64 %71, i64* %9
%72 = load i32, i32* %3
%73 = load i32, i32* @SHUT_WR
%74 = call i32 @shutdown(i32 %72, i32 %73)
%75 = load i64, i64* %9
%76 = icmp slt i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %64
%78 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %64
%81 = load i32, i32* %3
%82 = call i32 @close(i32 %81)
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 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@SHUT_RDWR = global i32 0
@.str.2 = [16 x i8] c"ERROR on accept\00"
@stderr = global i32 0
@.str.3 = [42 x i8] c"Was read %ld bytes, but recv() failed:\0A\09 \00"
@.str.4 = [1 x i8] zeroinitializer
@SHUT_RD = global i32 0
@.str.5 = [25 x i8] c"Here is the message: %s\0A\00"
@.str.6 = [19 x i8] c"I got your message\00"
@SHUT_WR = global i32 0
@.str.7 = [24 x i8] c"ERROR writing to socket\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i64
%6 = bitcast i32* %1 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = call i32 @bzero(i8* %8, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 5001)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %12, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %17
%32 = call i32 @listen(i32 %12, i32 5)
store i32 12, i32* %1
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%34 = call i32 @accept(i32 %12, %struct.sockaddr* %33, i32* %1)
%35 = load i32, i32* @SHUT_RDWR
%36 = call i32 @shutdown(i32 %12, i32 %35)
%37 = call i32 @close(i32 %12)
%38 = icmp slt i32 %34, 0
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %31
%43 = bitcast i64* %5 to i8*
store i64 255, i64* %5
%44 = call i32 @readn(i32 %34, i8* %7, i64* %5)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = load i64, i64* %5
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i64 %48)
%50 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %42
%53 = load i32, i32* @SHUT_RD
%54 = call i32 @shutdown(i32 %34, i32 %53)
%55 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %7)
%56 = call i64 @write(i32 %34, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 18)
%57 = load i32, i32* @SHUT_WR
%58 = call i32 @shutdown(i32 %34, i32 %57)
%59 = icmp slt i64 %56, 0
br i1 %59, label %60, label %63
60: ; preds = %52
%61 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %52
%64 = call i32 @close(i32 %34)
ret i32 0
}