#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, char const *, int);
int try_port(char const *, int);
int listen_find_open_port(const char *addr, int start, int end, int *port) {
  int i;
  int rc;
  if (start == -1)
    start = end;
  if (end == -1)
    end = start;
  for (i = start; i <= end; i++) {
    rc = try_port(addr, i);
    if (rc >= 0) {
      *port = i;
      printf("URI=%s:%d\n", addr ? addr : "", i);
      return rc;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<listen_find_open_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1146 <listen_find_open_port+0x26>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1156 <listen_find_open_port+0x36>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     11e3 <listen_find_open_port+0xc3>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <try_port@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     11d0 <listen_find_open_port+0xb0>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
je     11a2 <listen_find_open_port+0x82>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    11b2 <listen_find_open_port+0x92>
lea    0xe61(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11b2 <listen_find_open_port+0x92>
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <listen_find_open_port+0xca>
jmp    11d5 <listen_find_open_port+0xb5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    115c <listen_find_open_port+0x3c>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_find_open_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r13d
cmp    $0xffffffff,%esi
mov    %esi,%ebx
cmove  %edx,%ebx
cmp    $0xffffffff,%edx
cmove  %esi,%r13d
mov    $0xffffffff,%r12d
cmp    %r13d,%ebx
jg     119c <listen_find_open_port+0x7c>
mov    %rcx,%r15
mov    %rdi,%r14
add    $0x1,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <try_port@plt>
test   %eax,%eax
jns    1178 <listen_find_open_port+0x58>
add    $0x1,%ebx
cmp    %ebx,%r13d
jne    1160 <listen_find_open_port+0x40>
jmp    119c <listen_find_open_port+0x7c>
mov    %eax,%ebp
mov    %ebx,(%r15)
test   %r14,%r14
lea    0xe83(%rip),%rsi
cmovne %r14,%rsi
lea    0xe6e(%rip),%rdi
mov    %ebx,%edx
call   1030 <printf@plt>
mov    %ebp,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"URI=%s:%d\0A\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @listen_find_open_port(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%12 = load i32, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = load i32, i32* %8
store i32 %15, i32* %7
br label %16
16: ; preds = %14, %4
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i32, i32* %7
store i32 %20, i32* %8
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* %7
store i32 %22, i32* %10
br label %23
23: ; preds = %47, %21
%24 = load i32, i32* %10
%25 = load i32, i32* %8
%26 = icmp sle i32 %24, %25
br i1 %26, label %27, label %50
27: ; preds = %23
%28 = load i8*, i8** %6
%29 = load i32, i32* %10
%30 = call i32 @try_port(i8* %28, i32 %29)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp sge i32 %31, 0
br i1 %32, label %33, label %46
33: ; preds = %27
%34 = load i32, i32* %10
%35 = load i32*, i32** %9
store i32 %34, i32* %35
%36 = load i8*, i8** %6
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = load i8*, i8** %6
br label %41
40: ; preds = %33
br label %41
41: ; preds = %40, %38
%42 = phi i8* [ %39, %38 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %40 ]
%43 = load i32, i32* %10
%44 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %42, i32 %43)
%45 = load i32, i32* %11
store i32 %45, i32* %5
br label %51
46: ; preds = %27
br label %47
47: ; preds = %46
%48 = load i32, i32* %10
%49 = add nsw i32 %48, 1
store i32 %49, i32* %10
br label %23
50: ; preds = %23
store i32 -1, i32* %5
br label %51
51: ; preds = %50, %41
%52 = load i32, i32* %5
ret i32 %52
}

LLVM IR (-O2):
@.str = [11 x i8] c"URI=%s:%d\0A\00"
@.str.1 = [1 x i8] zeroinitializer
define i32 @listen_find_open_port(i8* %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = icmp eq i32 %1, -1
%6 = select i1 %5, i32 %2, i32 %1
%7 = icmp eq i32 %2, -1
%8 = select i1 %7, i32 %1, i32 %2
%9 = icmp sgt i32 %6, %8
br i1 %9, label %21, label %10
10: ; preds = %4, %18
%11 = phi i32 [ %19, %18 ], [ %6, %4 ]
%12 = tail call i32 @try_port(i8* %0, i32 %11)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
store i32 %11, i32* %3
%15 = icmp eq i8* %0, null
%16 = select i1 %15, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %0
%17 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %11)
br label %21
18: ; preds = %10
%19 = add i32 %11, 1
%20 = icmp eq i32 %11, %8
br i1 %20, label %21, label %10
21: ; preds = %18, %4, %14
%22 = phi i32 [ %12, %14 ], [ -1, %4 ], [ -1, %18 ]
ret i32 %22
}