#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*/ buffer;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(unsigned char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char,
                           unsigned char, unsigned char, unsigned char,
                           unsigned char, unsigned char, unsigned char);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ system(char *);
int change_baud(int baud) {
  int result = -1;
  unsigned char BH = 0x2a;
  unsigned char BL = 0xf2;
  switch (baud) {
  case 9600:
    BH = 0xae;
    BL = 0xc8;
    break;
  case 19200:
    BH = 0x56;
    BL = 0xe4;
    break;
  case 38400:
    BH = 0x2a;
    BL = 0xf2;
    break;
  case 57600:
    BH = 0x1c;
    BL = 0x4c;
    break;
    ;
  case 115200:
    BH = 0x0d;
    BL = 0xa6;
    break;
  default:
    return result;
    break;
  }
  result = -2;
  unsigned char buf[6];
  unsigned char baudarr[7] = {0x56, 0x00, 0x24, 0x03, 0x01, BH, BL};
  FILE *camera = fopen("/dev/ttyMFD1", "a+");
  fprintf(camera, "%c%c%c%c%c%c%c", baudarr[0], baudarr[1], baudarr[2],
          baudarr[3], baudarr[4], baudarr[5], baudarr[6]);
  fgets(buf, 5, camera);
  if (buf[0] == 0x76 && buf[2] == 0x24)
    result = 0;
  char buffer[32] = {0x00};
  snprintf(buffer, sizeof(buffer), "stty -F /dev/ttyMFD1 %d", baud);
  system(buffer);
  fclose(camera);
  return result;
}


