#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 termios {
  int dummy;
};
typedef int /*<<< orphan*/ speed_t;

/* Variables and functions */
int /*<<< orphan*/ B9600;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ buildpacket(unsigned char *, int *, int, unsigned char *,
                               int);
int /*<<< orphan*/ cfsetspeed(struct termios *, int /*<<< orphan*/);
int checkrom(int);
int /*<<< orphan*/ convlong(unsigned char *, long);
int listen(int, unsigned char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ readline(int /*<<< orphan*/, int, unsigned char *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ write(int, unsigned char *, int);
int changebaud(int fd, long int speed, speed_t speedname) {
  unsigned char pak[1024];
  unsigned char buf[1024];
  int len, res;
  unsigned char p[4];
  convlong(p, speed);
  buildpacket(pak, &len, 0x1a, p, 4);
  printf("Trying %ld baud.\n", speed);
  write(fd, pak, len);
  res = listen(fd, buf);
  if (!(res == 1 && buf[0] == 0x06)) {
    printf("Problem sending set_com_baud.\n");
    return 0;
  }
  struct termios tio;
  tcgetattr(fd, &tio);
  cfsetspeed(&tio, speedname);
  tcsetattr(fd, TCSANOW, &tio);
  if (!(checkrom(fd) == 1)) {
    printf("Bad response at new baud rate.\n");
    cfsetspeed(&tio, B9600);
    tcsetattr(fd, TCSANOW, &tio);
    return 0;
  }
  if (!(readline(0, fd, buf))) {
    printf("Problem reading line\n");
    cfsetspeed(&tio, B9600);
    tcsetattr(fd, TCSANOW, &tio);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<changebaud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x82c(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <convlong@plt>
lea    -0x420(%rbp),%rdi
lea    -0x82c(%rbp),%rcx
lea    -0x824(%rbp),%rsi
mov    $0x1a,%edx
mov    $0x4,%r8d
call   1070 <buildpacket@plt>
mov    -0x10(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x824(%rbp),%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x820(%rbp),%rsi
call   1090 <listen@plt>
mov    %eax,-0x828(%rbp)
cmpl   $0x1,-0x828(%rbp)
jne    1243 <changebaud+0xa3>
movzbl -0x820(%rbp),%eax
cmp    $0x6,%eax
je     125d <changebaud+0xbd>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1353 <changebaud+0x1b3>
mov    -0x8(%rbp),%edi
lea    -0x830(%rbp),%rsi
call   10b0 <tcgetattr@plt>
mov    -0x14(%rbp),%esi
lea    -0x830(%rbp),%rdi
call   1050 <cfsetspeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
lea    -0x830(%rbp),%rdx
call   10c0 <tcsetattr@plt>
mov    -0x8(%rbp),%edi
call   10a0 <checkrom@plt>
cmp    $0x1,%eax
je     12eb <changebaud+0x14b>
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
lea    -0x830(%rbp),%rdi
call   1050 <cfsetspeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
lea    -0x830(%rbp),%rdx
call   10c0 <tcsetattr@plt>
movl   $0x0,-0x4(%rbp)
jmp    1353 <changebaud+0x1b3>
mov    -0x8(%rbp),%esi
lea    -0x820(%rbp),%rdx
xor    %edi,%edi
call   1060 <readline@plt>
cmp    $0x0,%eax
jne    134c <changebaud+0x1ac>
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%esi
lea    -0x830(%rbp),%rdi
call   1050 <cfsetspeed@plt>
mov    -0x8(%rbp),%edi
mov    0x2cb6(%rip),%rax
mov    (%rax),%esi
lea    -0x830(%rbp),%rdx
call   10c0 <tcsetattr@plt>
movl   $0x0,-0x4(%rbp)
jmp    1353 <changebaud+0x1b3>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<changebaud>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r12d
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <convlong@plt>
lea    0x410(%rsp),%r13
lea    0x8(%rsp),%rsi
mov    %r13,%rdi
mov    $0x1a,%edx
mov    %rbx,%rcx
mov    $0x4,%r8d
call   1080 <buildpacket@plt>
lea    0xdff(%rip),%rdi
xor    %r15d,%r15d
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edx
mov    %r12d,%edi
mov    %r13,%rsi
call   1040 <write@plt>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
call   10a0 <listen@plt>
cmp    $0x1,%eax
jne    1293 <changebaud+0xe3>
cmpb   $0x6,0x10(%rsp)
jne    1293 <changebaud+0xe3>
mov    %rsp,%rbx
mov    %r12d,%edi
mov    %rbx,%rsi
call   10c0 <tcgetattr@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <cfsetspeed@plt>
mov    0x2d92(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r12d,%edi
mov    %rbx,%rdx
call   10d0 <tcsetattr@plt>
mov    %r12d,%edi
call   10b0 <checkrom@plt>
cmp    $0x1,%eax
jne    12a1 <changebaud+0xf1>
lea    0x10(%rsp),%rdx
xor    %edi,%edi
mov    %r12d,%esi
call   1070 <readline@plt>
mov    $0x1,%r15d
test   %eax,%eax
jne    12d2 <changebaud+0x122>
lea    0xdbe(%rip),%rdi
jmp    12a8 <changebaud+0xf8>
lea    0xd78(%rip),%rdi
call   1030 <puts@plt>
jmp    12d2 <changebaud+0x122>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <cfsetspeed@plt>
mov    0x0(%rbp),%esi
mov    %r12d,%edi
mov    %rbx,%rdx
call   10d0 <tcsetattr@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [18 x i8] c"Trying %ld baud.\0A\00"
@.str.1 = [31 x i8] c"Problem sending set_com_baud.\0A\00"
@TCSANOW = global i32 0
@.str.2 = [32 x i8] c"Bad response at new baud rate.\0A\00"
@B9600 = global i32 0
@.str.3 = [22 x i8] c"Problem reading line\0A\00"
define i32 @changebaud(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca [4 x i8]
%13 = alloca %struct.termios
store i32 %0, i32* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%15 = load i64, i64* %6
%16 = call i32 @convlong(i8* %14, i64 %15)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%19 = call i32 @buildpacket(i8* %17, i32* %10, i32 26, i8* %18, i32 4)
%20 = load i64, i64* %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %20)
%22 = load i32, i32* %5
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%24 = load i32, i32* %10
%25 = call i32 @write(i32 %22, i8* %23, i32 %24)
%26 = load i32, i32* %5
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%28 = call i32 @listen(i32 %26, i8* %27)
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %36
31: ; preds = %3
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%33 = load i8, i8* %32
%34 = zext i8 %33 to i32
%35 = icmp eq i32 %34, 6
br i1 %35, label %38, label %36
36: ; preds = %31, %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %69
38: ; preds = %31
%39 = load i32, i32* %5
%40 = call i32 @tcgetattr(i32 %39, %struct.termios* %13)
%41 = load i32, i32* %7
%42 = call i32 @cfsetspeed(%struct.termios* %13, i32 %41)
%43 = load i32, i32* %5
%44 = load i32, i32* @TCSANOW
%45 = call i32 @tcsetattr(i32 %43, i32 %44, %struct.termios* %13)
%46 = load i32, i32* %5
%47 = call i32 @checkrom(i32 %46)
%48 = icmp eq i32 %47, 1
br i1 %48, label %56, label %49
49: ; preds = %38
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32, i32* @B9600
%52 = call i32 @cfsetspeed(%struct.termios* %13, i32 %51)
%53 = load i32, i32* %5
%54 = load i32, i32* @TCSANOW
%55 = call i32 @tcsetattr(i32 %53, i32 %54, %struct.termios* %13)
store i32 0, i32* %4
br label %69
56: ; preds = %38
%57 = load i32, i32* %5
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%59 = call i32 @readline(i32 0, i32 %57, i8* %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %68, label %61
61: ; preds = %56
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%63 = load i32, i32* @B9600
%64 = call i32 @cfsetspeed(%struct.termios* %13, i32 %63)
%65 = load i32, i32* %5
%66 = load i32, i32* @TCSANOW
%67 = call i32 @tcsetattr(i32 %65, i32 %66, %struct.termios* %13)
store i32 0, i32* %4
br label %69
68: ; preds = %56
store i32 1, i32* %4
br label %69
69: ; preds = %68, %61, %49, %36
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [18 x i8] c"Trying %ld baud.\0A\00"
@TCSANOW = global i32 0
@B9600 = global i32 0
@str = [30 x i8] c"Problem sending set_com_baud.\00"
@str.4 = [31 x i8] c"Bad response at new baud rate.\00"
@str.5 = [21 x i8] c"Problem reading line\00"
define i32 @changebaud(i32 %0, i64 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca [4 x i8]
%8 = alloca %struct.termios
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = bitcast i32* %6 to i8*
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%13 = call i32 @convlong(i8* %12, i64 %1)
%14 = call i32 @buildpacket(i8* %9, i32* %6, i32 26, i8* %12, i32 4)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %1)
%16 = load i32, i32* %6
%17 = call i32 @write(i32 %0, i8* %9, i32 %16)
%18 = call i32 @listen(i32 %0, i8* %10)
%19 = icmp eq i32 %18, 1
%20 = load i8, i8* %10
%21 = icmp eq i8 %20, 6
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %25, label %23
23: ; preds = %3
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %45
25: ; preds = %3
%26 = bitcast %struct.termios* %8 to i8*
%27 = call i32 @tcgetattr(i32 %0, %struct.termios* %8)
%28 = call i32 @cfsetspeed(%struct.termios* %8, i32 %2)
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %0, i32 %29, %struct.termios* %8)
%31 = call i32 @checkrom(i32 %0)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = call i32 @readline(i32 0, i32 %0, i8* %10)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %33, %25
%37 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @str.4, i64 0, i64 0), %25 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0), %33 ]
%38 = call i32 @puts(i8* dereferenceable(1) %37)
%39 = load i32, i32* @B9600
%40 = call i32 @cfsetspeed(%struct.termios* %8, i32 %39)
%41 = load i32, i32* @TCSANOW
%42 = call i32 @tcsetattr(i32 %0, i32 %41, %struct.termios* %8)
br label %43
43: ; preds = %36, %33
%44 = phi i32 [ 1, %33 ], [ 0, %36 ]
br label %45
45: ; preds = %43, %23
%46 = phi i32 [ %44, %43 ], [ 0, %23 ]
ret i32 %46
}