#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 /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ puts(char *);
unsigned int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                      unsigned int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  int sockfd;
  unsigned clilen, n;
  char line[11];
  struct sockaddr_in servaddr, cliaddr;
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(5001);
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
    puts("Socket fail");
    return -1;
  }
  if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    puts("Binding fail");
    close(sockfd);
    return -1;
  }
  for (;;) {
    clilen = sizeof(cliaddr);
    if ((n = recvfrom(sockfd, line, 11, 0, (struct sockaddr *)&cliaddr,
                      &clilen)) < 0) {
      puts("Error receiving");
      close(sockfd);
      return -1;
    }
    puts(line);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x38(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    120b <main+0x8b>
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <main+0x127>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1246 <main+0xc6>
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <main+0x127>
jmp    124b <main+0xcb>
movl   $0xc,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x27(%rbp),%rsi
lea    -0x48(%rbp),%r8
mov    $0xb,%edx
xor    %ecx,%ecx
lea    -0x18(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jae    1299 <main+0x119>
lea    0xd99(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a7 <main+0x127>
lea    -0x27(%rbp),%rdi
call   1030 <puts@plt>
jmp    124b <main+0xcb>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0x10(%rsp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1239 <main+0xb9>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1247 <main+0xc7>
lea    0x1d(%rsp),%rbx
lea    0x28(%rsp),%r14
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xb,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1070 <recvfrom@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
jmp    1210 <main+0x90>
lea    0xdc0(%rip),%rdi
call   1030 <puts@plt>
jmp    125a <main+0xda>
lea    0xdbe(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [12 x i8] c"Socket fail\00"
@.str.1 = [13 x i8] c"Binding fail\00"
@.str.2 = [16 x i8] c"Error receiving\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 i32
%9 = alloca [11 x i8]
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 5001)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
27: ; preds = %2
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %3
br label %50
36: ; preds = %27
br label %37
37: ; preds = %47, %36
store i32 12, i32* %7
%38 = load i32, i32* %6
%39 = getelementptr inbounds [11 x i8], [11 x i8]* %9, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%41 = call i32 @recvfrom(i32 %38, i8* %39, i32 11, i32 0, %struct.sockaddr* %40, i32* %7)
store i32 %41, i32* %8
%42 = icmp ult i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %50
47: ; preds = %37
%48 = getelementptr inbounds [11 x i8], [11 x i8]* %9, i64 0, i64 0
%49 = call i32 @puts(i8* %48)
br label %37
50: ; preds = %43, %32, %25
%51 = load i32, i32* %3
ret i32 %51
}

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
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [12 x i8] c"Socket fail\00"
@.str.1 = [13 x i8] c"Binding fail\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [11 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 5001)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @PF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %37
25: ; preds = %2
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i64 @bind(i32 %21, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %34
31: ; preds = %25
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @close(i32 %21)
br label %37
34: ; preds = %29, %34
store i32 12, i32* %3
%35 = call i32 @recvfrom(i32 %21, i8* %8, i32 11, i32 0, %struct.sockaddr* %30, i32* %3)
%36 = call i32 @puts(i8* %8)
br label %34
37: ; preds = %31, %23
ret i32 -1
}