#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 uint8_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ listenForResponse(char **, int *);
char *localNetworkPrefix();
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sendBroadcast(char *, char *, int);
int /*<<< orphan*/ sleep(int);
int snprintf(char *, int, char *, int);
scalar_t__ strlen(char *);
int findBroker(char **address, int *port, bool slow) {
  printf("find broker...\n");
  int l = snprintf(NULL, 0, "biot er on port %d", 8888);
  char *prefix = localNetworkPrefix();
  if (strlen(prefix) > 0) {
    char *broadcastMessage = malloc(l + 1);
    for (uint8_t i = 1; i < 255; i++) {
      snprintf(broadcastMessage, l + 1, "biot er on port %d", 8888);
      sendBroadcast(broadcastMessage, prefix, i);
      if (listenForResponse(address, port)) {
        printf("got it...\n");
        free(prefix);
        return 1;
      }
      if (slow)
        sleep(3);
    }
  }
  free(prefix);
  return 0;
}


x86 assembly (clang, -O0):
<findBroker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xe56(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xe54(%rip),%rdx
mov    $0x22b8,%ecx
call   1070 <snprintf@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <localNetworkPrefix@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x0,%rax
jle    1299 <findBroker+0x109>
mov    -0x20(%rbp),%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0xff,-0x34(%rbp)
jge    1294 <findBroker+0x104>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
lea    0xdf5(%rip),%rdx
mov    $0x22b8,%ecx
call   1070 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   10a0 <sendBroadcast@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <listenForResponse@plt>
cmp    $0x0,%rax
je     126d <findBroker+0xdd>
lea    0xdd0(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a9 <findBroker+0x119>
cmpl   $0x0,-0x1c(%rbp)
je     1281 <findBroker+0xf1>
mov    $0x3,%edi
call   10b0 <sleep@plt>
jmp    1286 <findBroker+0xf6>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11fd <findBroker+0x6d>
jmp    1299 <findBroker+0x109>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findBroker>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,0x4(%rsp)
mov    %rsi,%r12
mov    %rdi,%r13
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
lea    0xe48(%rip),%rdx
xor    %r14d,%r14d
xor    %edi,%edi
xor    %esi,%esi
mov    $0x22b8,%ecx
call   1070 <snprintf@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <localNetworkPrefix@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
test   %rax,%rax
jle    126b <findBroker+0xdb>
add    $0x1,%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    $0x1,%r15d
jmp    120d <findBroker+0x7d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r15d
cmp    $0xff,%r15d
je     1254 <findBroker+0xc4>
mov    %r14,%rdi
mov    %ebp,%esi
lea    0xde7(%rip),%rdx
mov    $0x22b8,%ecx
call   1070 <snprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   10a0 <sendBroadcast@plt>
mov    %r13,%rdi
mov    %r12,%rsi
call   1080 <listenForResponse@plt>
test   %rax,%rax
jne    1259 <findBroker+0xc9>
cmpl   $0x0,0x4(%rsp)
je     1200 <findBroker+0x70>
mov    $0x3,%edi
call   10b0 <sleep@plt>
jmp    1200 <findBroker+0x70>
xor    %r14d,%r14d
jmp    126b <findBroker+0xdb>
lea    0xdc2(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"find broker...\0A\00"
@.str.1 = [19 x i8] c"biot er on port %d\00"
@.str.2 = [11 x i8] c"got it...\0A\00"
define i32 @findBroker(i8** %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%12 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @snprintf(i8* null, i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 8888)
store i32 %13, i32* %8
%14 = call i8* (...) @localNetworkPrefix()
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = call i64 @strlen(i8* %15)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %52
18: ; preds = %3
%19 = load i32, i32* %8
%20 = add nsw i32 %19, 1
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %10
store i32 1, i32* %11
br label %22
22: ; preds = %48, %18
%23 = load i32, i32* %11
%24 = icmp slt i32 %23, 255
br i1 %24, label %25, label %51
25: ; preds = %22
%26 = load i8*, i8** %10
%27 = load i32, i32* %8
%28 = add nsw i32 %27, 1
%29 = call i32 @snprintf(i8* %26, i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 8888)
%30 = load i8*, i8** %10
%31 = load i8*, i8** %9
%32 = load i32, i32* %11
%33 = call i32 @sendBroadcast(i8* %30, i8* %31, i32 %32)
%34 = load i8**, i8*** %5
%35 = load i32*, i32** %6
%36 = call i64 @listenForResponse(i8** %34, i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %25
%39 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%40 = load i8*, i8** %9
%41 = call i32 @free(i8* %40)
store i32 1, i32* %4
br label %55
42: ; preds = %25
%43 = load i32, i32* %7
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @sleep(i32 3)
br label %47
47: ; preds = %45, %42
br label %48
48: ; preds = %47
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %22
51: ; preds = %22
br label %52
52: ; preds = %51, %3
%53 = load i8*, i8** %9
%54 = call i32 @free(i8* %53)
store i32 0, i32* %4
br label %55
55: ; preds = %52, %38
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str.1 = [19 x i8] c"biot er on port %d\00"
@str = [15 x i8] c"find broker...\00"
@str.3 = [10 x i8] c"got it...\00"
define i32 @findBroker(i8** %0, i32* %1, i32 %2) {
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @snprintf(i8* null, i32 0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 8888)
%6 = tail call i8* (...) @localNetworkPrefix()
%7 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %3
%10 = add nsw i32 %5, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i32 %2, 0
br label %13
13: ; preds = %9, %24
%14 = phi i32 [ 1, %9 ], [ %25, %24 ]
%15 = tail call i32 @snprintf(i8* %11, i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 8888)
%16 = tail call i32 @sendBroadcast(i8* %11, i8* %6, i32 %14)
%17 = tail call i64 @listenForResponse(i8** %0, i32* %1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.3, i64 0, i64 0))
br label %27
21: ; preds = %13
br i1 %12, label %24, label %22
22: ; preds = %21
%23 = tail call i32 @sleep(i32 3)
br label %24
24: ; preds = %21, %22
%25 = add nuw nsw i32 %14, 1
%26 = icmp eq i32 %25, 255
br i1 %26, label %27, label %13
27: ; preds = %24, %3, %19
%28 = phi i32 [ 1, %19 ], [ 0, %3 ], [ 0, %24 ]
%29 = tail call i32 @free(i8* %6)
ret i32 %28
}