#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 timeval {
  long tv_sec;
  long tv_usec;
};
typedef int /*<<< orphan*/ elm_response2;
typedef int /*<<< orphan*/ elm_response;
typedef int /*<<< orphan*/ brt_cmd;
typedef int /*<<< orphan*/ brd_cmd;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ blindcmd(int, char *, int);
scalar_t__ errno;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
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*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ *strstr(char *, char *);
int write(int, char *, int /*<<< orphan*/);
long attempt_upgradebaudrate(int fd, long rate, long previousrate) {
  char brd_cmd[64];
  int timeout = 500;
  int brt_val = timeout / 5;
  char brt_cmd[64];
  snprintf(brt_cmd, sizeof(brt_cmd), "ATBRT%02X", brt_val);
  blindcmd(fd, brt_cmd, 1);
  int brd_val = 4000000l / rate;
  snprintf(brd_cmd, sizeof(brd_cmd),
           "ATBRD%02X"
           "\r",
           brd_val);
  printf("%li [%02X]:", rate, brd_val);
  int nbytes = write(fd, brd_cmd, strlen(brd_cmd));
  if (-1 == nbytes) {
    printf("\n");
    perror("Error writing to serial port upgrading baudrate");
    return -1;
  }
  char elm_response[64];
  memset(elm_response, '\0', sizeof(elm_response));
  int readcount = 0;
  while (NULL == strstr(elm_response, "?") &&
         NULL == strstr(elm_response, "OK")) {
    nbytes =
        read(fd, elm_response + readcount, sizeof(elm_response) - readcount);
    if (-1 == nbytes && errno != EAGAIN) {
      printf("\n");
      perror("Error reading[1] from serial port upgrading baudrate");
      return -1;
    }
    if (-1 != nbytes) {
      readcount += nbytes;
    }
  }
  if (NULL != strstr(elm_response, "OK")) {
    if (-1 == modifybaud(fd, rate)) {
      printf("Error modifying baudrate to %li\n", rate);
      return -1;
    }
    char elm_response2[256];
    memset(elm_response2, '\0', sizeof(elm_response2));
    struct timeval start, end;
    gettimeofday(&start, NULL);
    int readcount2 = 0;
    while (readcount2 < 5 && NULL == strstr(elm_response2, ">")) {
      nbytes = read(fd, elm_response2 + readcount2,
                    sizeof(elm_response2) - readcount2);
      if (-1 == nbytes && EAGAIN != errno) {
        perror("Error reading[2] from serial port upgrading baudrate");
        modifybaud(fd, previousrate);
        return -1;
      }
      gettimeofday(&end, NULL);
      long timediff = (end.tv_sec - start.tv_sec) * 1000000l +
                      (end.tv_usec - start.tv_usec);
      if (2000l * timeout < timediff) {
        printf("timed out\n");
        break;
      }
      if (-1 != nbytes) {
        readcount2 += nbytes;
      }
    }
    if (NULL != strstr(elm_response2, "ELM")) {
      write(fd, "\r", strlen("\r"));
      printf("success, ");
      return 0;
    } else {
      printf("fail [no ELM], ");
      modifybaud(fd, previousrate);
      return -1;
    }
  } else {
    printf("fail [no OK], ");
    modifybaud(fd, previousrate);
    return -1;
  }
}


