#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 */
struct SPort {
  int dummy;
};

/* Variables and functions */
int TryBaudRate(struct SPort *, int, int);
int /*<<< orphan*/ Vd(int, int);
int /*<<< orphan*/ Vl();
int /*<<< orphan*/ Vs(char *);
int /*<<< orphan*/ Vsl(char *);
int FindBaudRate(struct SPort *port) {
  int baudrate = 150000;
  int breaklength = 50;
  int scale = TryBaudRate(port, baudrate, breaklength);
  if (scale == 0) {
    return 0;
  }
  while (scale != 100) {
    Vs(", scale ");
    Vd(scale, 1);
    Vsl("%");
    baudrate = (baudrate * scale) / 100;
    scale = TryBaudRate(port, baudrate, breaklength);
  }
  if (scale == 0)
    return 0;
  breaklength = 100000 / baudrate;
  if (breaklength < 2)
    breaklength = 2;
  Vsl("Finding upper bound.");
  int upperbound = baudrate;
  do {
    int trial = (upperbound * 102) / 100;
    scale = TryBaudRate(port, trial, breaklength);
    if (scale == 100)
      upperbound = trial;
  } while (scale == 100);
  Vl();
  Vsl("Finding lower bound.");
  int lowerbound = baudrate;
  do {
    int trial = (lowerbound * 98) / 100;
    scale = TryBaudRate(port, trial, breaklength);
    if (scale == 100)
      lowerbound = trial;
  } while (scale == 100);
  Vl();
  return (lowerbound + upperbound) / 2;
}


