#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 /*<<< orphan*/ speeds;

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ attempt_upgradebaudrate(int, long, long);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
long upgradebaudrate(int fd, long baudrate_target, long current_baudrate) {
  int old_flags = fcntl(fd, F_GETFL);
  if (-1 == fcntl(fd, F_SETFL, O_NONBLOCK)) {
    perror("fcntl");
  }
  long speeds[] = {38400, 57600, 115200, 230400, 460800, 500000, 576000};
  int i;
  if (-1 == baudrate_target) {
    return 0;
  }
  long current_best = -1;
  printf("Baudrate upgrading: ");
  if (0 < baudrate_target) {
    int retval =
        (0 == attempt_upgradebaudrate(fd, baudrate_target, current_baudrate)
             ? baudrate_target
             : -1);
    fcntl(fd, F_SETFL, old_flags);
    printf("\n");
    return retval;
  }
  for (i = 0; i < sizeof(speeds) / sizeof(speeds[0]); i++) {
    if (0 == attempt_upgradebaudrate(fd, speeds[i], current_best)) {
      current_best = speeds[i];
    }
  }
  printf("\n");
  fcntl(fd, F_SETFL, old_flags);
  return current_best;
}


x86 assembly (clang, -O0):
<upgradebaudrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11b3 <upgradebaudrate+0x63>
lea    0xe52(%rip),%rdi
call   1070 <perror@plt>
lea    -0x60(%rbp),%rdi
lea    0xe62(%rip),%rsi
mov    $0x38,%edx
call   1060 <memcpy@plt>
mov    $0xffffffffffffffff,%rax
cmp    -0x18(%rbp),%rax
jne    11e6 <upgradebaudrate+0x96>
movq   $0x0,-0x8(%rbp)
jmp    12f9 <upgradebaudrate+0x1a9>
movq   $0xffffffffffffffff,-0x70(%rbp)
ff
lea    0xe11(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jge    1277 <upgradebaudrate+0x127>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <attempt_upgradebaudrate@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1231 <upgradebaudrate+0xe1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
jmp    1241 <upgradebaudrate+0xf1>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x80(%rbp)
jmp    1241 <upgradebaudrate+0xf1>
mov    -0x80(%rbp),%rax
mov    %eax,-0x74(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
lea    0xdb6(%rip),%rdi
call   1050 <printf@plt>
movslq -0x74(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f9 <upgradebaudrate+0x1a9>
movl   $0x0,-0x64(%rbp)
movslq -0x64(%rbp),%rax
cmp    $0x7,%rax
jae    12cf <upgradebaudrate+0x17f>
mov    -0xc(%rbp),%edi
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rsi
mov    -0x70(%rbp),%rdx
call   1030 <attempt_upgradebaudrate@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12bc <upgradebaudrate+0x16c>
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,8),%rax
mov    %rax,-0x70(%rbp)
jmp    12c1 <upgradebaudrate+0x171>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    127e <upgradebaudrate+0x12e>
lea    0xd45(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x70(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<upgradebaudrate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%ebx
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    %eax,%r14d
mov    0x2e5e(%rip),%r12
mov    (%r12),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1050 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12d3 <upgradebaudrate+0x183>
cmp    $0xffffffffffffffff,%rbp
je     12e9 <upgradebaudrate+0x199>
lea    0xe5c(%rip),%rdi
call   1060 <printf@plt>
test   %rbp,%rbp
jle    11ef <upgradebaudrate+0x9f>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1040 <attempt_upgradebaudrate@plt>
mov    %rax,%r15
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movslq %ebp,%rax
neg    %r15
sbb    %rbp,%rbp
or     %rax,%rbp
jmp    12eb <upgradebaudrate+0x19b>
mov    $0x9600,%esi
mov    %ebx,%edi
mov    $0xffffffffffffffff,%rdx
call   1040 <attempt_upgradebaudrate@plt>
neg    %rax
sbb    %rbp,%rbp
or     $0x9600,%rbp
mov    $0xe100,%r15d
mov    $0xe100,%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <attempt_upgradebaudrate@plt>
test   %rax,%rax
cmove  %r15,%rbp
mov    $0x1c200,%r15d
mov    $0x1c200,%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <attempt_upgradebaudrate@plt>
test   %rax,%rax
cmove  %r15,%rbp
mov    $0x38400,%r15d
mov    $0x38400,%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <attempt_upgradebaudrate@plt>
test   %rax,%rax
cmove  %r15,%rbp
mov    $0x70800,%r15d
mov    $0x70800,%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <attempt_upgradebaudrate@plt>
test   %rax,%rax
cmove  %r15,%rbp
mov    $0x7a120,%r15d
mov    $0x7a120,%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <attempt_upgradebaudrate@plt>
test   %rax,%rax
cmove  %r15,%rbp
mov    $0x8ca00,%r15d
mov    $0x8ca00,%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1040 <attempt_upgradebaudrate@plt>
test   %rax,%rax
cmove  %r15,%rbp
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <fcntl@plt>
jmp    12eb <upgradebaudrate+0x19b>
lea    0xd26(%rip),%rdi
call   1070 <perror@plt>
cmp    $0xffffffffffffffff,%rbp
jne    11a3 <upgradebaudrate+0x53>
xor    %ebp,%ebp
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [6 x i8] c"fcntl\00"
@__const.upgradebaudrate.speeds = [7 x i64] [i64 38400, i64 57600, i64 115200, i64 230400, i64 460800, i64 500000, i64 576000]
@.str.1 = [21 x i8] c"Baudrate upgrading: \00"
@.str.2 = [2 x i8] c"\0A\00"
define i64 @upgradebaudrate(i32 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca [7 x i64]
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%13 = load i32, i32* %5
%14 = load i32, i32* @F_GETFL
%15 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %14)
store i32 %15, i32* %8
%16 = load i32, i32* %5
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @O_NONBLOCK
%19 = call i32 (i32, i32, ...) @fcntl(i32 %16, i32 %17, i32 %18)
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %3
%24 = bitcast [7 x i64]* %9 to i8*
%25 = load i64, i64* %6
%26 = icmp eq i64 -1, %25
br i1 %26, label %27, label %28
27: ; preds = %23
store i64 0, i64* %4
br label %81
28: ; preds = %23
store i64 -1, i64* %11
%29 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%30 = load i64, i64* %6
%31 = icmp slt i64 0, %30
br i1 %31, label %32, label %51
32: ; preds = %28
%33 = load i32, i32* %5
%34 = load i64, i64* %6
%35 = load i64, i64* %7
%36 = call i64 @attempt_upgradebaudrate(i32 %33, i64 %34, i64 %35)
%37 = icmp eq i64 0, %36
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = load i64, i64* %6
br label %41
40: ; preds = %32
br label %41
41: ; preds = %40, %38
%42 = phi i64 [ %39, %38 ], [ -1, %40 ]
%43 = trunc i64 %42 to i32
store i32 %43, i32* %12
%44 = load i32, i32* %5
%45 = load i32, i32* @F_SETFL
%46 = load i32, i32* %8
%47 = call i32 (i32, i32, ...) @fcntl(i32 %44, i32 %45, i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
store i64 %50, i64* %4
br label %81
51: ; preds = %28
store i32 0, i32* %10
br label %52
52: ; preds = %71, %51
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = icmp ult i64 %54, 7
br i1 %55, label %56, label %74
56: ; preds = %52
%57 = load i32, i32* %5
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [7 x i64], [7 x i64]* %9, i64 0, i64 %59
%61 = load i64, i64* %60
%62 = load i64, i64* %11
%63 = call i64 @attempt_upgradebaudrate(i32 %57, i64 %61, i64 %62)
%64 = icmp eq i64 0, %63
br i1 %64, label %65, label %70
65: ; preds = %56
%66 = load i32, i32* %10
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [7 x i64], [7 x i64]* %9, i64 0, i64 %67
%69 = load i64, i64* %68
store i64 %69, i64* %11
br label %70
70: ; preds = %65, %56
br label %71
71: ; preds = %70
%72 = load i32, i32* %10
%73 = add nsw i32 %72, 1
store i32 %73, i32* %10
br label %52
74: ; preds = %52
%75 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%76 = load i32, i32* %5
%77 = load i32, i32* @F_SETFL
%78 = load i32, i32* %8
%79 = call i32 (i32, i32, ...) @fcntl(i32 %76, i32 %77, i32 %78)
%80 = load i64, i64* %11
store i64 %80, i64* %4
br label %81
81: ; preds = %74, %41, %27
%82 = load i64, i64* %4
ret i64 %82
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [6 x i8] c"fcntl\00"
@.str.1 = [21 x i8] c"Baudrate upgrading: \00"
define i64 @upgradebaudrate(i32 %0, i64 %1, i64 %2) {
%4 = load i32, i32* @F_GETFL
%5 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %4)
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %3
%13 = icmp eq i64 %1, -1
br i1 %13, label %51, label %14
14: ; preds = %12
%15 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp sgt i64 %1, 0
br i1 %16, label %42, label %17
17: ; preds = %14
%18 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 38400, i64 -1)
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, i64 38400, i64 -1
%21 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 57600, i64 %20)
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i64 57600, i64 %20
%24 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 115200, i64 %23)
%25 = icmp eq i64 %24, 0
%26 = select i1 %25, i64 115200, i64 %23
%27 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 230400, i64 %26)
%28 = icmp eq i64 %27, 0
%29 = select i1 %28, i64 230400, i64 %26
%30 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 460800, i64 %29)
%31 = icmp eq i64 %30, 0
%32 = select i1 %31, i64 460800, i64 %29
%33 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 500000, i64 %32)
%34 = icmp eq i64 %33, 0
%35 = select i1 %34, i64 500000, i64 %32
%36 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 576000, i64 %35)
%37 = icmp eq i64 %36, 0
%38 = select i1 %37, i64 576000, i64 %35
%39 = tail call i32 @putchar(i32 10)
%40 = load i32, i32* @F_SETFL
%41 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %40, i32 %5)
br label %51
42: ; preds = %14
%43 = tail call i64 @attempt_upgradebaudrate(i32 %0, i64 %1, i64 %2)
%44 = icmp eq i64 %43, 0
%45 = load i32, i32* @F_SETFL
%46 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %45, i32 %5)
%47 = tail call i32 @putchar(i32 10)
%48 = shl i64 %1, 32
%49 = ashr exact i64 %48, 32
%50 = select i1 %44, i64 %49, i64 -1
br label %51
51: ; preds = %42, %17, %12
%52 = phi i64 [ 0, %12 ], [ %50, %42 ], [ %38, %17 ]
ret i64 %52
}