#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_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ reuseaddr;
typedef int /*<<< orphan*/ buffer;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ isDataReady(int);
long listenToSocket(int, char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strdup(char *);
char *strerror(int /*<<< orphan*/);
char *strtok(char *, char *);
int /*<<< orphan*/ usleep(int);
int listenForResponse(char **address, int *port) {
  int sock;
  int reuseaddr = 1;
  struct sockaddr_in addr;
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    printf("error opening socket: %s\n", strerror(errno));
    exit(0);
  }
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8890);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    printf("error binding to socket: %s\n", strerror(errno));
    exit(0);
  }
  usleep(1000);
  if (isDataReady(sock)) {
    char buffer[180];
    long count = listenToSocket(sock, buffer, sizeof(buffer));
    if (count > 0) {
      printf("rx response to broadcast: %s\n", buffer);
      char *p = strtok(buffer, ":");
      if (p != NULL) {
        *address = strdup(p);
        p = strtok(NULL, ":");
        if (p != NULL) {
          *port = atoi(p);
        }
        close(sock);
        return (1);
      }
    }
  }
  close(sock);
  return (0);
}


x86 assembly (clang, -O0):
<listenForResponse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1244 <listenForResponse+0x64>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xdc8(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x22ba,%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12cc <listenForResponse+0xec>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd5a(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    $0x3e8,%edi
call   10f0 <usleep@plt>
mov    -0x1c(%rbp),%edi
call   1030 <isDataReady@plt>
cmp    $0x0,%rax
je     13b9 <listenForResponse+0x1d9>
mov    -0x1c(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    $0xb4,%edx
call   1080 <listenToSocket@plt>
mov    %rax,-0xf8(%rbp)
cmpq   $0x0,-0xf8(%rbp)
jle    13b4 <listenForResponse+0x1d4>
lea    -0xf0(%rbp),%rsi
lea    0xd18(%rip),%rdi
call   1060 <printf@plt>
lea    -0xf0(%rbp),%rdi
lea    0xd23(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0x100(%rbp)
je     13af <listenForResponse+0x1cf>
mov    -0x100(%rbp),%rdi
call   10d0 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
mov    %eax,%edi
lea    0xce8(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0x100(%rbp)
je     139b <listenForResponse+0x1bb>
mov    -0x100(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c8 <listenForResponse+0x1e8>
jmp    13b4 <listenForResponse+0x1d4>
jmp    13b9 <listenForResponse+0x1d9>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenForResponse>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xd8,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
movl   $0x1,0xc(%rsp)
mov    0x2dde(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     131f <listenForResponse+0x13f>
mov    %eax,%ebp
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x22ba,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1336 <listenForResponse+0x156>
mov    $0x3e8,%edi
call   10f0 <usleep@plt>
mov    %ebp,%edi
call   1030 <isDataReady@plt>
test   %rax,%rax
je     1308 <listenForResponse+0x128>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0xb4,%edx
call   1080 <listenToSocket@plt>
test   %rax,%rax
jle    1308 <listenForResponse+0x128>
lea    0xd87(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
call   1060 <printf@plt>
lea    0xd91(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1308 <listenForResponse+0x128>
mov    %rax,%rdi
call   10d0 <strdup@plt>
mov    %rax,(%r15)
lea    0xd72(%rip),%rsi
xor    %edi,%edi
call   10a0 <strtok@plt>
test   %rax,%rax
je     12fa <listenForResponse+0x11a>
mov    %rax,%rdi
call   10b0 <atoi@plt>
mov    %eax,(%r14)
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x1,%eax
jmp    1311 <listenForResponse+0x131>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xccc(%rip),%rdi
jmp    134b <listenForResponse+0x16b>
mov    0x2c83(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xccf(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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 = [26 x i8] c"error opening socket: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"error binding to socket: %s\0A\00"
@.str.2 = [30 x i8] c"rx response to broadcast: %s\0A\00"
@.str.3 = [2 x i8] c":\00"
define i32 @listenForResponse(i8** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca [180 x i8]
%10 = alloca i64
%11 = alloca i8*
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i32 1, i32* %7
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %2
%23 = load i32, i32* %6
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %23, i32 %24, i32 %25, i32* %7, i32 4)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 8890)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %6
%35 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%36 = call i64 @bind(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %22
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 0)
unreachable
43: ; preds = %22
%44 = call i32 @usleep(i32 1000)
%45 = load i32, i32* %6
%46 = call i64 @isDataReady(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %77
48: ; preds = %43
%49 = load i32, i32* %6
%50 = getelementptr inbounds [180 x i8], [180 x i8]* %9, i64 0, i64 0
%51 = call i64 @listenToSocket(i32 %49, i8* %50, i32 180)
store i64 %51, i64* %10
%52 = load i64, i64* %10
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %76
54: ; preds = %48
%55 = getelementptr inbounds [180 x i8], [180 x i8]* %9, i64 0, i64 0
%56 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = getelementptr inbounds [180 x i8], [180 x i8]* %9, i64 0, i64 0
%58 = call i8* @strtok(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %58, i8** %11
%59 = load i8*, i8** %11
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %75
61: ; preds = %54
%62 = load i8*, i8** %11
%63 = call i8* @strdup(i8* %62)
%64 = load i8**, i8*** %4
store i8* %63, i8** %64
%65 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %65, i8** %11
%66 = load i8*, i8** %11
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %72
68: ; preds = %61
%69 = load i8*, i8** %11
%70 = call i32 @atoi(i8* %69)
%71 = load i32*, i32** %5
store i32 %70, i32* %71
br label %72
72: ; preds = %68, %61
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
store i32 1, i32* %3
br label %80
75: ; preds = %54
br label %76
76: ; preds = %75, %48
br label %77
77: ; preds = %76, %43
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
store i32 0, i32* %3
br label %80
80: ; preds = %77, %72
%81 = load i32, i32* %3
ret i32 %81
}

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
@.str = [26 x i8] c"error opening socket: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"error binding to socket: %s\0A\00"
@.str.2 = [30 x i8] c"rx response to broadcast: %s\0A\00"
@.str.3 = [2 x i8] c":\00"
define i32 @listenForResponse(i8** nocapture writeonly %0, i32* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [180 x i8]
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = bitcast %struct.sockaddr_in* %4 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 %17
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %10, i32 %18, i32 %19, i32* %3, i32 4)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 8890)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i64 @bind(i32 %10, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %17
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %17
%36 = call i32 @usleep(i32 1000)
%37 = call i64 @isDataReady(i32 %10)
%38 = icmp eq i64 %37, 0
br i1 %38, label %54, label %39
39: ; preds = %35
%40 = getelementptr inbounds [180 x i8], [180 x i8]* %5, i64 0, i64 0
%41 = call i64 @listenToSocket(i32 %10, i8* %40, i32 180)
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %39
%44 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%45 = call i8* @strtok(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%46 = icmp eq i8* %45, null
br i1 %46, label %53, label %47
47: ; preds = %43
%48 = call i8* @strdup(i8* %45)
store i8* %48, i8** %0
%49 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%50 = icmp eq i8* %49, null
br i1 %50, label %56, label %51
51: ; preds = %47
%52 = call i32 @atoi(i8* %49)
store i32 %52, i32* %1
br label %56
53: ; preds = %43, %39
br label %54
54: ; preds = %53, %35
%55 = call i32 @close(i32 %10)
br label %58
56: ; preds = %47, %51
%57 = call i32 @close(i32 %10)
br label %58
58: ; preds = %56, %54
%59 = phi i32 [ 0, %54 ], [ 1, %56 ]
ret i32 %59
}