x86 assembly (clang, -O0):
<change_baud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movb   $0x2a,-0xd(%rbp)
movb   $0xf2,-0xe(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
sub    $0x2580,%eax
je     11ec <change_baud+0x7c>
jmp    11a0 <change_baud+0x30>
mov    -0x54(%rbp),%eax
sub    $0x4b00,%eax
je     11f9 <change_baud+0x89>
jmp    11b3 <change_baud+0x43>
mov    -0x54(%rbp),%eax
sub    $0x9600,%eax
je     1206 <change_baud+0x96>
jmp    11c6 <change_baud+0x56>
mov    -0x54(%rbp),%eax
sub    $0xe100,%eax
je     1213 <change_baud+0xa3>
jmp    11d9 <change_baud+0x69>
mov    -0x54(%rbp),%eax
sub    $0x1c200,%eax
je     1220 <change_baud+0xb0>
jmp    122d <change_baud+0xbd>
movb   $0xae,-0xd(%rbp)
movb   $0xc8,-0xe(%rbp)
jmp    1238 <change_baud+0xc8>
movb   $0x56,-0xd(%rbp)
movb   $0xe4,-0xe(%rbp)
jmp    1238 <change_baud+0xc8>
movb   $0x2a,-0xd(%rbp)
movb   $0xf2,-0xe(%rbp)
jmp    1238 <change_baud+0xc8>
movb   $0x1c,-0xd(%rbp)
movb   $0x4c,-0xe(%rbp)
jmp    1238 <change_baud+0xc8>
movb   $0xd,-0xd(%rbp)
movb   $0xa6,-0xe(%rbp)
jmp    1238 <change_baud+0xc8>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1333 <change_baud+0x1c3>
movl   $0xfffffffe,-0xc(%rbp)
movb   $0x56,-0x1b(%rbp)
movb   $0x0,-0x1a(%rbp)
movb   $0x24,-0x19(%rbp)
movb   $0x3,-0x18(%rbp)
movb   $0x1,-0x17(%rbp)
mov    -0xd(%rbp),%al
mov    %al,-0x16(%rbp)
mov    -0xe(%rbp),%al
mov    %al,-0x15(%rbp)
lea    0xd9a(%rip),%rdi
lea    0xda0(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1b(%rbp),%dl
mov    -0x1a(%rbp),%cl
mov    -0x19(%rbp),%r8b
mov    -0x18(%rbp),%r9b
mov    -0x17(%rbp),%r10b
mov    -0x16(%rbp),%al
lea    0xd7a(%rip),%rsi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
movzbl %r9b,%r9d
movzbl %r10b,%r10d
mov    %r10d,(%rsp)
movzbl %al,%eax
mov    %eax,0x8(%rsp)
movzbl -0x15(%rbp),%eax
mov    %eax,0x10(%rsp)
call   1080 <fprintf@plt>
lea    -0x14(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x5,%esi
call   1070 <fgets@plt>
movzbl -0x14(%rbp),%eax
cmp    $0x76,%eax
jne    12f3 <change_baud+0x183>
movzbl -0x12(%rbp),%eax
cmp    $0x24,%eax
jne    12f3 <change_baud+0x183>
movl   $0x0,-0xc(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xd09(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rdi
call   1040 <system@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_baud>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edi,%r14d
mov    $0xffffffff,%ebp
cmp    $0x95ff,%edi
jle    11a1 <change_baud+0x41>
cmp    $0x9600,%r14d
je     11bf <change_baud+0x5f>
cmp    $0xe100,%r14d
je     11c7 <change_baud+0x67>
cmp    $0x1c200,%r14d
jne    1286 <change_baud+0x126>
mov    $0xd,%bpl
mov    $0xa6,%r15b
jmp    11d5 <change_baud+0x75>
cmp    $0x2580,%r14d
je     11cf <change_baud+0x6f>
cmp    $0x4b00,%r14d
jne    1286 <change_baud+0x126>
mov    $0x56,%bpl
mov    $0xe4,%r15b
jmp    11d5 <change_baud+0x75>
mov    $0x2a,%bpl
mov    $0xf2,%r15b
jmp    11d5 <change_baud+0x75>
mov    $0x1c,%bpl
mov    $0x4c,%r15b
jmp    11d5 <change_baud+0x75>
mov    $0xae,%bpl
mov    $0xc8,%r15b
lea    0xe24(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
sub    $0x8,%rsp
movzbl %r15b,%eax
movzbl %bpl,%ebp
lea    0xe12(%rip),%rsi
mov    %rbx,%rdi
mov    $0x56,%edx
xor    %ecx,%ecx
mov    $0x24,%r8d
mov    $0x3,%r9d
push   %rax
push   %rbp
push   $0x1
call   1070 <fprintf@plt>
add    $0x20,%rsp
lea    0xa(%rsp),%rdi
mov    $0x5,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    0xa(%rsp),%al
mov    0xc(%rsp),%cl
xor    $0x76,%al
xor    $0x24,%cl
xor    %edx,%edx
or     %al,%cl
sete   %dl
lea    (%rdx,%rdx,1),%ebp
add    $0xfffffffe,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xdbe(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x20,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    %r15,%rdi
call   1040 <system@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyMFD1\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [15 x i8] c"%c%c%c%c%c%c%c\00"
@.str.3 = [24 x i8] c"stty -F /dev/ttyMFD1 %d\00"
define i32 @change_baud(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
%7 = alloca [6 x i8]
%8 = alloca [7 x i8]
%9 = alloca i32*
%10 = alloca [32 x i8]
store i32 %0, i32* %3
store i32 -1, i32* %4
store i8 42, i8* %5
store i8 -14, i8* %6
%11 = load i32, i32* %3
switch i32 %11, label %17 [
i32 9600, label %12
i32 19200, label %13
i32 38400, label %14
i32 57600, label %15
i32 115200, label %16
]
12: ; preds = %1
store i8 -82, i8* %5
store i8 -56, i8* %6
br label %19
13: ; preds = %1
store i8 86, i8* %5
store i8 -28, i8* %6
br label %19
14: ; preds = %1
store i8 42, i8* %5
store i8 -14, i8* %6
br label %19
15: ; preds = %1
store i8 28, i8* %5
store i8 76, i8* %6
br label %19
16: ; preds = %1
store i8 13, i8* %5
store i8 -90, i8* %6
br label %19
17: ; preds = %1
%18 = load i32, i32* %4
store i32 %18, i32* %2
br label %69
19: ; preds = %16, %15, %14, %13, %12
store i32 -2, i32* %4
%20 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
store i8 86, i8* %20
%21 = getelementptr inbounds i8, i8* %20, i64 1
store i8 0, i8* %21
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8 36, i8* %22
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8 3, i8* %23
%24 = getelementptr inbounds i8, i8* %23, i64 1
store i8 1, i8* %24
%25 = getelementptr inbounds i8, i8* %24, i64 1
%26 = load i8, i8* %5
store i8 %26, i8* %25
%27 = getelementptr inbounds i8, i8* %25, i64 1
%28 = load i8, i8* %6
store i8 %28, i8* %27
%29 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %9
%30 = load i32*, i32** %9
%31 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%32 = load i8, i8* %31
%33 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 1
%34 = load i8, i8* %33
%35 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 2
%36 = load i8, i8* %35
%37 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 3
%38 = load i8, i8* %37
%39 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 4
%40 = load i8, i8* %39
%41 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 5
%42 = load i8, i8* %41
%43 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 6
%44 = load i8, i8* %43
%45 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %32, i8 zeroext %34, i8 zeroext %36, i8 zeroext %38, i8 zeroext %40, i8 zeroext %42, i8 zeroext %44)
%46 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%47 = load i32*, i32** %9
%48 = call i32 @fgets(i8* %46, i32 5, i32* %47)
%49 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = icmp eq i32 %51, 118
br i1 %52, label %53, label %59
53: ; preds = %19
%54 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 2
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = icmp eq i32 %56, 36
br i1 %57, label %58, label %59
58: ; preds = %53
store i32 0, i32* %4
br label %59
59: ; preds = %58, %53, %19
%60 = bitcast [32 x i8]* %10 to i8*
%61 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%62 = load i32, i32* %3
%63 = call i32 @snprintf(i8* %61, i32 32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %62)
%64 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%65 = call i32 @system(i8* %64)
%66 = load i32*, i32** %9
%67 = call i32 @fclose(i32* %66)
%68 = load i32, i32* %4
store i32 %68, i32* %2
br label %69
69: ; preds = %59, %17
%70 = load i32, i32* %2
ret i32 %70
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyMFD1\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [15 x i8] c"%c%c%c%c%c%c%c\00"
@.str.3 = [24 x i8] c"stty -F /dev/ttyMFD1 %d\00"
define i32 @change_baud(i32 %0) {
%2 = alloca [6 x i8]
%3 = alloca [32 x i8]
switch i32 %0, label %26 [
i32 9600, label %8
i32 19200, label %4
i32 38400, label %5
i32 57600, label %6
i32 115200, label %7
]
4: ; preds = %1
br label %8
5: ; preds = %1
br label %8
6: ; preds = %1
br label %8
7: ; preds = %1
br label %8
8: ; preds = %1, %7, %6, %5, %4
%9 = phi i8 [ -90, %7 ], [ 76, %6 ], [ -14, %5 ], [ -28, %4 ], [ -56, %1 ]
%10 = phi i8 [ 13, %7 ], [ 28, %6 ], [ 42, %5 ], [ 86, %4 ], [ -82, %1 ]
%11 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 0
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8 zeroext 86, i8 zeroext 0, i8 zeroext 36, i8 zeroext 3, i8 zeroext 1, i8 zeroext %10, i8 zeroext %9)
%14 = call i32 @fgets(i8* %11, i32 5, i32* %12)
%15 = load i8, i8* %11
%16 = icmp eq i8 %15, 118
%17 = getelementptr inbounds [6 x i8], [6 x i8]* %2, i64 0, i64 2
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 36
%20 = select i1 %16, i1 %19, i1 false
%21 = select i1 %20, i32 0, i32 -2
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%23 = call i32 @snprintf(i8* %22, i32 32, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%24 = call i32 @system(i8* %22)
%25 = call i32 @fclose(i32* %12)
br label %26
26: ; preds = %1, %8
%27 = phi i32 [ %21, %8 ], [ -1, %1 ]
ret i32 %27
}