#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*/ testcmd;
typedef int /*<<< orphan*/ retbuf;
typedef int /*<<< orphan*/ guesses;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, char, int);
int modifybaud(int, long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *strstr(char *, char *);
int write(int, char const *, int);
long guessbaudrate(int fd) {
  const char testcmd[] = "0100\r\n";
  long guesses[] = {9600, 38400, 115200, 57600, 2400, 1200};
  int i;
  printf("Baudrate guessing: ");
  for (i = 0; i < sizeof(guesses) / sizeof(guesses[0]); i++) {
    long guess = guesses[i];
    printf("%li, ", guess);
    if (-1 == modifybaud(fd, guess)) {
      fprintf(stderr, "Error setting baudrate %li\n", guess);
      return -1;
    }
    char retbuf[1024];
    memset(retbuf, '\0', sizeof(retbuf));
    int nbytes = write(fd, testcmd, sizeof(testcmd));
    if (-1 == nbytes) {
      perror("Error writing to serial port guessing baudrate");
      return -1;
    }
    sleep(1);
    nbytes = read(fd, retbuf, sizeof(retbuf));
    if (-1 == nbytes) {
      perror("Error reading from serial port guessing baudrate");
      return -1;
    }
    if (NULL != strstr(retbuf, ">")) {
      printf("success at %li\n", guess);
      return guess;
    }
  }
  fprintf(stderr, "Couldn't guess baudrate\n");
  return -1;
}


x86 assembly (clang, -O0):
<guessbaudrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %edi,-0xc(%rbp)
mov    0xe5c(%rip),%eax
mov    %eax,-0x13(%rbp)
mov    0xe56(%rip),%ax
mov    %ax,-0xf(%rbp)
mov    0xe4e(%rip),%al
mov    %al,-0xd(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
movq   $0x2580,-0x50(%rbp)
movq   $0x9600,-0x48(%rbp)
movq   $0x1c200,-0x40(%rbp)
movq   $0xe100,-0x38(%rbp)
movq   $0x960,-0x30(%rbp)
movq   $0x4b0,-0x28(%rbp)
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x54(%rbp)
movslq -0x54(%rbp),%rax
cmp    $0x6,%rax
jae    1375 <guessbaudrate+0x1e5>
movslq -0x54(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x60(%rbp),%rsi
call   1080 <modifybaud@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1280 <guessbaudrate+0xf0>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    -0x60(%rbp),%rdx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1394 <guessbaudrate+0x204>
lea    -0x460(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x13(%rbp),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
mov    %eax,-0x464(%rbp)
mov    $0xffffffff,%eax
cmp    -0x464(%rbp),%eax
jne    12d4 <guessbaudrate+0x144>
lea    0xd7b(%rip),%rdi
call   1090 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1394 <guessbaudrate+0x204>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x464(%rbp)
mov    $0xffffffff,%eax
cmp    -0x464(%rbp),%eax
jne    1322 <guessbaudrate+0x192>
lea    0xd5c(%rip),%rdi
call   1090 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1394 <guessbaudrate+0x204>
lea    -0x460(%rbp),%rdi
lea    0xd6d(%rip),%rsi
call   10b0 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1362 <guessbaudrate+0x1d2>
mov    -0x60(%rbp),%rsi
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x60(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1394 <guessbaudrate+0x204>
jmp    1367 <guessbaudrate+0x1d7>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1210 <guessbaudrate+0x80>
mov    0x2c64(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<guessbaudrate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %edi,%r13d
movl   $0xa0d30,0xb(%rsp)
movl   $0x30303130,0x8(%rsp)
30
movaps 0xe45(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe49(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe4d(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe5e(%rip),%rdi
mov    $0x2580,%ebx
mov    $0x2580,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2580,%esi
mov    %r13d,%edi
call   1090 <modifybaud@plt>
cmp    $0xffffffff,%eax
je     12bd <guessbaudrate+0x12d>
mov    $0x2580,%ebx
mov    $0x1,%ebp
lea    0x40(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0xe1c(%rip),%r12
nop
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x7,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     131f <guessbaudrate+0x18f>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     132d <guessbaudrate+0x19d>
mov    %r14,%rdi
mov    $0x3e,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    12d9 <guessbaudrate+0x149>
cmp    $0x6,%rbp
je     12ec <guessbaudrate+0x15c>
mov    0x10(%rsp,%rbp,8),%rbx
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1090 <modifybaud@plt>
add    $0x1,%rbp
cmp    $0xffffffff,%eax
jne    1230 <guessbaudrate+0xa0>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd84(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1303 <guessbaudrate+0x173>
lea    0xded(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    130a <guessbaudrate+0x17a>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x448,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd47(%rip),%rdi
call   10a0 <perror@plt>
jmp    1303 <guessbaudrate+0x173>
lea    0xd68(%rip),%rdi
call   10a0 <perror@plt>
jmp    1303 <guessbaudrate+0x173>

LLVM IR (-O0):
@__const.guessbaudrate.testcmd = [7 x i8] c"0100\0D\0A\00"
@.str = [20 x i8] c"Baudrate guessing: \00"
@.str.1 = [6 x i8] c"%li, \00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Error setting baudrate %li\0A\00"
@.str.3 = [47 x i8] c"Error writing to serial port guessing baudrate\00"
@.str.4 = [49 x i8] c"Error reading from serial port guessing baudrate\00"
@.str.5 = [2 x i8] c">\00"
@.str.6 = [16 x i8] c"success at %li\0A\00"
@.str.7 = [25 x i8] c"Couldn't guess baudrate\0A\00"
define i64 @guessbaudrate(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca [7 x i8]
%5 = alloca [6 x i64]
%6 = alloca i32
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i32
store i32 %0, i32* %3
%10 = bitcast [7 x i8]* %4 to i8*
%11 = bitcast [6 x i64]* %5 to i8*
%12 = bitcast i8* %11 to [6 x i64]*
%13 = getelementptr inbounds [6 x i64], [6 x i64]* %12, i32 0, i32 0
store i64 9600, i64* %13
%14 = getelementptr inbounds [6 x i64], [6 x i64]* %12, i32 0, i32 1
store i64 38400, i64* %14
%15 = getelementptr inbounds [6 x i64], [6 x i64]* %12, i32 0, i32 2
store i64 115200, i64* %15
%16 = getelementptr inbounds [6 x i64], [6 x i64]* %12, i32 0, i32 3
store i64 57600, i64* %16
%17 = getelementptr inbounds [6 x i64], [6 x i64]* %12, i32 0, i32 4
store i64 2400, i64* %17
%18 = getelementptr inbounds [6 x i64], [6 x i64]* %12, i32 0, i32 5
store i64 1200, i64* %18
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %20
20: ; preds = %67, %1
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, 6
br i1 %23, label %24, label %70
24: ; preds = %20
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [6 x i64], [6 x i64]* %5, i64 0, i64 %26
%28 = load i64, i64* %27
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %29)
%31 = load i32, i32* %3
%32 = load i64, i64* %7
%33 = call i32 @modifybaud(i32 %31, i64 %32)
%34 = icmp eq i32 -1, %33
br i1 %34, label %35, label %39
35: ; preds = %24
%36 = load i32, i32* @stderr
%37 = load i64, i64* %7
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i64 %37)
store i64 -1, i64* %2
br label %73
39: ; preds = %24
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = call i32 @memset(i8* %40, i8 signext 0, i32 1024)
%42 = load i32, i32* %3
%43 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%44 = call i32 @write(i32 %42, i8* %43, i32 7)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp eq i32 -1, %45
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i64 -1, i64* %2
br label %73
49: ; preds = %39
%50 = call i32 @sleep(i32 1)
%51 = load i32, i32* %3
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 1024)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp eq i32 -1, %54
br i1 %55, label %56, label %58
56: ; preds = %49
%57 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
store i64 -1, i64* %2
br label %73
58: ; preds = %49
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%60 = call i32* @strstr(i8* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%61 = icmp ne i32* null, %60
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = load i64, i64* %7
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i64 %63)
%65 = load i64, i64* %7
store i64 %65, i64* %2
br label %73
66: ; preds = %58
br label %67
67: ; preds = %66
%68 = load i32, i32* %6
%69 = add nsw i32 %68, 1
store i32 %69, i32* %6
br label %20
70: ; preds = %20
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
store i64 -1, i64* %2
br label %73
73: ; preds = %70, %62, %56, %47, %35
%74 = load i64, i64* %2
ret i64 %74
}

LLVM IR (-O2):
@__const.guessbaudrate.testcmd = [7 x i8] c"0100\0D\0A\00"
@.str = [20 x i8] c"Baudrate guessing: \00"
@.str.1 = [6 x i8] c"%li, \00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"Error setting baudrate %li\0A\00"
@.str.3 = [47 x i8] c"Error writing to serial port guessing baudrate\00"
@.str.4 = [49 x i8] c"Error reading from serial port guessing baudrate\00"
@.str.6 = [16 x i8] c"success at %li\0A\00"
@.str.7 = [25 x i8] c"Couldn't guess baudrate\0A\00"
define i64 @guessbaudrate(i32 %0) {
%2 = alloca [7 x i8]
%3 = alloca [6 x i64]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%6 = bitcast [6 x i64]* %3 to i8*
%7 = bitcast [6 x i64]* %3 to <2 x i64>*
store <2 x i64> <i64 9600, i64 38400>, <2 x i64>* %7
%8 = getelementptr inbounds [6 x i64], [6 x i64]* %3, i64 0, i64 2
%9 = bitcast i64* %8 to <2 x i64>*
store <2 x i64> <i64 115200, i64 57600>, <2 x i64>* %9
%10 = getelementptr inbounds [6 x i64], [6 x i64]* %3, i64 0, i64 4
%11 = bitcast i64* %10 to <2 x i64>*
store <2 x i64> <i64 2400, i64 1200>, <2 x i64>* %11
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 9600)
%15 = call i32 @modifybaud(i32 %0, i64 9600)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %45, %1
%18 = phi i64 [ 9600, %1 ], [ %47, %45 ]
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i64 %18)
br label %54
21: ; preds = %1, %45
%22 = phi i64 [ %43, %45 ], [ 0, %1 ]
%23 = phi i64 [ %47, %45 ], [ 9600, %1 ]
%24 = call i32 @memset(i8* %13, i8 signext 0, i32 1024)
%25 = call i32 @write(i32 %0, i8* %5, i32 7)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %40
29: ; preds = %21
%30 = call i32 @sleep(i32 1)
%31 = call i32 @read(i32 %0, i8* %13, i32 1024)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
br label %40
35: ; preds = %29
%36 = call i8* @strchr(i8* %13, i32 62)
%37 = icmp eq i8* %36, null
br i1 %37, label %42, label %38
38: ; preds = %35
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i64 %23)
br label %40
40: ; preds = %27, %33, %38
%41 = phi i64 [ %23, %38 ], [ -1, %33 ], [ -1, %27 ]
br label %54
42: ; preds = %35
%43 = add nuw nsw i64 %22, 1
%44 = icmp eq i64 %43, 6
br i1 %44, label %51, label %45
45: ; preds = %42
%46 = getelementptr inbounds [6 x i64], [6 x i64]* %3, i64 0, i64 %43
%47 = load i64, i64* %46
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %47)
%49 = call i32 @modifybaud(i32 %0, i64 %47)
%50 = icmp eq i32 %49, -1
br i1 %50, label %17, label %21
51: ; preds = %42
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
br label %54
54: ; preds = %40, %17, %51
%55 = phi i64 [ -1, %51 ], [ -1, %17 ], [ %41, %40 ]
ret i64 %55
}