x86 assembly (clang, -O0):
<FindBaudRate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x249f0,-0x14(%rbp)
movl   $0x32,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <TryBaudRate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1192 <FindBaudRate+0x42>
movl   $0x0,-0x4(%rbp)
jmp    12e5 <FindBaudRate+0x195>
jmp    1197 <FindBaudRate+0x47>
cmpl   $0x64,-0x1c(%rbp)
je     11ef <FindBaudRate+0x9f>
lea    0xe58(%rip),%rdi
call   1060 <Vs@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1050 <Vd@plt>
lea    0xe48(%rip),%rdi
call   1070 <Vsl@plt>
mov    -0x14(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <TryBaudRate@plt>
mov    %eax,-0x1c(%rbp)
jmp    1197 <FindBaudRate+0x47>
cmpl   $0x0,-0x1c(%rbp)
jne    1205 <FindBaudRate+0xb5>
movl   $0x0,-0x4(%rbp)
jmp    12e5 <FindBaudRate+0x195>
mov    $0x186a0,%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,-0x18(%rbp)
cmpl   $0x2,-0x18(%rbp)
jge    1222 <FindBaudRate+0xd2>
movl   $0x2,-0x18(%rbp)
lea    0xde2(%rip),%rdi
call   1070 <Vsl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
imul   $0x66,-0x20(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <TryBaudRate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jne    1265 <FindBaudRate+0x115>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    126a <FindBaudRate+0x11a>
cmpl   $0x64,-0x1c(%rbp)
je     1234 <FindBaudRate+0xe4>
mov    $0x0,%al
call   1030 <Vl@plt>
lea    0xd9e(%rip),%rdi
call   1070 <Vsl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
imul   $0x62,-0x28(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1040 <TryBaudRate@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jne    12be <FindBaudRate+0x16e>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12c3 <FindBaudRate+0x173>
cmpl   $0x64,-0x1c(%rbp)
je     128d <FindBaudRate+0x13d>
mov    $0x0,%al
call   1030 <Vl@plt>
mov    -0x28(%rbp),%eax
add    -0x20(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FindBaudRate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x249f0,%ebp
mov    $0x249f0,%esi
mov    $0x32,%edx
call   1040 <TryBaudRate@plt>
mov    %eax,%r13d
test   %eax,%eax
je     12b4 <FindBaudRate+0x164>
cmp    $0x64,%r13d
je     11ef <FindBaudRate+0x9f>
mov    $0x249f0,%ebp
lea    0xe71(%rip),%r15
lea    0xe73(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1060 <Vs@plt>
mov    %r13d,%edi
mov    $0x1,%esi
call   1050 <Vd@plt>
mov    %r12,%rdi
call   1070 <Vsl@plt>
imul   %r13d,%ebp
movslq %ebp,%rax
imul   $0x51eb851f,%rax,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x25,%rbp
add    %eax,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x32,%edx
call   1040 <TryBaudRate@plt>
mov    %eax,%r13d
cmp    $0x64,%eax
jne    11a0 <FindBaudRate+0x50>
mov    $0x186a0,%eax
xor    %edx,%edx
idiv   %ebp
cmp    $0x3,%eax
mov    $0x2,%r15d
cmovge %eax,%r15d
lea    0xdff(%rip),%rdi
call   1070 <Vsl@plt>
mov    %ebp,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%r12d
imul   $0x66,%ebx,%eax
cltq
imul   $0x51eb851f,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x25,%rbx
add    %eax,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
call   1040 <TryBaudRate@plt>
cmp    $0x64,%eax
je     1220 <FindBaudRate+0xd0>
xor    %eax,%eax
call   1030 <Vl@plt>
lea    0xdc4(%rip),%rdi
call   1070 <Vsl@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%ebx
imul   $0x62,%ebp,%eax
cltq
imul   $0x51eb851f,%rax,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x25,%rbp
add    %eax,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
call   1040 <TryBaudRate@plt>
cmp    $0x64,%eax
je     1270 <FindBaudRate+0x120>
xor    %eax,%eax
call   1030 <Vl@plt>
add    %r12d,%ebx
mov    %ebx,%r13d
shr    $0x1f,%r13d
add    %ebx,%r13d
sar    %r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.SPort = type { i32 }
@.str = [9 x i8] c", scale \00"
@.str.1 = [2 x i8] c"%\00"
@.str.2 = [21 x i8] c"Finding upper bound.\00"
@.str.3 = [21 x i8] c"Finding lower bound.\00"
define i32 @FindBaudRate(%struct.SPort* %0) {
%2 = alloca i32
%3 = alloca %struct.SPort*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.SPort* %0, %struct.SPort** %3
store i32 150000, i32* %4
store i32 50, i32* %5
%11 = load %struct.SPort*, %struct.SPort** %3
%12 = load i32, i32* %4
%13 = load i32, i32* %5
%14 = call i32 @TryBaudRate(%struct.SPort* %11, i32 %12, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
br label %90
18: ; preds = %1
br label %19
19: ; preds = %22, %18
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 100
br i1 %21, label %22, label %35
22: ; preds = %19
%23 = call i32 @Vs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* %6
%25 = call i32 @Vd(i32 %24, i32 1)
%26 = call i32 @Vsl(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %4
%28 = load i32, i32* %6
%29 = mul nsw i32 %27, %28
%30 = sdiv i32 %29, 100
store i32 %30, i32* %4
%31 = load %struct.SPort*, %struct.SPort** %3
%32 = load i32, i32* %4
%33 = load i32, i32* %5
%34 = call i32 @TryBaudRate(%struct.SPort* %31, i32 %32, i32 %33)
store i32 %34, i32* %6
br label %19
35: ; preds = %19
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 0, i32* %2
br label %90
39: ; preds = %35
%40 = load i32, i32* %4
%41 = sdiv i32 100000, %40
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = icmp slt i32 %42, 2
br i1 %43, label %44, label %45
44: ; preds = %39
store i32 2, i32* %5
br label %45
45: ; preds = %44, %39
%46 = call i32 @Vsl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %4
store i32 %47, i32* %7
br label %48
48: ; preds = %61, %45
%49 = load i32, i32* %7
%50 = mul nsw i32 %49, 102
%51 = sdiv i32 %50, 100
store i32 %51, i32* %8
%52 = load %struct.SPort*, %struct.SPort** %3
%53 = load i32, i32* %8
%54 = load i32, i32* %5
%55 = call i32 @TryBaudRate(%struct.SPort* %52, i32 %53, i32 %54)
store i32 %55, i32* %6
%56 = load i32, i32* %6
%57 = icmp eq i32 %56, 100
br i1 %57, label %58, label %60
58: ; preds = %48
%59 = load i32, i32* %8
store i32 %59, i32* %7
br label %60
60: ; preds = %58, %48
br label %61
61: ; preds = %60
%62 = load i32, i32* %6
%63 = icmp eq i32 %62, 100
br i1 %63, label %48, label %64
64: ; preds = %61
%65 = call i32 (...) @Vl()
%66 = call i32 @Vsl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%67 = load i32, i32* %4
store i32 %67, i32* %9
br label %68
68: ; preds = %81, %64
%69 = load i32, i32* %9
%70 = mul nsw i32 %69, 98
%71 = sdiv i32 %70, 100
store i32 %71, i32* %10
%72 = load %struct.SPort*, %struct.SPort** %3
%73 = load i32, i32* %10
%74 = load i32, i32* %5
%75 = call i32 @TryBaudRate(%struct.SPort* %72, i32 %73, i32 %74)
store i32 %75, i32* %6
%76 = load i32, i32* %6
%77 = icmp eq i32 %76, 100
br i1 %77, label %78, label %80
78: ; preds = %68
%79 = load i32, i32* %10
store i32 %79, i32* %9
br label %80
80: ; preds = %78, %68
br label %81
81: ; preds = %80
%82 = load i32, i32* %6
%83 = icmp eq i32 %82, 100
br i1 %83, label %68, label %84
84: ; preds = %81
%85 = call i32 (...) @Vl()
%86 = load i32, i32* %9
%87 = load i32, i32* %7
%88 = add nsw i32 %86, %87
%89 = sdiv i32 %88, 2
store i32 %89, i32* %2
br label %90
90: ; preds = %84, %38, %17
%91 = load i32, i32* %2
ret i32 %91
}

LLVM IR (-O2):
%struct.SPort = type { i32 }
@.str = [9 x i8] c", scale \00"
@.str.1 = [2 x i8] c"%\00"
@.str.2 = [21 x i8] c"Finding upper bound.\00"
@.str.3 = [21 x i8] c"Finding lower bound.\00"
define i32 @FindBaudRate(%struct.SPort* %0) {
%2 = tail call i32 @TryBaudRate(%struct.SPort* %0, i32 150000, i32 50)
switch i32 %2, label %3 [
i32 0, label %38
i32 100, label %13
]
3: ; preds = %1, %3
%4 = phi i32 [ %10, %3 ], [ 150000, %1 ]
%5 = phi i32 [ %11, %3 ], [ %2, %1 ]
%6 = tail call i32 @Vs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @Vd(i32 %5, i32 1)
%8 = tail call i32 @Vsl(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = mul nsw i32 %4, %5
%10 = sdiv i32 %9, 100
%11 = tail call i32 @TryBaudRate(%struct.SPort* %0, i32 %10, i32 50)
%12 = icmp eq i32 %11, 100
br i1 %12, label %13, label %3
13: ; preds = %3, %1
%14 = phi i32 [ 150000, %1 ], [ %10, %3 ]
%15 = sdiv i32 100000, %14
%16 = icmp sgt i32 %15, 2
%17 = select i1 %16, i32 %15, i32 2
%18 = tail call i32 @Vsl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %19, %13
%20 = phi i32 [ %14, %13 ], [ %22, %19 ]
%21 = mul nsw i32 %20, 102
%22 = sdiv i32 %21, 100
%23 = tail call i32 @TryBaudRate(%struct.SPort* %0, i32 %22, i32 %17)
%24 = icmp eq i32 %23, 100
br i1 %24, label %19, label %25
25: ; preds = %19
%26 = tail call i32 (...) @Vl()
%27 = tail call i32 @Vsl(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %28, %25
%29 = phi i32 [ %14, %25 ], [ %31, %28 ]
%30 = mul nsw i32 %29, 98
%31 = sdiv i32 %30, 100
%32 = tail call i32 @TryBaudRate(%struct.SPort* %0, i32 %31, i32 %17)
%33 = icmp eq i32 %32, 100
br i1 %33, label %28, label %34
34: ; preds = %28
%35 = tail call i32 (...) @Vl()
%36 = add nsw i32 %29, %20
%37 = sdiv i32 %36, 2
br label %38
38: ; preds = %1, %34
%39 = phi i32 [ %37, %34 ], [ %2, %1 ]
ret i32 %39
}