#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 timespec {
  unsigned long tv_sec;
  unsigned long tv_nsec;
};
struct sockaddr_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ _exit(int);
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int sscanf(char *, char *, unsigned long *, unsigned long *, unsigned long *,
           unsigned long *);
void serve1(int socket_fd) {
  char buf[128];
  memset(buf, 0, sizeof(buf));
  struct sockaddr_storage sa;
  socklen_t sa_len = sizeof(sa);
  ssize_t ret = recvfrom(socket_fd, buf, sizeof(buf) - 1, 0,
                         (struct sockaddr *)&sa, &sa_len);
  if (ret == -1) {
    return;
  }
  unsigned long int secs1;
  unsigned long int nanos1;
  unsigned long int secs2;
  unsigned long int nanos2;
  if (sscanf(buf, "%lu%lu%lu%lu", &secs1, &nanos1, &secs2, &nanos2) != 4) {
    return;
  }
  if (secs1 + secs2 > 600 || nanos1 >= 1000 * 1000 * 1000 ||
      nanos2 >= 1000 * 1000 * 1000) {
    return;
  }
  if (fork() != 0) {
    return;
  }
  struct timespec ts1, ts2;
  ts1.tv_sec = secs1;
  ts1.tv_nsec = nanos1;
  ts2.tv_sec = secs2;
  ts2.tv_nsec = nanos2;
  if (-1 == clock_nanosleep(CLOCK_MONOTONIC, 0, &ts1, NULL))
    _exit(1);
  sendto(socket_fd, "B\n", 2, 0, (struct sockaddr *)&sa, sa_len);
  if (-1 == clock_nanosleep(CLOCK_MONOTONIC, 0, &ts2, NULL))
    _exit(2);
  sendto(socket_fd, "A\n", 2, 0, (struct sockaddr *)&sa, sa_len);
  _exit(0);
}