x86 assembly (clang, -O0):
<attempt_upgradebaudrate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1f4,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %eax,-0x68(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x68(%rbp),%ecx
mov    $0x40,%esi
lea    0xe0f(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <blindcmd@plt>
mov    $0x3d0900,%eax
cqto
idivq  -0x18(%rbp)
mov    %eax,-0xb4(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0xb4(%rbp),%ecx
mov    $0x40,%esi
lea    0xdd9(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x18(%rbp),%rsi
mov    -0xb4(%rbp),%edx
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x24c(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24c(%rbp),%edi
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0xb8(%rbp)
mov    $0xffffffff,%eax
cmp    -0xb8(%rbp),%eax
jne    12bd <attempt_upgradebaudrate+0x10d>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd76(%rip),%rdi
call   10c0 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1676 <attempt_upgradebaudrate+0x4c6>
lea    -0x100(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1080 <memset@plt>
movl   $0x0,-0x104(%rbp)
lea    -0x100(%rbp),%rdi
lea    0xd69(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,%rdx
xor    %eax,%eax
xor    %ecx,%ecx
cmp    %rdx,%rcx
mov    %al,-0x24d(%rbp)
jne    1327 <attempt_upgradebaudrate+0x177>
lea    -0x100(%rbp),%rdi
lea    0xd42(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
mov    %al,-0x24d(%rbp)
mov    -0x24d(%rbp),%al
test   $0x1,%al
jne    133a <attempt_upgradebaudrate+0x18a>
jmp    13e4 <attempt_upgradebaudrate+0x234>
mov    -0xc(%rbp),%edi
lea    -0x100(%rbp),%rsi
movslq -0x104(%rbp),%rax
add    %rax,%rsi
movslq -0x104(%rbp),%rcx
mov    $0x40,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1090 <read@plt>
mov    %eax,-0xb8(%rbp)
mov    $0xffffffff,%eax
cmp    -0xb8(%rbp),%eax
jne    13bc <attempt_upgradebaudrate+0x20c>
mov    0x2c5e(%rip),%rax
mov    (%rax),%rax
mov    0x2c44(%rip),%rcx
cmp    (%rcx),%rax
je     13bc <attempt_upgradebaudrate+0x20c>
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xcac(%rip),%rdi
call   10c0 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1676 <attempt_upgradebaudrate+0x4c6>
mov    $0xffffffff,%eax
cmp    -0xb8(%rbp),%eax
je     13df <attempt_upgradebaudrate+0x22f>
mov    -0xb8(%rbp),%eax
add    -0x104(%rbp),%eax
mov    %eax,-0x104(%rbp)
jmp    12da <attempt_upgradebaudrate+0x12a>
lea    -0x100(%rbp),%rdi
lea    0xc61(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1654 <attempt_upgradebaudrate+0x4a4>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   10b0 <modifybaud@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    143f <attempt_upgradebaudrate+0x28f>
mov    -0x18(%rbp),%rsi
lea    0xc60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1676 <attempt_upgradebaudrate+0x4c6>
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x220(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
movl   $0x0,-0x234(%rbp)
xor    %eax,%eax
cmpl   $0x5,-0x234(%rbp)
mov    %al,-0x24e(%rbp)
jge    14a5 <attempt_upgradebaudrate+0x2f5>
lea    -0x210(%rbp),%rdi
lea    0xc1d(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
mov    %al,-0x24e(%rbp)
mov    -0x24e(%rbp),%al
test   $0x1,%al
jne    14b8 <attempt_upgradebaudrate+0x308>
jmp    15c8 <attempt_upgradebaudrate+0x418>
mov    -0xc(%rbp),%edi
lea    -0x210(%rbp),%rsi
movslq -0x234(%rbp),%rax
add    %rax,%rsi
movslq -0x234(%rbp),%rcx
mov    $0x100,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1090 <read@plt>
mov    %eax,-0xb8(%rbp)
mov    $0xffffffff,%eax
cmp    -0xb8(%rbp),%eax
jne    1538 <attempt_upgradebaudrate+0x388>
mov    0x2ad0(%rip),%rax
mov    (%rax),%rax
mov    0x2ad6(%rip),%rcx
cmp    (%rcx),%rax
je     1538 <attempt_upgradebaudrate+0x388>
lea    0xb94(%rip),%rdi
call   10c0 <perror@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   10b0 <modifybaud@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1676 <attempt_upgradebaudrate+0x4c6>
lea    -0x230(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x230(%rbp),%rax
sub    -0x220(%rbp),%rax
imul   $0xf4240,%rax,%rax
mov    -0x228(%rbp),%rcx
sub    -0x218(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x240(%rbp)
movslq -0x64(%rbp),%rax
imul   $0x7d0,%rax,%rax
cmp    -0x240(%rbp),%rax
jge    15a0 <attempt_upgradebaudrate+0x3f0>
lea    0xb4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    15c8 <attempt_upgradebaudrate+0x418>
mov    $0xffffffff,%eax
cmp    -0xb8(%rbp),%eax
je     15c3 <attempt_upgradebaudrate+0x413>
mov    -0xb8(%rbp),%eax
add    -0x234(%rbp),%eax
mov    %eax,-0x234(%rbp)
jmp    146c <attempt_upgradebaudrate+0x2bc>
lea    -0x210(%rbp),%rdi
lea    0xb18(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     162d <attempt_upgradebaudrate+0x47d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x254(%rbp)
lea    0xa1a(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x254(%rbp),%edi
mov    %eax,%edx
lea    0xa06(%rip),%rsi
call   1030 <write@plt>
lea    0xad9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1676 <attempt_upgradebaudrate+0x4c6>
lea    0xac8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   10b0 <modifybaud@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1676 <attempt_upgradebaudrate+0x4c6>
lea    0xab1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   10b0 <modifybaud@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attempt_upgradebaudrate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f8,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r13d
lea    0xdff(%rip),%rdx
lea    0x170(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    $0x64,%ecx
call   1090 <snprintf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   10d0 <blindcmd@plt>
mov    %r15,%rax
shr    $0x20,%rax
je     1243 <attempt_upgradebaudrate+0x63>
mov    $0x3d0900,%eax
xor    %edx,%edx
idiv   %r15
mov    %rax,%rbx
jmp    124f <attempt_upgradebaudrate+0x6f>
mov    $0x3d0900,%eax
xor    %edx,%edx
div    %r15d
mov    %eax,%ebx
lea    0xdb4(%rip),%rdx
lea    0x1b0(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x40,%esi
mov    %ebx,%ecx
call   1090 <snprintf@plt>
lea    0xda1(%rip),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1508 <attempt_upgradebaudrate+0x328>
mov    %r14,0x8(%rsp)
lea    0x30(%rsp),%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   10b0 <memset@plt>
mov    %rbx,%rdi
mov    $0x3f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     13d0 <attempt_upgradebaudrate+0x1f0>
lea    0xd7b(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1100 <strstr@plt>
test   %rax,%rax
je     1465 <attempt_upgradebaudrate+0x285>
mov    %r13d,%edi
mov    %r15,%rsi
call   10e0 <modifybaud@plt>
cmp    $0xffffffff,%eax
je     146e <attempt_upgradebaudrate+0x28e>
lea    0x70(%rsp),%rdi
xor    %ebp,%ebp
xor    %esi,%esi
mov    $0x100,%edx
call   10b0 <memset@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   10a0 <gettimeofday@plt>
mov    0x20(%rsp),%r14
mov    0x28(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    133b <attempt_upgradebaudrate+0x15b>
nopl   (%rax)
add    %eax,%ebp
cmp    $0x5,%ebp
jge    148d <attempt_upgradebaudrate+0x2ad>
lea    0x70(%rsp),%rdi
mov    $0x3e,%esi
call   1070 <strchr@plt>
test   %rax,%rax
jne    148d <attempt_upgradebaudrate+0x2ad>
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x70,%rsi
mov    $0x100,%edx
sub    %eax,%edx
mov    %r13d,%edi
call   10c0 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    138e <attempt_upgradebaudrate+0x1ae>
mov    0x2c55(%rip),%rax
mov    (%rax),%rax
mov    0x2c5b(%rip),%rcx
cmp    (%rcx),%rax
jne    1520 <attempt_upgradebaudrate+0x340>
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <gettimeofday@plt>
mov    0x10(%rsp),%rax
sub    %r14,%rax
mov    0x18(%rsp),%rcx
sub    %r15,%rcx
imul   $0xf4240,%rax,%rax
add    %rcx,%rax
cmp    $0xf4241,%rax
jge    1481 <attempt_upgradebaudrate+0x2a1>
xor    %eax,%eax
cmp    $0xffffffff,%ebx
je     1330 <attempt_upgradebaudrate+0x150>
mov    %ebx,%eax
jmp    1330 <attempt_upgradebaudrate+0x150>
lea    0xc7a(%rip),%r12
lea    0x30(%rsp),%rbx
mov    0x2bed(%rip),%r14
jmp    1408 <attempt_upgradebaudrate+0x228>
cs nopw 0x0(%rax,%rax,1)
nop
add    %eax,%ebp
mov    %rbx,%rdi
mov    $0x3f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
jne    12cf <attempt_upgradebaudrate+0xef>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1100 <strstr@plt>
test   %rax,%rax
jne    12cf <attempt_upgradebaudrate+0xef>
movslq %ebp,%rax
lea    (%rsp,%rax,1),%rsi
add    $0x30,%rsi
mov    $0x40,%edx
sub    %eax,%edx
mov    %r13d,%edi
call   10c0 <read@plt>
cmp    $0xffffffff,%eax
jne    13f0 <attempt_upgradebaudrate+0x210>
mov    0x2b9e(%rip),%rax
mov    (%rax),%rax
cmp    (%r14),%rax
je     13f2 <attempt_upgradebaudrate+0x212>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xbf9(%rip),%rdi
call   10f0 <perror@plt>
jmp    14ec <attempt_upgradebaudrate+0x30c>
lea    0xc91(%rip),%rdi
jmp    14d8 <attempt_upgradebaudrate+0x2f8>
lea    0xc14(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    14ec <attempt_upgradebaudrate+0x30c>
lea    0xc84(%rip),%rdi
call   1040 <puts@plt>
lea    0xc4b(%rip),%rsi
lea    0x70(%rsp),%rdi
call   1100 <strstr@plt>
test   %rax,%rax
je     14d1 <attempt_upgradebaudrate+0x2f1>
lea    0xb69(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    0xc1d(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <printf@plt>
jmp    14f3 <attempt_upgradebaudrate+0x313>
lea    0xc15(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r13d,%edi
mov    0x8(%rsp),%rsi
call   10e0 <modifybaud@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x1f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xb08(%rip),%rdi
call   10f0 <perror@plt>
jmp    14ec <attempt_upgradebaudrate+0x30c>
lea    0xb83(%rip),%rdi
call   10f0 <perror@plt>
jmp    14df <attempt_upgradebaudrate+0x2ff>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [10 x i8] c"ATBRT%02X\00"
@.str.1 = [11 x i8] c"ATBRD%02X\0D\00"
@.str.2 = [12 x i8] c"%li [%02X]:\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [48 x i8] c"Error writing to serial port upgrading baudrate\00"
@.str.5 = [2 x i8] c"?\00"
@.str.6 = [3 x i8] c"OK\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.7 = [53 x i8] c"Error reading[1] from serial port upgrading baudrate\00"
@.str.8 = [33 x i8] c"Error modifying baudrate to %li\0A\00"
@.str.9 = [2 x i8] c">\00"
@.str.10 = [53 x i8] c"Error reading[2] from serial port upgrading baudrate\00"
@.str.11 = [11 x i8] c"timed out\0A\00"
@.str.12 = [4 x i8] c"ELM\00"
@.str.13 = [2 x i8] c"\0D\00"
@.str.14 = [10 x i8] c"success, \00"
@.str.15 = [16 x i8] c"fail [no ELM], \00"
@.str.16 = [15 x i8] c"fail [no OK], \00"
define i64 @attempt_upgradebaudrate(i32 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca [64 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca [64 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca [64 x i8]
%15 = alloca i32
%16 = alloca [256 x i8]
%17 = alloca %struct.timeval
%18 = alloca %struct.timeval
%19 = alloca i32
%20 = alloca i64
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 500, i32* %9
%21 = load i32, i32* %9
%22 = sdiv i32 %21, 5
store i32 %22, i32* %10
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%24 = load i32, i32* %10
%25 = call i32 @snprintf(i8* %23, i32 64, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %5
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%28 = call i32 @blindcmd(i32 %26, i8* %27, i32 1)
%29 = load i64, i64* %6
%30 = sdiv i64 4000000, %29
%31 = trunc i64 %30 to i32
store i32 %31, i32* %12
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%33 = load i32, i32* %12
%34 = call i32 @snprintf(i8* %32, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i64, i64* %6
%36 = load i32, i32* %12
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %35, i32 %36)
%38 = load i32, i32* %5
%39 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @write(i32 %38, i8* %39, i32 %41)
store i32 %42, i32* %13
%43 = load i32, i32* %13
%44 = icmp eq i32 -1, %43
br i1 %44, label %45, label %48
45: ; preds = %3
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @perror(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
store i64 -1, i64* %4
br label %184
48: ; preds = %3
%49 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%50 = call i32 @memset(i8* %49, i8 signext 0, i32 64)
store i32 0, i32* %15
br label %51
51: ; preds = %88, %48
%52 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%53 = call i32* @strstr(i8* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%54 = icmp eq i32* null, %53
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%57 = call i32* @strstr(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%58 = icmp eq i32* null, %57
br label %59
59: ; preds = %55, %51
%60 = phi i1 [ false, %51 ], [ %58, %55 ]
br i1 %60, label %61, label %89
61: ; preds = %59
%62 = load i32, i32* %5
%63 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%64 = load i32, i32* %15
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i32, i32* %15
%68 = sext i32 %67 to i64
%69 = sub i64 64, %68
%70 = trunc i64 %69 to i32
%71 = call i32 @read(i32 %62, i8* %66, i32 %70)
store i32 %71, i32* %13
%72 = load i32, i32* %13
%73 = icmp eq i32 -1, %72
br i1 %73, label %74, label %81
74: ; preds = %61
%75 = load i64, i64* @errno
%76 = load i64, i64* @EAGAIN
%77 = icmp ne i64 %75, %76
br i1 %77, label %78, label %81
78: ; preds = %74
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%80 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
store i64 -1, i64* %4
br label %184
81: ; preds = %74, %61
%82 = load i32, i32* %13
%83 = icmp ne i32 -1, %82
br i1 %83, label %84, label %88
84: ; preds = %81
%85 = load i32, i32* %13
%86 = load i32, i32* %15
%87 = add nsw i32 %86, %85
store i32 %87, i32* %15
br label %88
88: ; preds = %84, %81
br label %51
89: ; preds = %59
%90 = getelementptr inbounds [64 x i8], [64 x i8]* %14, i64 0, i64 0
%91 = call i32* @strstr(i8* %90, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%92 = icmp ne i32* null, %91
br i1 %92, label %93, label %179
93: ; preds = %89
%94 = load i32, i32* %5
%95 = load i64, i64* %6
%96 = call i32 @modifybaud(i32 %94, i64 %95)
%97 = icmp eq i32 -1, %96
br i1 %97, label %98, label %101
98: ; preds = %93
%99 = load i64, i64* %6
%100 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i64 %99)
store i64 -1, i64* %4
br label %184
101: ; preds = %93
%102 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%103 = call i32 @memset(i8* %102, i8 signext 0, i32 256)
%104 = call i32 @gettimeofday(%struct.timeval* %17, i32* null)
store i32 0, i32* %19
br label %105
105: ; preds = %164, %101
%106 = load i32, i32* %19
%107 = icmp slt i32 %106, 5
br i1 %107, label %108, label %112
108: ; preds = %105
%109 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%110 = call i32* @strstr(i8* %109, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%111 = icmp eq i32* null, %110
br label %112
112: ; preds = %108, %105
%113 = phi i1 [ false, %105 ], [ %111, %108 ]
br i1 %113, label %114, label %165
114: ; preds = %112
%115 = load i32, i32* %5
%116 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%117 = load i32, i32* %19
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds i8, i8* %116, i64 %118
%120 = load i32, i32* %19
%121 = sext i32 %120 to i64
%122 = sub i64 256, %121
%123 = trunc i64 %122 to i32
%124 = call i32 @read(i32 %115, i8* %119, i32 %123)
store i32 %124, i32* %13
%125 = load i32, i32* %13
%126 = icmp eq i32 -1, %125
br i1 %126, label %127, label %136
127: ; preds = %114
%128 = load i64, i64* @EAGAIN
%129 = load i64, i64* @errno
%130 = icmp ne i64 %128, %129
br i1 %130, label %131, label %136
131: ; preds = %127
%132 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.10, i64 0, i64 0))
%133 = load i32, i32* %5
%134 = load i64, i64* %7
%135 = call i32 @modifybaud(i32 %133, i64 %134)
store i64 -1, i64* %4
br label %184
136: ; preds = %127, %114
%137 = call i32 @gettimeofday(%struct.timeval* %18, i32* null)
%138 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
%139 = load i64, i64* %138
%140 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 0
%141 = load i64, i64* %140
%142 = sub nsw i64 %139, %141
%143 = mul nsw i64 %142, 1000000
%144 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
%145 = load i64, i64* %144
%146 = getelementptr inbounds %struct.timeval, %struct.timeval* %17, i32 0, i32 1
%147 = load i64, i64* %146
%148 = sub nsw i64 %145, %147
%149 = add nsw i64 %143, %148
store i64 %149, i64* %20
%150 = load i32, i32* %9
%151 = sext i32 %150 to i64
%152 = mul nsw i64 2000, %151
%153 = load i64, i64* %20
%154 = icmp slt i64 %152, %153
br i1 %154, label %155, label %157
155: ; preds = %136
%156 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0))
br label %165
157: ; preds = %136
%158 = load i32, i32* %13
%159 = icmp ne i32 -1, %158
br i1 %159, label %160, label %164
160: ; preds = %157
%161 = load i32, i32* %13
%162 = load i32, i32* %19
%163 = add nsw i32 %162, %161
store i32 %163, i32* %19
br label %164
164: ; preds = %160, %157
br label %105
165: ; preds = %155, %112
%166 = getelementptr inbounds [256 x i8], [256 x i8]* %16, i64 0, i64 0
%167 = call i32* @strstr(i8* %166, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0))
%168 = icmp ne i32* null, %167
br i1 %168, label %169, label %174
169: ; preds = %165
%170 = load i32, i32* %5
%171 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0))
%172 = call i32 @write(i32 %170, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0), i32 %171)
%173 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.14, i64 0, i64 0))
store i64 0, i64* %4
br label %184
174: ; preds = %165
%175 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.15, i64 0, i64 0))
%176 = load i32, i32* %5
%177 = load i64, i64* %7
%178 = call i32 @modifybaud(i32 %176, i64 %177)
store i64 -1, i64* %4
br label %184
179: ; preds = %89
%180 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.16, i64 0, i64 0))
%181 = load i32, i32* %5
%182 = load i64, i64* %7
%183 = call i32 @modifybaud(i32 %181, i64 %182)
store i64 -1, i64* %4
br label %184
184: ; preds = %179, %174, %169, %131, %98, %78, %45
%185 = load i64, i64* %4
ret i64 %185
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [10 x i8] c"ATBRT%02X\00"
@.str.1 = [11 x i8] c"ATBRD%02X\0D\00"
@.str.2 = [12 x i8] c"%li [%02X]:\00"
@.str.4 = [48 x i8] c"Error writing to serial port upgrading baudrate\00"
@.str.6 = [3 x i8] c"OK\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.7 = [53 x i8] c"Error reading[1] from serial port upgrading baudrate\00"
@.str.8 = [33 x i8] c"Error modifying baudrate to %li\0A\00"
@.str.10 = [53 x i8] c"Error reading[2] from serial port upgrading baudrate\00"
@.str.12 = [4 x i8] c"ELM\00"
@.str.13 = [2 x i8] c"\0D\00"
@.str.14 = [10 x i8] c"success, \00"
@.str.15 = [16 x i8] c"fail [no ELM], \00"
@.str.16 = [15 x i8] c"fail [no OK], \00"
@str = [10 x i8] c"timed out\00"
define i64 @attempt_upgradebaudrate(i32 %0, i64 %1, i64 %2) {
%4 = alloca [64 x i8]
%5 = alloca [64 x i8]
%6 = alloca [64 x i8]
%7 = alloca [256 x i8]
%8 = alloca %struct.timeval
%9 = alloca %struct.timeval
%10 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%12 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 100)
%13 = call i32 @blindcmd(i32 %0, i8* %11, i32 1)
%14 = sdiv i64 4000000, %1
%15 = trunc i64 %14 to i32
%16 = call i32 @snprintf(i8* %10, i32 64, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %1, i32 %15)
%18 = call i32 @strlen(i8* %10)
%19 = call i32 @write(i32 %0, i8* %10, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = call i32 @putchar(i32 10)
%23 = call i32 @perror(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0))
br label %121
24: ; preds = %3
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i8 signext 0, i32 64)
%27 = call i8* @strchr(i8* %25, i32 63)
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %52
29: ; preds = %24, %48
%30 = phi i32 [ %49, %48 ], [ 0, %24 ]
%31 = call i32* @strstr(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %52
33: ; preds = %29
%34 = sext i32 %30 to i64
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 %34
%36 = sub i32 64, %30
%37 = call i32 @read(i32 %0, i8* %35, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %46
39: ; preds = %33
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp eq i64 %40, %41
br i1 %42, label %48, label %43
43: ; preds = %39
%44 = call i32 @putchar(i32 10)
%45 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0))
br label %119
46: ; preds = %33
%47 = add nsw i32 %37, %30
br label %48
48: ; preds = %39, %46
%49 = phi i32 [ %47, %46 ], [ %30, %39 ]
%50 = call i8* @strchr(i8* %25, i32 63)
%51 = icmp eq i8* %50, null
br i1 %51, label %29, label %52
52: ; preds = %29, %48, %24
%53 = call i32* @strstr(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%54 = icmp eq i32* %53, null
br i1 %54, label %116, label %55
55: ; preds = %52
%56 = call i32 @modifybaud(i32 %0, i64 %1)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i64 %1)
br label %119
60: ; preds = %55
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%62 = call i32 @memset(i8* %61, i8 signext 0, i32 256)
%63 = bitcast %struct.timeval* %8 to i8*
%64 = bitcast %struct.timeval* %9 to i8*
%65 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
%66 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 0
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
%68 = load i64, i64* %67
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 1
%70 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
%71 = load i64, i64* %70
br label %72
72: ; preds = %60, %100
%73 = phi i32 [ 0, %60 ], [ %102, %100 ]
%74 = call i8* @strchr(i8* %61, i32 62)
%75 = icmp eq i8* %74, null
br i1 %75, label %76, label %104
76: ; preds = %72
%77 = sext i32 %73 to i64
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %77
%79 = sub i32 256, %73
%80 = call i32 @read(i32 %0, i8* %78, i32 %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %89
82: ; preds = %76
%83 = load i64, i64* @EAGAIN
%84 = load i64, i64* @errno
%85 = icmp eq i64 %83, %84
br i1 %85, label %89, label %86
86: ; preds = %82
%87 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.10, i64 0, i64 0))
%88 = call i32 @modifybaud(i32 %0, i64 %2)
br label %114
89: ; preds = %82, %76
%90 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
%91 = load i64, i64* %66
%92 = sub nsw i64 %91, %68
%93 = mul nsw i64 %92, 1000000
%94 = load i64, i64* %69
%95 = sub i64 %94, %71
%96 = add nsw i64 %95, %93
%97 = icmp sgt i64 %96, 1000000
br i1 %97, label %98, label %100
98: ; preds = %89
%99 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %104
100: ; preds = %89
%101 = select i1 %81, i32 0, i32 %80
%102 = add nsw i32 %101, %73
%103 = icmp slt i32 %102, 5
br i1 %103, label %72, label %104
104: ; preds = %100, %72, %98
%105 = call i32* @strstr(i8* %61, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.12, i64 0, i64 0))
%106 = icmp eq i32* %105, null
br i1 %106, label %111, label %107
107: ; preds = %104
%108 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0))
%109 = call i32 @write(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.13, i64 0, i64 0), i32 %108)
%110 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.14, i64 0, i64 0))
br label %114
111: ; preds = %104
%112 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.15, i64 0, i64 0))
%113 = call i32 @modifybaud(i32 %0, i64 %2)
br label %114
114: ; preds = %111, %107, %86
%115 = phi i64 [ -1, %86 ], [ 0, %107 ], [ -1, %111 ]
br label %119
116: ; preds = %52
%117 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.16, i64 0, i64 0))
%118 = call i32 @modifybaud(i32 %0, i64 %2)
br label %119
119: ; preds = %116, %114, %58, %43
%120 = phi i64 [ -1, %43 ], [ -1, %58 ], [ %115, %114 ], [ -1, %116 ]
br label %121
121: ; preds = %119, %21
%122 = phi i64 [ -1, %21 ], [ %120, %119 ]
ret i64 %122
}