#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 {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ si_master;
typedef int /*<<< orphan*/ hello;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_BROADCAST;
int allowBroadcast(int, int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char const *, int, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ setReciveTimeout(int, int, int /*<<< orphan*/);
int isThereAMaster(int sock, char *broadcastIP, int port,
                   struct sockaddr_in *si_master) {
  struct sockaddr_in si_broad;
  si_broad.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  si_broad.sin_family = AF_INET;
  si_broad.sin_port = htons(port);
  static const char hello[] = "hello, is there anybody?";
  char buf[50];
  memset(buf, 0, sizeof buf);
  int recvLen, helloLen = sizeof(hello);
  setReciveTimeout(sock, 3, 0);
  if (allowBroadcast(sock, 1) == -1) {
    die("allowBroadcast()");
  }
  int siBroadLen = sizeof si_broad;
  if (sendto(sock, hello, helloLen, 0, (struct sockaddr *)&si_broad,
             (siBroadLen)) == -1) {
    die("sendto()");
  }
  if (allowBroadcast(sock, 0) == -1) {
    die("allowBroadcast()");
  }
  printf("Waiting for master response\n");
  int siMasterLen = sizeof(si_master);
  recvLen =
      recvfrom(sock, buf, 50, 0, (struct sockaddr *)&si_master, &(siMasterLen));
  if (recvLen > 0) {
    return 0;
  } else {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<isThereAMaster>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1070 <memset@plt>
movl   $0x19,-0x78(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x3,%esi
xor    %edx,%edx
call   1030 <setReciveTimeout@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1090 <allowBroadcast@plt>
cmp    $0xffffffff,%eax
jne    1219 <isThereAMaster+0x89>
lea    0xe05(%rip),%rdi
call   10b0 <die@plt>
movl   $0xc,-0x7c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x78(%rbp),%edx
lea    -0x30(%rbp),%r8
mov    -0x7c(%rbp),%r9d
lea    0xdcb(%rip),%rsi
xor    %ecx,%ecx
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1251 <isThereAMaster+0xc1>
lea    0xdde(%rip),%rdi
call   10b0 <die@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1090 <allowBroadcast@plt>
cmp    $0xffffffff,%eax
jne    1270 <isThereAMaster+0xe0>
lea    0xdae(%rip),%rdi
call   10b0 <die@plt>
lea    0xdbc(%rip),%rdi
call   1050 <printf@plt>
movl   $0x8,-0x80(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x32,%edx
xor    %ecx,%ecx
lea    -0x80(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jle    12b7 <isThereAMaster+0x127>
movl   $0x0,-0x4(%rbp)
jmp    12be <isThereAMaster+0x12e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isThereAMaster>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    %edx,%ebp
mov    %edi,%ebx
mov    %rcx,0x18(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x32,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    $0x3,%esi
xor    %edx,%edx
call   1040 <setReciveTimeout@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <allowBroadcast@plt>
cmp    $0xffffffff,%eax
jne    1205 <isThereAMaster+0x75>
lea    0xe19(%rip),%rdi
call   10b0 <die@plt>
lea    0xdf4(%rip),%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    $0x19,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1236 <isThereAMaster+0xa6>
lea    0xdf9(%rip),%rdi
call   10b0 <die@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1090 <allowBroadcast@plt>
cmp    $0xffffffff,%eax
jne    1250 <isThereAMaster+0xc0>
lea    0xdce(%rip),%rdi
call   10b0 <die@plt>
lea    0xddc(%rip),%rdi
call   1030 <puts@plt>
movl   $0x8,0x4(%rsp)
lea    0x20(%rsp),%rsi
lea    0x18(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebx,%edi
mov    $0x32,%edx
xor    %ecx,%ecx
call   10a0 <recvfrom@plt>
xor    %ecx,%ecx
test   %eax,%eax
setle  %cl
neg    %ecx
mov    %ecx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_BROADCAST = global i32 0
@AF_INET = global i32 0
@isThereAMaster.hello = internal constant [25 x i8] c"hello, is there anybody?\00"
@.str = [17 x i8] c"allowBroadcast()\00"
@.str.1 = [9 x i8] c"sendto()\00"
@.str.2 = [29 x i8] c"Waiting for master response\0A\00"
define i32 @isThereAMaster(i32 %0, i8* %1, i32 %2, %struct.sockaddr_in* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in
%11 = alloca [50 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %9
%16 = load i32, i32* @INADDR_BROADCAST
%17 = call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* %8
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %23, i32* %24
%25 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 50)
store i32 25, i32* %13
%27 = load i32, i32* %6
%28 = call i32 @setReciveTimeout(i32 %27, i32 3, i32 0)
%29 = load i32, i32* %6
%30 = call i32 @allowBroadcast(i32 %29, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %4
%33 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %32, %4
store i32 12, i32* %14
%35 = load i32, i32* %6
%36 = load i32, i32* %13
%37 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%38 = load i32, i32* %14
%39 = call i32 @sendto(i32 %35, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @isThereAMaster.hello, i64 0, i64 0), i32 %36, i32 0, %struct.sockaddr* %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %34
%44 = load i32, i32* %6
%45 = call i32 @allowBroadcast(i32 %44, i32 0)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %47, %43
%50 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 8, i32* %15
%51 = load i32, i32* %6
%52 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%53 = bitcast %struct.sockaddr_in** %9 to %struct.sockaddr*
%54 = call i32 @recvfrom(i32 %51, i8* %52, i32 50, i32 0, %struct.sockaddr* %53, i32* %15)
store i32 %54, i32* %12
%55 = load i32, i32* %12
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %49
store i32 0, i32* %5
br label %59
58: ; preds = %49
store i32 -1, i32* %5
br label %59
59: ; preds = %58, %57
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INADDR_BROADCAST = global i32 0
@AF_INET = global i32 0
@isThereAMaster.hello = internal constant [25 x i8] c"hello, is there anybody?\00"
@.str = [17 x i8] c"allowBroadcast()\00"
@.str.1 = [9 x i8] c"sendto()\00"
@str = [28 x i8] c"Waiting for master response\00"
define i32 @isThereAMaster(i32 %0, i8* nocapture readnone %1, i32 %2, %struct.sockaddr_in* %3) {
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.sockaddr_in
%7 = alloca [50 x i8]
%8 = alloca i32
store %struct.sockaddr_in* %3, %struct.sockaddr_in** %5
%9 = bitcast %struct.sockaddr_in* %6 to i8*
%10 = load i32, i32* @INADDR_BROADCAST
%11 = tail call i32 @htonl(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %2)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%18 = call i32 @memset(i8* %17, i32 0, i32 50)
%19 = call i32 @setReciveTimeout(i32 %0, i32 3, i32 0)
%20 = call i32 @allowBroadcast(i32 %0, i32 1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %4
%25 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%26 = call i32 @sendto(i32 %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @isThereAMaster.hello, i64 0, i64 0), i32 25, i32 0, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = call i32 @allowBroadcast(i32 %0, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%37 = bitcast i32* %8 to i8*
store i32 8, i32* %8
%38 = bitcast %struct.sockaddr_in** %5 to %struct.sockaddr*
%39 = call i32 @recvfrom(i32 %0, i8* %17, i32 50, i32 0, %struct.sockaddr* %38, i32* %8)
%40 = icmp slt i32 %39, 1
%41 = sext i1 %40 to i32
ret i32 %41
}