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

/* Type definitions */

/* Variables and functions */
int create_inet_server_socket(char *, char *, int, int, int /*<<< orphan*/);
int destroy_inet_socket(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom_inet_dgram_socket(int, char *, int, char *, int, char *, int,
                               int /*<<< orphan*/, int);
int sendto_inet_dgram_socket(int, char *, int, char *, char *,
                             int /*<<< orphan*/);
int main(void) {
  int sfd, bytes, ret;
  char src_host[128], src_service[7], buf[16];
  src_host[127] = 0;
  src_service[6] = 0;
  sfd = create_inet_server_socket("0.0.0.0", "1234", 2, 3, 0);
  if (-1 == sfd) {
    perror("couldn't create server\n");
    exit(1);
  }
  printf("Socket up and running\n");
  while (1) {
    memset(buf, 0, 16);
    ret = bytes = recvfrom_inet_dgram_socket(sfd, buf, 15, src_host, 127,
                                             src_service, 6, 0, 1);
    if (ret < 0) {
      perror(0);
      exit(1);
    }
    ret = sendto_inet_dgram_socket(sfd, buf, bytes, src_host, src_service, 0);
    if (ret < 0) {
      perror(0);
      exit(1);
    }
    printf("Connection from %s port %s: %s (%i)\n", src_host, src_service, buf,
           bytes);
    printf("Connection processed\n");
  }
  ret = destroy_inet_socket(sfd);
  if (ret < 0) {
    perror(0);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x11(%rbp)
movb   $0x0,-0x91(%rbp)
lea    0xe6c(%rip),%rdi
lea    0xe6d(%rip),%rsi
mov    $0x2,%edx
mov    $0x3,%ecx
xor    %r8d,%r8d
call   1060 <create_inet_server_socket@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11d4 <main+0x64>
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0xb0(%rbp),%rsi
lea    -0x90(%rbp),%rcx
lea    -0x97(%rbp),%r9
mov    $0xf,%edx
mov    $0x7f,%r8d
xor    %eax,%eax
movl   $0x6,(%rsp)
movl   $0x0,0x8(%rsp)
movl   $0x1,0x10(%rsp)
call   1050 <recvfrom_inet_dgram_socket@plt>
mov    %eax,-0xc(%rbp)
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1259 <main+0xe9>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    -0x90(%rbp),%rcx
lea    -0x97(%rbp),%r8
xor    %r9d,%r9d
call   1080 <sendto_inet_dgram_socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    129c <main+0x12c>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x90(%rbp),%rsi
lea    -0x97(%rbp),%rdx
lea    -0xb0(%rbp),%rcx
mov    -0xc(%rbp),%r8d
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e2 <main+0x72>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
movb   $0x0,0x9f(%rsp)
movb   $0x0,0xf(%rsp)
lea    0xe5b(%rip),%rdi
lea    0xe5c(%rip),%rsi
mov    $0x2,%edx
mov    $0x3,%ecx
xor    %r8d,%r8d
call   1070 <create_inet_server_socket@plt>
cmp    $0xffffffff,%eax
jne    11cf <main+0x4f>
lea    0xe43(%rip),%rdi
jmp    12cf <main+0x14f>
mov    %eax,%r14d
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
sub    $0x8,%rsp
lea    0x28(%rsp),%rcx
lea    0x11(%rsp),%r9
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xf,%edx
mov    $0x7f,%r8d
push   $0x1
push   $0x0
push   $0x6
call   1060 <recvfrom_inet_dgram_socket@plt>
add    $0x20,%rsp
test   %eax,%eax
js     12cd <main+0x14d>
mov    %eax,%r12d
lea    0x10(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0x9(%rsp),%rbp
lea    0xe1f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
mov    %r15,%rcx
mov    %rbp,%r8
xor    %r9d,%r9d
call   1090 <sendto_inet_dgram_socket@plt>
test   %eax,%eax
js     12cd <main+0x14d>
lea    0xdb3(%rip),%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
mov    %r12d,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
sub    $0x8,%rsp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xf,%edx
mov    %r15,%rcx
mov    $0x7f,%r8d
mov    %rbp,%r9
push   $0x1
push   $0x0
push   $0x6
call   1060 <recvfrom_inet_dgram_socket@plt>
add    $0x20,%rsp
mov    %eax,%r12d
test   %eax,%eax
jns    1250 <main+0xd0>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"0.0.0.0\00"
@.str.1 = [5 x i8] c"1234\00"
@.str.2 = [24 x i8] c"couldn't create server\0A\00"
@.str.3 = [23 x i8] c"Socket up and running\0A\00"
@.str.4 = [37 x i8] c"Connection from %s port %s: %s (%i)\0A\00"
@.str.5 = [22 x i8] c"Connection processed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca [7 x i8]
%7 = alloca [16 x i8]
store i32 0, i32* %1
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 127
store i8 0, i8* %8
%9 = getelementptr inbounds [7 x i8], [7 x i8]* %6, i64 0, i64 6
store i8 0, i8* %9
%10 = call i32 @create_inet_server_socket(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 3, i32 0)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %43
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 16)
%21 = load i32, i32* %2
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%24 = getelementptr inbounds [7 x i8], [7 x i8]* %6, i64 0, i64 0
%25 = call i32 @recvfrom_inet_dgram_socket(i32 %21, i8* %22, i32 15, i8* %23, i32 127, i8* %24, i32 6, i32 0, i32 1)
store i32 %25, i32* %3
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %18
%29 = call i32 @perror(i8* null)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %18
%32 = load i32, i32* %2
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%34 = load i32, i32* %3
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%36 = getelementptr inbounds [7 x i8], [7 x i8]* %6, i64 0, i64 0
%37 = call i32 @sendto_inet_dgram_socket(i32 %32, i8* %33, i32 %34, i8* %35, i8* %36, i32 0)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %31
%41 = call i32 @perror(i8* null)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %31
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%45 = getelementptr inbounds [7 x i8], [7 x i8]* %6, i64 0, i64 0
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%47 = load i32, i32* %3
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %44, i8* %45, i8* %46, i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
br label %18
}

LLVM IR (-O2):
@.str = [8 x i8] c"0.0.0.0\00"
@.str.1 = [5 x i8] c"1234\00"
@.str.2 = [24 x i8] c"couldn't create server\0A\00"
@.str.4 = [37 x i8] c"Connection from %s port %s: %s (%i)\0A\00"
@str = [22 x i8] c"Socket up and running\00"
@str.6 = [21 x i8] c"Connection processed\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = alloca [7 x i8]
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 127
store i8 0, i8* %7
%8 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 6
store i8 0, i8* %8
%9 = tail call i32 @create_inet_server_socket(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 3, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%16 = call i32 @memset(i8* %6, i32 0, i32 16)
%17 = call i32 @recvfrom_inet_dgram_socket(i32 %9, i8* %6, i32 15, i8* %4, i32 127, i8* %5, i32 6, i32 0, i32 1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %29, %14
%20 = call i32 @perror(i8* null)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14, %29
%23 = phi i32 [ %33, %29 ], [ %17, %14 ]
%24 = call i32 @sendto_inet_dgram_socket(i32 %9, i8* %6, i32 %23, i8* %4, i8* %5, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* null)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %4, i8* %5, i8* %6, i32 %23)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.6, i64 0, i64 0))
%32 = call i32 @memset(i8* %6, i32 0, i32 16)
%33 = call i32 @recvfrom_inet_dgram_socket(i32 %9, i8* %6, i32 15, i8* %4, i32 127, i8* %5, i32 6, i32 0, i32 1)
%34 = icmp slt i32 %33, 0
br i1 %34, label %19, label %22
}