#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;
};
struct servent {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int write(int, char *, int);
int main(int nargs, char **argv) {
  int sd;
  struct sockaddr_in server, client;
  struct servent *sp, *getservbyname();
  sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("beim Oeffnen des TCP-Sockets");
    exit(1);
  }
  bzero((char *)&server, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY);
  server.sin_port = htons(7777);
  if (bind(sd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("beim Anbinden des TCP-Sockets");
    exit(1);
  }
  listen(sd, 5);
  for (;;) {
    int nsd;
    socklen_t addrlen_client = sizeof(client);
    nsd = accept(sd, (struct sockaddr *)&client, &addrlen_client);
    if (nsd < 0) {
      perror("beim Lesen vom Socket ");
      continue;
    }
    if (fork() == 0) {
      char buffer[256];
      bzero(buffer, 256);
      int bytes_recv = read(nsd, buffer, 255);
      if (bytes_recv < 0) {
        perror("Beim Lesen der Daten");
        close(nsd);
        exit(1);
      }
      fprintf(stdout, "%d Bytes empfangen\n", bytes_recv);
      bytes_recv = write(nsd, buffer, bytes_recv);
      if (bytes_recv < 0) {
        perror("beim Schreiben der Daten");
        close(nsd);
        exit(1);
      }
      close(nsd);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1235 <main+0x55>
lea    0xdda(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1e61,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    129e <main+0xbe>
lea    0xd8e(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12e0 <main+0x100>
lea    0xd65(%rip),%rdi
call   10b0 <perror@plt>
jmp    12ab <main+0xcb>
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
jne    13b0 <main+0x1d0>
lea    -0x140(%rbp),%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
mov    -0x3c(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    1347 <main+0x167>
lea    0xd22(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
mov    -0x144(%rbp),%edx
lea    0xd0a(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x3c(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    -0x144(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    13a8 <main+0x1c8>
lea    0xcea(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x3c(%rbp),%edi
call   1060 <close@plt>
jmp    12ab <main+0xcb>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    0x2df0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
jns    1218 <main+0x38>
lea    0xdea(%rip),%rdi
jmp    126e <main+0x8e>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x1e61,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    127d <main+0x9d>
lea    0xdaf(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x30(%rsp),%r13
jmp    12b0 <main+0xd0>
lea    0xd9a(%rip),%rdi
call   10b0 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
test   %eax,%eax
js     129a <main+0xba>
mov    %eax,%r12d
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jne    12b0 <main+0xd0>
mov    %r13,%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     132d <main+0x14d>
mov    %eax,%ebp
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
mov    %ebp,%edx
call   1080 <fprintf@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1336 <main+0x156>
mov    %r12d,%edi
call   1060 <close@plt>
jmp    12b0 <main+0xd0>
lea    0xd1e(%rip),%rdi
jmp    133d <main+0x15d>
lea    0xd3e(%rip),%rdi
call   10b0 <perror@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.servent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [29 x i8] c"beim Oeffnen des TCP-Sockets\00"
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"beim Anbinden des TCP-Sockets\00"
@.str.2 = [23 x i8] c"beim Lesen vom Socket \00"
@.str.3 = [21 x i8] c"Beim Lesen der Daten\00"
@stdout = global i32 0
@.str.4 = [20 x i8] c"%d Bytes empfangen\0A\00"
@.str.5 = [25 x i8] c"beim Schreiben der Daten\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.servent*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [256 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = bitcast %struct.sockaddr_in* %7 to i8*
%24 = call i32 @bzero(i8* %23, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = call i32 @htons(i32 7777)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %22
%38 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %22
%41 = load i32, i32* %6
%42 = call i32 @listen(i32 %41, i32 5)
br label %43
43: ; preds = %85, %49, %40
store i32 12, i32* %11
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %11)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %43
51: ; preds = %43
%52 = call i64 (...) @fork()
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %85
54: ; preds = %51
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%56 = call i32 @bzero(i8* %55, i32 256)
%57 = load i32, i32* %10
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 255)
store i32 %59, i32* %13
%60 = load i32, i32* %13
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %54
%63 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%64 = load i32, i32* %10
%65 = call i32 @close(i32 %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %54
%68 = load i32, i32* @stdout
%69 = load i32, i32* %13
%70 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %69)
%71 = load i32, i32* %10
%72 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%73 = load i32, i32* %13
%74 = call i32 @write(i32 %71, i8* %72, i32 %73)
store i32 %74, i32* %13
%75 = load i32, i32* %13
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %82
77: ; preds = %67
%78 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%79 = load i32, i32* %10
%80 = call i32 @close(i32 %79)
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %67
%83 = load i32, i32* %10
%84 = call i32 @close(i32 %83)
br label %85
85: ; preds = %82, %51
br label %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.servent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [29 x i8] c"beim Oeffnen des TCP-Sockets\00"
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"beim Anbinden des TCP-Sockets\00"
@.str.2 = [23 x i8] c"beim Lesen vom Socket \00"
@.str.3 = [21 x i8] c"Beim Lesen der Daten\00"
@stdout = global i32 0
@.str.4 = [20 x i8] c"%d Bytes empfangen\0A\00"
@.str.5 = [25 x i8] c"beim Schreiben der Daten\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%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 slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = call i32 @bzero(i8* %7, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = tail call i32 @htonl(i32 %20)
%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 7777)
%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 %11, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %16
%29 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %16
%32 = call i32 @listen(i32 %11, i32 5)
%33 = bitcast i32* %5 to i8*
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
br label %36
36: ; preds = %63, %31
store i32 12, i32* %5
%37 = call i32 @accept(i32 %11, %struct.sockaddr* %34, i32* %5)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %63
41: ; preds = %36
%42 = call i64 (...) @fork()
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %63
44: ; preds = %41
%45 = call i32 @bzero(i8* %35, i32 256)
%46 = call i32 @read(i32 %37, i8* %35, i32 255)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @close(i32 %37)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %44
%53 = load i32, i32* @stdout
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %46)
%55 = call i32 @write(i32 %37, i8* %35, i32 %46)
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%59 = call i32 @close(i32 %37)
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %52
%62 = call i32 @close(i32 %37)
br label %63
63: ; preds = %41, %61, %39
br label %36
}