#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*/ serverAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ write(int, char *, int);
int main(void) {
  int port;
  int listenSocket;
  int connectSocket;
  char c;
  struct sockaddr_in serverAddr;
  port = 4000;
  listenSocket = socket(AF_INET, SOCK_STREAM, 0);
  if (listenSocket < 0) {
    perror("socket()");
    exit(1);
  }
  memset(&serverAddr, 0, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  serverAddr.sin_port = htons(port);
  if (bind(listenSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) <
      0) {
    perror("bind()");
    close(listenSocket);
    exit(2);
  }
  if (listen(listenSocket, 512) < 0) {
    perror("listen()");
    close(listenSocket);
    exit(3);
  }
  while (1) {
    if ((connectSocket = accept(listenSocket, NULL, NULL)) < 0) {
      perror("accept()");
      close(connectSocket);
      exit(4);
    }
    if (read(connectSocket, &c, 1) < 0) {
      perror("read()");
      close(connectSocket);
      exit(5);
    }
    if (c == '0') {
      close(connectSocket);
      exit(0);
    }
    if (write(connectSocket, &c, 1) < 0) {
      perror("write()");
      close(connectSocket);
      exit(6);
    }
    close(connectSocket);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xfa0,-0x8(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1212 <main+0x52>
lea    0xdfd(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1283 <main+0xc3>
lea    0xd9d(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x200,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12b8 <main+0xf8>
lea    0xd6f(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
jmp    12bd <main+0xfd>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10c0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    12f6 <main+0x136>
lea    0xd3a(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    $0x4,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
jge    132f <main+0x16f>
lea    0xd0a(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    $0x5,%edi
call   10d0 <exit@plt>
movsbl -0x11(%rbp),%eax
cmp    $0x30,%eax
jne    134b <main+0x18b>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1384 <main+0x1c4>
lea    0xcbc(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    $0x6,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
jmp    12bd <main+0xfd>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e19(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    11fc <main+0x3c>
lea    0xe13(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0xfa0,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    126d <main+0xad>
lea    0xdb3(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    %r14d,%edi
mov    $0x200,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    129d <main+0xdd>
lea    0xd8a(%rip),%rdi
call   10b0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12cc <main+0x10c>
lea    0xd63(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x4,%edi
call   10d0 <exit@plt>
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
js     132a <main+0x16a>
cmpb   $0x30,0xf(%rsp)
je     1347 <main+0x187>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1355 <main+0x195>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12e0 <main+0x120>
jmp    12af <main+0xef>
lea    0xcf1(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x5,%edi
call   10d0 <exit@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xccd(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@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 = [9 x i8] c"socket()\00"
@INADDR_ANY = global i32 0
@.str.1 = [7 x i8] c"bind()\00"
@.str.2 = [9 x i8] c"listen()\00"
@.str.3 = [9 x i8] c"accept()\00"
@.str.4 = [7 x i8] c"read()\00"
@.str.5 = [8 x i8] c"write()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca %struct.sockaddr_in
store i32 0, i32* %1
store i32 4000, i32* %2
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%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 = load i32, i32* %2
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %3
%27 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %15
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
%34 = call i32 @exit(i32 2)
unreachable
35: ; preds = %15
%36 = load i32, i32* %3
%37 = call i64 @listen(i32 %36, i32 512)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
%43 = call i32 @exit(i32 3)
unreachable
44: ; preds = %35
br label %45
45: ; preds = %44, %80
%46 = load i32, i32* %3
%47 = call i32 @accept(i32 %46, i32* null, i32* null)
store i32 %47, i32* %4
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* %4
%52 = call i32 @close(i32 %51)
%53 = call i32 @exit(i32 4)
unreachable
54: ; preds = %45
%55 = load i32, i32* %4
%56 = call i64 @read(i32 %55, i8* %5, i32 1)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%60 = load i32, i32* %4
%61 = call i32 @close(i32 %60)
%62 = call i32 @exit(i32 5)
unreachable
63: ; preds = %54
%64 = load i8, i8* %5
%65 = sext i8 %64 to i32
%66 = icmp eq i32 %65, 48
br i1 %66, label %67, label %71
67: ; preds = %63
%68 = load i32, i32* %4
%69 = call i32 @close(i32 %68)
%70 = call i32 @exit(i32 0)
unreachable
71: ; preds = %63
%72 = load i32, i32* %4
%73 = call i64 @write(i32 %72, i8* %5, i32 1)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%77 = load i32, i32* %4
%78 = call i32 @close(i32 %77)
%79 = call i32 @exit(i32 6)
unreachable
80: ; preds = %71
%81 = load i32, i32* %4
%82 = call i32 @close(i32 %81)
br label %45
}

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