#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*/ server;
typedef int /*<<< orphan*/ client;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
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 *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int nargs, char **argv) {
  int sd;
  socklen_t addrlen_client;
  struct sockaddr_in client, server;
  char buf[1024];
  sd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sd < 0) {
    perror("beim Oeffnen des UDP-Sockets");
    exit(1);
  }
  server.sin_family = AF_INET;
  server.sin_port = htons(7777);
  server.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("beim Anbinden des UDP-Sockets");
    close(sd);
    exit(1);
  }
  addrlen_client = sizeof(client);
  for (;;) {
    bzero(buf, sizeof(buf));
    int bytes_recv = 0;
    if ((bytes_recv = recvfrom(sd, buf, sizeof(buf), 0,
                               (struct sockaddr *)&client, &addrlen_client)) <
        0) {
      perror("beim Empfangen der Daten");
      close(sd);
      exit(1);
    }
    fprintf(stdout, "%d Bytes empfangen\n", bytes_recv);
    if (sendto(sd, buf, bytes_recv, 0, (struct sockaddr *)&client,
               sizeof(client)) < 0) {
      perror("beim Senden der Daten");
      close(sd);
      exit(1);
    }
  }
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1205 <main+0x55>
lea    0xe0a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x1e61,%edi
call   1030 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1268 <main+0xb8>
lea    0xdcc(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0xc,-0x18(%rbp)
lea    -0x440(%rbp),%rdi
mov    $0x400,%esi
call   10b0 <bzero@plt>
movl   $0x0,-0x444(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x18(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x444(%rbp)
cmp    $0x0,%eax
jge    12d5 <main+0x125>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
mov    -0x444(%rbp),%edx
lea    0xd69(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x444(%rbp),%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1050 <sendto@plt>
cmp    $0x0,%rax
jge    1339 <main+0x189>
lea    0xd46(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    126f <main+0xbf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    0x2e20(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jns    11f4 <main+0x44>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    %eax,%r13d
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x1e61,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    1240 <main+0x90>
lea    0xde2(%rip),%rdi
jmp    12c7 <main+0x117>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r14
mov    0x2d6f(%rip),%r15
lea    0xdf4(%rip),%r12
mov    %rbx,%rdi
mov    $0x400,%esi
call   10b0 <bzero@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r14,%r8
lea    0xc(%rsp),%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
js     12c0 <main+0x110>
mov    %eax,%ebp
mov    (%r15),%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1050 <sendto@plt>
test   %rax,%rax
jns    1260 <main+0xb0>
lea    0xdaa(%rip),%rdi
jmp    12c7 <main+0x117>
lea    0xd74(%rip),%rdi
call   10a0 <perror@plt>
mov    %r13d,%edi
call   1060 <close@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_DGRAM = global i32 0
@.str = [29 x i8] c"beim Oeffnen des UDP-Sockets\00"
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"beim Anbinden des UDP-Sockets\00"
@.str.2 = [25 x i8] c"beim Empfangen der Daten\00"
@stdout = global i32 0
@.str.3 = [20 x i8] c"%d Bytes empfangen\0A\00"
@.str.4 = [22 x i8] c"beim Senden der Daten\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca [1024 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 7777)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
store i32 12, i32* %7
br label %39
39: ; preds = %67, %38
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%41 = call i32 @bzero(i8* %40, i32 1024)
store i32 0, i32* %11
%42 = load i32, i32* %6
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%44 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%45 = call i32 @recvfrom(i32 %42, i8* %43, i32 1024, i32 0, %struct.sockaddr* %44, i32* %7)
store i32 %45, i32* %11
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %39
%53 = load i32, i32* @stdout
%54 = load i32, i32* %11
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %6
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%58 = load i32, i32* %11
%59 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%60 = call i64 @sendto(i32 %56, i8* %57, i32 %58, i32 0, %struct.sockaddr* %59, i32 12)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %52
%63 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %52
br label %39
}

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_DGRAM = global i32 0
@.str = [29 x i8] c"beim Oeffnen des UDP-Sockets\00"
@INADDR_ANY = global i32 0
@.str.1 = [30 x i8] c"beim Anbinden des UDP-Sockets\00"
@.str.2 = [25 x i8] c"beim Empfangen der Daten\00"
@stdout = global i32 0
@.str.3 = [20 x i8] c"%d Bytes empfangen\0A\00"
@.str.4 = [22 x i8] c"beim Senden der Daten\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [1024 x i8]
%7 = bitcast i32* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 7777)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = tail call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i64 @bind(i32 %13, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @close(i32 %13)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %18
store i32 12, i32* %3
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %35
35: ; preds = %43, %33
%36 = call i32 @bzero(i8* %10, i32 1024)
%37 = call i32 @recvfrom(i32 %13, i8* %10, i32 1024, i32 0, %struct.sockaddr* %34, i32* %3)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @close(i32 %13)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %35
%44 = load i32, i32* @stdout
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %37)
%46 = call i64 @sendto(i32 %13, i8* %10, i32 %37, i32 0, %struct.sockaddr* %34, i32 12)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %35
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @close(i32 %13)
%51 = call i32 @exit(i32 1)
unreachable
}