#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 */
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                          struct sockaddr *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int sscanf(char *, char *, int *, int *);
int /*<<< orphan*/ write(int, char *, int);
void serve1(int socket_fd, struct sockaddr *to, socklen_t to_len) {
  char buf[128];
  memset(buf, 0, sizeof(buf));
  ssize_t ret = recv(socket_fd, buf, sizeof(buf) - 1, 0);
  if (ret == -1) {
    if (errno == EAGAIN) {
      sendto(socket_fd, "", 0, 0, to, to_len);
    }
    return;
  }
  int fdnum = -1, val = -1, len;
  if (sscanf(buf, "%d%d", &fdnum, &val) != 2)
    return;
  int fd_allowed = 0;
  if (fdnum == 1)
    fd_allowed = 1;
  if (fdnum >= 3 && fdnum <= 1000)
    fd_allowed = 1;
  if (!fd_allowed)
    return;
  len = snprintf(buf, sizeof(buf), "%d\n", val);
  if (len <= 0 || len > sizeof(buf))
    return;
  write(fdnum, buf, len);
}


x86 assembly (clang, -O0):
<serve1>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x7f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
jne    11eb <serve1+0x8b>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <serve1+0x86>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%r8
mov    -0x14(%rbp),%r9d
lea    0xe2b(%rip),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1070 <sendto@plt>
jmp    12e9 <serve1+0x189>
movl   $0xffffffff,-0xa8(%rbp)
ff ff ff
movl   $0xffffffff,-0xac(%rbp)
ff ff ff
lea    -0xa0(%rbp),%rdi
lea    0xdf3(%rip),%rsi
lea    -0xa8(%rbp),%rdx
lea    -0xac(%rbp),%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
je     122e <serve1+0xce>
jmp    12e9 <serve1+0x189>
movl   $0x0,-0xb4(%rbp)
cmpl   $0x1,-0xa8(%rbp)
jne    124f <serve1+0xef>
movl   $0x1,-0xb4(%rbp)
cmpl   $0x3,-0xa8(%rbp)
jl     1276 <serve1+0x116>
cmpl   $0x3e8,-0xa8(%rbp)
jg     1276 <serve1+0x116>
movl   $0x1,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jne    1288 <serve1+0x128>
jmp    12e9 <serve1+0x189>
lea    -0xa0(%rbp),%rdi
mov    -0xac(%rbp),%ecx
mov    $0x80,%esi
lea    0xd64(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0xb0(%rbp)
cmpl   $0x0,-0xb0(%rbp)
jle    12cc <serve1+0x16c>
movslq -0xb0(%rbp),%rax
cmp    $0x80,%rax
jbe    12d1 <serve1+0x171>
jmp    12e9 <serve1+0x189>
mov    -0xa8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0xb0(%rbp),%edx
call   1040 <write@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve1>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x98,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7f,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1219 <serve1+0xb9>
movl   $0xffffffff,0x8(%rsp)
ff
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xe4a(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jne    1247 <serve1+0xe7>
mov    0x8(%rsp),%eax
cmp    $0x1,%eax
je     11e2 <serve1+0x82>
add    $0xfffffffd,%eax
cmp    $0x3e5,%eax
ja     1247 <serve1+0xe7>
mov    0xc(%rsp),%ecx
lea    0xe18(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
call   1050 <snprintf@plt>
lea    -0x81(%rax),%ecx
cmp    $0xffffff80,%ecx
jb     1247 <serve1+0xe7>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
jmp    1247 <serve1+0xe7>
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    0x2da6(%rip),%rcx
cmp    (%rcx),%rax
jne    1247 <serve1+0xe7>
lea    0xdd2(%rip),%rsi
mov    %ebp,%edi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r14d,%r9d
call   1070 <sendto@plt>
add    $0x98,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [5 x i8] c"%d%d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define void @serve1(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
store i32 %2, i32* %6
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 128)
%15 = load i32, i32* %4
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%17 = call i32 @recv(i32 %15, i8* %16, i32 127, i32 0)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %30
20: ; preds = %3
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* %4
%26 = load %struct.sockaddr*, %struct.sockaddr** %5
%27 = load i32, i32* %6
%28 = call i32 @sendto(i32 %25, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 0, i32 0, %struct.sockaddr* %26, i32 %27)
br label %29
29: ; preds = %24, %20
br label %66
30: ; preds = %3
store i32 -1, i32* %9
store i32 -1, i32* %10
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%32 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %9, i32* %10)
%33 = icmp ne i32 %32, 2
br i1 %33, label %34, label %35
34: ; preds = %30
br label %66
35: ; preds = %30
store i32 0, i32* %12
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 1, i32* %12
br label %39
39: ; preds = %38, %35
%40 = load i32, i32* %9
%41 = icmp sge i32 %40, 3
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i32, i32* %9
%44 = icmp sle i32 %43, 1000
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 1, i32* %12
br label %46
46: ; preds = %45, %42, %39
%47 = load i32, i32* %12
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %46
br label %66
50: ; preds = %46
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%52 = load i32, i32* %10
%53 = call i32 @snprintf(i8* %51, i32 128, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %52)
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = icmp sle i32 %54, 0
br i1 %55, label %60, label %56
56: ; preds = %50
%57 = load i32, i32* %11
%58 = sext i32 %57 to i64
%59 = icmp ugt i64 %58, 128
br i1 %59, label %60, label %61
60: ; preds = %56, %50
br label %66
61: ; preds = %56
%62 = load i32, i32* %9
%63 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%64 = load i32, i32* %11
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
br label %66
66: ; preds = %61, %60, %49, %34, %29
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [5 x i8] c"%d%d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
define void @serve1(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca [128 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 128)
%9 = call i32 @recv(i32 %0, i8* %7, i32 127, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %37
15: ; preds = %11
%16 = call i32 @sendto(i32 %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i32 0, i32 0, %struct.sockaddr* %1, i32 %2)
br label %37
17: ; preds = %3
%18 = bitcast i32* %5 to i8*
store i32 -1, i32* %5
%19 = bitcast i32* %6 to i8*
store i32 -1, i32* %6
%20 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32* %5, i32* %6)
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, 1
%25 = add i32 %23, -3
%26 = icmp ult i32 %25, 998
%27 = or i1 %24, %26
br i1 %27, label %28, label %36
28: ; preds = %22
%29 = load i32, i32* %6
%30 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = add i32 %30, -129
%32 = icmp ult i32 %31, -128
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i32, i32* %5
%35 = call i32 @write(i32 %34, i8* %7, i32 %30)
br label %36
36: ; preds = %33, %22, %28, %17
br label %37
37: ; preds = %11, %15, %36
ret void
}