#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*/ tunnel_lookup(unsigned char);
unsigned char tunnel_generate_id(void) {
  int ok;
  unsigned char tid;
  static unsigned char last_tid = 0xff;
  ok = 1;
  for (tid = last_tid + 1; tid != last_tid; ++tid) {
    if (!tunnel_lookup(tid)) {
      last_tid = tid;
      return tid;
    }
  }
  return 0xff;
}


x86 assembly (clang, -O0):
<tunnel_generate_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1,-0x8(%rbp)
movzbl 0x2f02(%rip),%eax
add    $0x1,%eax
mov    %al,-0x9(%rbp)
movzbl -0x9(%rbp),%eax
movzbl 0x2ef1(%rip),%ecx
cmp    %ecx,%eax
je     1177 <tunnel_generate_id+0x67>
movzbl -0x9(%rbp),%edi
call   1030 <tunnel_lookup@plt>
cmp    $0x0,%eax
jne    1165 <tunnel_generate_id+0x55>
mov    -0x9(%rbp),%al
mov    %al,0x2ece(%rip)
mov    -0x9(%rbp),%al
mov    %al,-0x1(%rbp)
jmp    117b <tunnel_generate_id+0x6b>
jmp    116a <tunnel_generate_id+0x5a>
mov    -0x9(%rbp),%al
add    $0x1,%al
mov    %al,-0x9(%rbp)
jmp    112c <tunnel_generate_id+0x1c>
movb   $0xff,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tunnel_generate_id>:
push   %rbx
mov    0x2f11(%rip),%bl
add    $0x1,%bl
nopw   0x0(%rax,%rax,1)
movzbl %bl,%edi
call   1030 <tunnel_lookup@plt>
test   %eax,%eax
je     113b <tunnel_generate_id+0x2b>
add    $0x1,%bl
cmp    0x2ef3(%rip),%bl
jne    1120 <tunnel_generate_id+0x10>
mov    $0xff,%bl
jmp    1141 <tunnel_generate_id+0x31>
mov    %bl,0x2ee7(%rip)
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@tunnel_generate_id.last_tid = internal global i8 -1
define zeroext i8 @tunnel_generate_id() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca i8
store i32 1, i32* %2
%4 = load i8, i8* @tunnel_generate_id.last_tid
%5 = zext i8 %4 to i32
%6 = add nsw i32 %5, 1
%7 = trunc i32 %6 to i8
store i8 %7, i8* %3
br label %8
8: ; preds = %22, %0
%9 = load i8, i8* %3
%10 = zext i8 %9 to i32
%11 = load i8, i8* @tunnel_generate_id.last_tid
%12 = zext i8 %11 to i32
%13 = icmp ne i32 %10, %12
br i1 %13, label %14, label %25
14: ; preds = %8
%15 = load i8, i8* %3
%16 = call i32 @tunnel_lookup(i8 zeroext %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i8, i8* %3
store i8 %19, i8* @tunnel_generate_id.last_tid
%20 = load i8, i8* %3
store i8 %20, i8* %1
br label %26
21: ; preds = %14
br label %22
22: ; preds = %21
%23 = load i8, i8* %3
%24 = add i8 %23, 1
store i8 %24, i8* %3
br label %8
25: ; preds = %8
store i8 -1, i8* %1
br label %26
26: ; preds = %25, %18
%27 = load i8, i8* %1
ret i8 %27
}

LLVM IR (-O2):
@tunnel_generate_id.last_tid = internal unnamed_addr global i8 -1
define zeroext i8 @tunnel_generate_id() {
%1 = load i8, i8* @tunnel_generate_id.last_tid
%2 = add i8 %1, 1
br label %3
3: ; preds = %0, %7
%4 = phi i8 [ %2, %0 ], [ %9, %7 ]
%5 = tail call i32 @tunnel_lookup(i8 zeroext %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = load i8, i8* @tunnel_generate_id.last_tid
%9 = add i8 %4, 1
%10 = icmp eq i8 %9, %8
br i1 %10, label %12, label %3
11: ; preds = %3
store i8 %4, i8* @tunnel_generate_id.last_tid
br label %12
12: ; preds = %7, %11
%13 = phi i8 [ %4, %11 ], [ -1, %7 ]
ret i8 %13
}