#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_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ client;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recvfrom(int, char *, int, int /*<<< orphan*/,
                            struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int s;
  struct sockaddr_in server, client;
  int c, l;
  s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s < 0) {
    printf("Eroare la crearea socketului...\n");
    return 1;
  }
  memset(&server, 0, sizeof(server));
  server.sin_port = htons(1234);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
    printf("Eroare la bind...\n");
    return 1;
  }
  l = sizeof(client);
  memset(&client, 0, sizeof(client));
  int old = -1;
  while (1) {
    char msg[100];
    recvfrom(s, msg, sizeof(msg), 0, (struct sockaddr *)&client, &l);
    int cur = atoi(msg);
    if (old != -1 && old + 1 != cur) {
      printf("Received out of order number %d, previous one was %d\n", cur,
             old);
    }
    old = cur;
    if (cur == 1000)
      old = -1;
  }
  close(s);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c2 <main+0x52>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <main+0x162>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    $0x4d2,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    122c <main+0xbc>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <main+0x162>
movl   $0xc,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
movl   $0xffffffff,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x64,%edx
xor    %ecx,%ecx
lea    -0x30(%rbp),%r9
call   1060 <recvfrom@plt>
lea    -0xa0(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
je     12ad <main+0x13d>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
cmp    -0xa4(%rbp),%eax
je     12ad <main+0x13d>
mov    -0xa4(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xa4(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x3e8,-0xa4(%rbp)
jne    12cd <main+0x15d>
movl   $0xffffffff,-0x34(%rbp)
jmp    124a <main+0xda>
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2e50(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1291 <main+0x111>
mov    %eax,%r13d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    $0x4d2,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     129a <main+0x11a>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    $0xffffffff,%r14d
lea    0x30(%rsp),%rbx
lea    0xc(%rsp),%r15
mov    $0xffffffff,%r12d
jmp    124d <main+0xcd>
cs nopw 0x0(%rax,%rax,1)
cmp    $0x3e8,%ebp
cmove  %r14d,%ebp
mov    %ebp,%r12d
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
lea    0x20(%rsp),%r8
mov    %r15,%r9
call   1070 <recvfrom@plt>
mov    %rbx,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    %r12d,%eax
inc    %eax
je     1240 <main+0xc0>
cmp    %ebp,%eax
je     1240 <main+0xc0>
lea    0xd7d(%rip),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1240 <main+0xc0>
lea    0xdb0(%rip),%rdi
jmp    12a1 <main+0x121>
lea    0xd95(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
@SOCK_DGRAM = global i32 0
@.str = [33 x i8] c"Eroare la crearea socketului...\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [19 x i8] c"Eroare la bind...\0A\00"
@.str.2 = [54 x i8] c"Received out of order number %d, previous one was %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca i32
store i32 0, i32* %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %58
17: ; preds = %0
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = call i32 @htons(i32 1234)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_in* %3 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 %32
30: ; preds = %17
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %58
32: ; preds = %17
store i32 12, i32* %6
%33 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
store i32 -1, i32* %7
br label %34
34: ; preds = %32, %57
%35 = load i32, i32* %2
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i32 @recvfrom(i32 %35, i8* %36, i32 100, i32 0, %struct.sockaddr* %37, i32* %6)
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %9
%41 = load i32, i32* %7
%42 = icmp ne i32 %41, -1
br i1 %42, label %43, label %52
43: ; preds = %34
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
%46 = load i32, i32* %9
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32, i32* %9
%50 = load i32, i32* %7
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %50)
br label %52
52: ; preds = %48, %43, %34
%53 = load i32, i32* %9
store i32 %53, i32* %7
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, 1000
br i1 %55, label %56, label %57
56: ; preds = %52
store i32 -1, i32* %7
br label %57
57: ; preds = %56, %52
br label %34
58: ; preds = %30, %15
%59 = load i32, i32* %1
ret i32 %59
}

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
@INADDR_ANY = global i32 0
@.str.2 = [54 x i8] c"Received out of order number %d, previous one was %d\0A\00"
@str = [18 x i8] c"Eroare la bind...\00"
@str.3 = [32 x i8] c"Eroare la crearea socketului...\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.3, i64 0, i64 0))
br label %44
14: ; preds = %0
%15 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%16 = call i32 @htons(i32 1234)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%23 = call i64 @bind(i32 %10, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %44
27: ; preds = %14
store i32 12, i32* %3
%28 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%30 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %31
31: ; preds = %41, %27
%32 = phi i32 [ -1, %27 ], [ %43, %41 ]
%33 = call i32 @recvfrom(i32 %10, i8* %29, i32 100, i32 0, %struct.sockaddr* %30, i32* %3)
%34 = call i32 @atoi(i8* %29)
%35 = icmp eq i32 %32, -1
%36 = add nsw i32 %32, 1
%37 = icmp eq i32 %36, %34
%38 = select i1 %35, i1 true, i1 %37
br i1 %38, label %41, label %39
39: ; preds = %31
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 %32)
br label %41
41: ; preds = %39, %31
%42 = icmp eq i32 %34, 1000
%43 = select i1 %42, i32 -1, i32 %34
br label %31
44: ; preds = %25, %12
ret i32 1
}