#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 */
typedef int /*<<< orphan*/ timeFromMaster;
struct timespec {
  int /*<<< orphan*/ tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ si_master;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ clock_settime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, struct timespec *, int, int /*<<< orphan*/, struct sockaddr *,
             int *);
scalar_t__ sendto(int, struct timespec *, int, int /*<<< orphan*/,
                  struct sockaddr *, int);
int /*<<< orphan*/ setReciveTimeout(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
void slave(int sock) {
  struct sockaddr_in si_master;
  int siMasterLen = sizeof(si_master), recvLen;
  char buf[2048];
  struct timespec timeFromMaster, currentTime;
  int timeouts = 0;
  memset(buf, 0, 2048);
  setReciveTimeout(sock, 13, 0);
  bool looped = false;
  while (1) {
    if ((recvLen = recvfrom(sock, (struct timespec *)&timeFromMaster, 2048, 0,
                            (struct sockaddr *)&si_master, &siMasterLen)) > 0) {
      if (looped) {
        clock_settime(CLOCK_REALTIME, &timeFromMaster);
        clock_gettime(CLOCK_REALTIME, &currentTime);
        printf("mas: %ld:%ld\n", timeFromMaster.tv_sec, timeFromMaster.tv_nsec);
        printf("own: %ld:%ld\n", currentTime.tv_sec, currentTime.tv_nsec);
        fflush(stdout);
        looped = false;
        timeouts = 0;
      } else if (sendto(sock, (struct timespec *)&timeFromMaster,
                        sizeof(timeFromMaster), 0,
                        (struct sockaddr *)&si_master, siMasterLen)) {
        looped = true;
      }
    } else if (timeouts < 3) {
      timeouts++;
      looped = false;
      printf("no message from master\n");
    } else {
      return;
    }
  }
}


x86 assembly (clang, -O0):
<slave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
movl   $0x0,-0x824(%rbp)
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1060 <memset@plt>
mov    -0x4(%rbp),%edi
mov    $0xd,%esi
xor    %edx,%edx
call   1040 <setReciveTimeout@plt>
movl   $0x0,-0x828(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%r8
lea    -0x818(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
lea    -0xc(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    12c3 <slave+0x143>
cmpl   $0x0,-0x828(%rbp)
je     1287 <slave+0x107>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    -0x818(%rbp),%rsi
call   10a0 <clock_settime@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    -0x820(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x814(%rbp),%esi
mov    -0x818(%rbp),%edx
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x81c(%rbp),%esi
mov    -0x820(%rbp),%edx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
movl   $0x0,-0x828(%rbp)
movl   $0x0,-0x824(%rbp)
jmp    12be <slave+0x13e>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%r8
mov    -0xc(%rbp),%r9d
lea    -0x818(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <sendto@plt>
cmp    $0x0,%rax
je     12b9 <slave+0x139>
movl   $0x1,-0x828(%rbp)
jmp    12be <slave+0x13e>
jmp    130a <slave+0x18a>
cmpl   $0x3,-0x824(%rbp)
jge    12fc <slave+0x17c>
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
movl   $0x0,-0x828(%rbp)
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1305 <slave+0x185>
add    $0x830,%rsp
pop    %rbp
ret
jmp    130a <slave+0x18a>
jmp    11cb <slave+0x4b>

x86 assembly (clang, -O2):
<slave>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
lea    0x20(%rsp),%rdi
xor    %r13d,%r13d
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    $0xd,%esi
xor    %edx,%edx
call   1050 <setReciveTimeout@plt>
lea    0x10(%rsp),%r14
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%r12
jmp    123c <slave+0xac>
xchg   %ax,%ax
mov    0x2df1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %r14,%rsi
call   10b0 <clock_settime@plt>
mov    0x0(%rbp),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    0x10(%rsp),%edx
mov    0x14(%rsp),%esi
xor    %r13d,%r13d
lea    0xdef(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%esi
lea    0xde7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   10a0 <recvfrom@plt>
test   %eax,%eax
jle    1290 <slave+0x100>
test   %ebp,%ebp
jne    11e0 <slave+0x50>
mov    0x4(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1080 <sendto@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
jmp    1240 <slave+0xb0>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x2,%r13d
jg     12a8 <slave+0x118>
add    $0x1,%r13d
lea    0xd7b(%rip),%rdi
call   1030 <puts@plt>
jmp    123c <slave+0xac>
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timespec = type { i32, i32 }
%struct.sockaddr = type { i32 }
@CLOCK_REALTIME = global i32 0
@.str = [14 x i8] c"mas: %ld:%ld\0A\00"
@.str.1 = [14 x i8] c"own: %ld:%ld\0A\00"
@stdout = global i32 0
@.str.2 = [24 x i8] c"no message from master\0A\00"
define void @slave(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2048 x i8]
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %2
store i32 4, i32* %4
store i32 0, i32* %9
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 2048)
%13 = load i32, i32* %2
%14 = call i32 @setReciveTimeout(i32 %13, i32 13, i32 0)
store i32 0, i32* %10
br label %15
15: ; preds = %1, %58
%16 = load i32, i32* %2
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @recvfrom(i32 %16, %struct.timespec* %7, i32 2048, i32 0, %struct.sockaddr* %17, i32* %4)
store i32 %18, i32* %5
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %49
20: ; preds = %15
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %40
23: ; preds = %20
%24 = load i32, i32* @CLOCK_REALTIME
%25 = call i32 @clock_settime(i32 %24, %struct.timespec* %7)
%26 = load i32, i32* @CLOCK_REALTIME
%27 = call i32 @clock_gettime(i32 %26, %struct.timespec* %8)
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %29, i32 %31)
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %36)
%38 = load i32, i32* @stdout
%39 = call i32 @fflush(i32 %38)
store i32 0, i32* %10
store i32 0, i32* %9
br label %48
40: ; preds = %20
%41 = load i32, i32* %2
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = load i32, i32* %4
%44 = call i64 @sendto(i32 %41, %struct.timespec* %7, i32 8, i32 0, %struct.sockaddr* %42, i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 1, i32* %10
br label %47
47: ; preds = %46, %40
br label %48
48: ; preds = %47, %23
br label %58
49: ; preds = %15
%50 = load i32, i32* %9
%51 = icmp slt i32 %50, 3
br i1 %51, label %52, label %56
52: ; preds = %49
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
store i32 %54, i32* %9
store i32 0, i32* %10
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %57
56: ; preds = %49
ret void
57: ; preds = %52
br label %58
58: ; preds = %57, %48
br label %15
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timespec = type { i32, i32 }
%struct.sockaddr = type { i32 }
@CLOCK_REALTIME = global i32 0
@.str = [14 x i8] c"mas: %ld:%ld\0A\00"
@.str.1 = [14 x i8] c"own: %ld:%ld\0A\00"
@stdout = global i32 0
@str = [23 x i8] c"no message from master\00"
define void @slave(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [2048 x i8]
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%10 = bitcast %struct.timespec* %5 to i8*
%11 = bitcast %struct.timespec* %6 to i8*
%12 = call i32 @memset(i8* %9, i32 0, i32 2048)
%13 = call i32 @setReciveTimeout(i32 %0, i32 13, i32 0)
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 1
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
br label %19
19: ; preds = %40, %1
%20 = phi i32 [ 0, %1 ], [ %41, %40 ]
br label %21
21: ; preds = %19, %42
%22 = phi i32 [ %46, %42 ], [ 0, %19 ]
%23 = call i32 @recvfrom(i32 %0, %struct.timespec* %5, i32 2048, i32 0, %struct.sockaddr* %14, i32* %3)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %21
%26 = icmp eq i32 %22, 0
br i1 %26, label %42, label %27
27: ; preds = %25
%28 = load i32, i32* @CLOCK_REALTIME
%29 = call i32 @clock_settime(i32 %28, %struct.timespec* %5)
%30 = load i32, i32* @CLOCK_REALTIME
%31 = call i32 @clock_gettime(i32 %30, %struct.timespec* %6)
%32 = load i32, i32* %15
%33 = load i32, i32* %16
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i32, i32* %17
%36 = load i32, i32* %18
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 %36)
%38 = load i32, i32* @stdout
%39 = call i32 @fflush(i32 %38)
br label %40
40: ; preds = %27, %49
%41 = phi i32 [ %50, %49 ], [ 0, %27 ]
br label %19
42: ; preds = %25
%43 = load i32, i32* %3
%44 = call i64 @sendto(i32 %0, %struct.timespec* %5, i32 8, i32 0, %struct.sockaddr* %14, i32 %43)
%45 = icmp ne i64 %44, 0
%46 = zext i1 %45 to i32
br label %21
47: ; preds = %21
%48 = icmp slt i32 %20, 3
br i1 %48, label %49, label %52
49: ; preds = %47
%50 = add nsw i32 %20, 1
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %40
52: ; preds = %47
ret void
}