x86 assembly (clang, -O0):
<serve1>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
movl   $0x4,-0x9c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
lea    -0x98(%rbp),%r8
mov    $0x7f,%edx
xor    %ecx,%ecx
lea    -0x9c(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0xa0(%rbp)
cmpl   $0xffffffff,-0xa0(%rbp)
jne    11d7 <serve1+0x67>
jmp    135a <serve1+0x1ea>
lea    -0x90(%rbp),%rdi
lea    0xe1b(%rip),%rsi
lea    -0xa8(%rbp),%rdx
lea    -0xb0(%rbp),%rcx
lea    -0xb8(%rbp),%r8
lea    -0xc0(%rbp),%r9
call   1070 <sscanf@plt>
cmp    $0x4,%eax
je     1214 <serve1+0xa4>
jmp    135a <serve1+0x1ea>
mov    -0xa8(%rbp),%rax
add    -0xb8(%rbp),%rax
cmp    $0x258,%rax
ja     1250 <serve1+0xe0>
cmpq   $0x3b9aca00,-0xb0(%rbp)
jae    1250 <serve1+0xe0>
cmpq   $0x3b9aca00,-0xc0(%rbp)
jb     1255 <serve1+0xe5>
jmp    135a <serve1+0x1ea>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
je     126b <serve1+0xfb>
jmp    135a <serve1+0x1ea>
mov    -0xa8(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0xb0(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0xb8(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0xc0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0xd0(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <clock_nanosleep@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12d7 <serve1+0x167>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x98(%rbp),%r8
mov    -0x9c(%rbp),%r9d
lea    0xd1e(%rip),%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0xe0(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1080 <clock_nanosleep@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    132f <serve1+0x1bf>
mov    $0x2,%edi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x98(%rbp),%r8
mov    -0x9c(%rbp),%r9d
lea    0xcc9(%rip),%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve1>:
push   %rbp
push   %rbx
sub    $0xd8,%rsp
mov    %edi,%ebp
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1040 <memset@plt>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7f,%edx
xor    %ecx,%ecx
call   1060 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     1215 <serve1+0xa5>
lea    0xe42(%rip),%rsi
lea    0x50(%rsp),%rdi
lea    0x28(%rsp),%rdx
lea    0x20(%rsp),%rcx
lea    0x18(%rsp),%r8
lea    0x10(%rsp),%r9
call   1070 <sscanf@plt>
cmp    $0x4,%eax
jne    1215 <serve1+0xa5>
mov    0x18(%rsp),%rax
add    0x28(%rsp),%rax
cmp    $0x258,%rax
ja     1215 <serve1+0xa5>
cmpq   $0x3b9ac9ff,0x20(%rsp)
9a 3b
ja     1215 <serve1+0xa5>
cmpq   $0x3b9ac9ff,0x10(%rsp)
9a 3b
ja     1215 <serve1+0xa5>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     121f <serve1+0xaf>
add    $0xd8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x28(%rsp),%rax
mov    %rax,0x40(%rsp)
mov    0x20(%rsp),%rax
mov    %rax,0x48(%rsp)
mov    0x18(%rsp),%rax
mov    %rax,0x30(%rsp)
mov    0x10(%rsp),%rax
mov    %rax,0x38(%rsp)
mov    0x2d9a(%rip),%rbx
mov    (%rbx),%edi
lea    0x40(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <clock_nanosleep@plt>
cmp    $0xffffffff,%eax
jne    126d <serve1+0xfd>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    0x4(%rsp),%r9d
lea    0xd94(%rip),%rsi
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    (%rbx),%edi
lea    0x30(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1080 <clock_nanosleep@plt>
cmp    $0xffffffff,%eax
jne    12ab <serve1+0x13b>
mov    $0x2,%edi
call   1030 <_exit@plt>
mov    0x4(%rsp),%r9d
lea    0xd59(%rip),%rsi
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1050 <sendto@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.timespec = type { i64, i64 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"%lu%lu%lu%lu\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [3 x i8] c"B\0A\00"
@.str.2 = [3 x i8] c"A\0A\00"
define void @serve1(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca %struct.timespec
%12 = alloca %struct.timespec
store i32 %0, i32* %2
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 128)
store i32 4, i32* %5
%15 = load i32, i32* %2
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%17 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%18 = call i32 @recvfrom(i32 %15, i8* %16, i32 127, i32 0, %struct.sockaddr* %17, i32* %5)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %22
21: ; preds = %1
br label %73
22: ; preds = %1
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%24 = call i32 @sscanf(i8* %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64* %7, i64* %8, i64* %9, i64* %10)
%25 = icmp ne i32 %24, 4
br i1 %25, label %26, label %27
26: ; preds = %22
br label %73
27: ; preds = %22
%28 = load i64, i64* %7
%29 = load i64, i64* %9
%30 = add i64 %28, %29
%31 = icmp ugt i64 %30, 600
br i1 %31, label %38, label %32
32: ; preds = %27
%33 = load i64, i64* %8
%34 = icmp uge i64 %33, 1000000000
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i64, i64* %10
%37 = icmp uge i64 %36, 1000000000
br i1 %37, label %38, label %39
38: ; preds = %35, %32, %27
br label %73
39: ; preds = %35
%40 = call i64 (...) @fork()
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %39
br label %73
43: ; preds = %39
%44 = load i64, i64* %7
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
store i64 %44, i64* %45
%46 = load i64, i64* %8
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
store i64 %46, i64* %47
%48 = load i64, i64* %9
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
store i64 %48, i64* %49
%50 = load i64, i64* %10
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 1
store i64 %50, i64* %51
%52 = load i32, i32* @CLOCK_MONOTONIC
%53 = call i32 @clock_nanosleep(i32 %52, i32 0, %struct.timespec* %11, i32* null)
%54 = icmp eq i32 -1, %53
br i1 %54, label %55, label %57
55: ; preds = %43
%56 = call i32 @_exit(i32 1)
unreachable
57: ; preds = %43
%58 = load i32, i32* %2
%59 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%60 = load i32, i32* %5
%61 = call i32 @sendto(i32 %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 0, %struct.sockaddr* %59, i32 %60)
%62 = load i32, i32* @CLOCK_MONOTONIC
%63 = call i32 @clock_nanosleep(i32 %62, i32 0, %struct.timespec* %12, i32* null)
%64 = icmp eq i32 -1, %63
br i1 %64, label %65, label %67
65: ; preds = %57
%66 = call i32 @_exit(i32 2)
unreachable
67: ; preds = %57
%68 = load i32, i32* %2
%69 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%70 = load i32, i32* %5
%71 = call i32 @sendto(i32 %68, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 0, %struct.sockaddr* %69, i32 %70)
%72 = call i32 @_exit(i32 0)
unreachable
73: ; preds = %42, %38, %26, %21
ret void
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.timespec = type { i64, i64 }
%struct.sockaddr = type { i32 }
@.str = [13 x i8] c"%lu%lu%lu%lu\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [3 x i8] c"B\0A\00"
@.str.2 = [3 x i8] c"A\0A\00"
define void @serve1(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca %struct.timespec
%10 = alloca %struct.timespec
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 128)
%13 = bitcast %struct.sockaddr_storage* %3 to i8*
%14 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%15 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%16 = call i32 @recvfrom(i32 %0, i8* %11, i32 127, i32 0, %struct.sockaddr* %15, i32* %4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %68, label %18
18: ; preds = %1
%19 = bitcast i64* %5 to i8*
%20 = bitcast i64* %6 to i8*
%21 = bitcast i64* %7 to i8*
%22 = bitcast i64* %8 to i8*
%23 = call i32 @sscanf(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64* %5, i64* %6, i64* %7, i64* %8)
%24 = icmp eq i32 %23, 4
br i1 %24, label %25, label %67
25: ; preds = %18
%26 = load i64, i64* %5
%27 = load i64, i64* %7
%28 = add i64 %27, %26
%29 = icmp ugt i64 %28, 600
%30 = load i64, i64* %6
%31 = icmp ugt i64 %30, 999999999
%32 = select i1 %29, i1 true, i1 %31
%33 = load i64, i64* %8
%34 = icmp ugt i64 %33, 999999999
%35 = select i1 %32, i1 true, i1 %34
br i1 %35, label %67, label %36
36: ; preds = %25
%37 = call i64 (...) @fork()
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %67
39: ; preds = %36
%40 = bitcast %struct.timespec* %9 to i8*
%41 = bitcast %struct.timespec* %10 to i8*
%42 = load i64, i64* %5
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i64 0, i32 0
store i64 %42, i64* %43
%44 = load i64, i64* %6
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %9, i64 0, i32 1
store i64 %44, i64* %45
%46 = load i64, i64* %7
%47 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i64 0, i32 0
store i64 %46, i64* %47
%48 = load i64, i64* %8
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i64 0, i32 1
store i64 %48, i64* %49
%50 = load i32, i32* @CLOCK_MONOTONIC
%51 = call i32 @clock_nanosleep(i32 %50, i32 0, %struct.timespec* %9, i32* null)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %39
%54 = call i32 @_exit(i32 1)
unreachable
55: ; preds = %39
%56 = load i32, i32* %4
%57 = call i32 @sendto(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 0, %struct.sockaddr* %15, i32 %56)
%58 = load i32, i32* @CLOCK_MONOTONIC
%59 = call i32 @clock_nanosleep(i32 %58, i32 0, %struct.timespec* %10, i32* null)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = call i32 @_exit(i32 2)
unreachable
63: ; preds = %55
%64 = load i32, i32* %4
%65 = call i32 @sendto(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 0, %struct.sockaddr* %15, i32 %64)
%66 = call i32 @_exit(i32 0)
unreachable
67: ; preds = %36, %25, %18
br label %68
68: ; preds = %1, %67
ret void
}