#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 */

/* Variables and functions */
int O_NOCTTY;
int O_WRONLY;
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int getopt(int, char **, char *);
scalar_t__ ioctl(int, int, int);
int open(char *, int);
int /*<<< orphan*/ optarg;
int optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_usage(char *);
int main(int argc, char *argv[]) {
  int opt;
  int bitrate = -1;
  while ((opt = getopt(argc, argv, "s:")) != -1) {
    switch (opt) {
    case 's':
      bitrate = atoi(optarg);
      break;
    }
  }
  if (argc - optind != 1) {
    print_usage(argv[0]);
    return -1;
  }
  int fd;
  if ((fd = open(argv[optind], O_WRONLY | O_NOCTTY)) < 0) {
    perror(argv[optind]);
    return -1;
  }
  if (bitrate >= 0 && bitrate <= 8) {
    if (ioctl(fd, 22, bitrate) < 0) {
      perror("ioctl SLCAN_SET_BAUDRATE");
      goto err;
    }
  }
  close(fd);
  return 0;
err:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     11d3 <main+0x63>
mov    -0x14(%rbp),%eax
sub    $0x73,%eax
jne    11ce <main+0x5e>
jmp    11bd <main+0x4d>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    118d <main+0x1d>
mov    -0x8(%rbp),%eax
mov    0x2e03(%rip),%rcx
sub    (%rcx),%eax
cmp    $0x1,%eax
je     1200 <main+0x90>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <print_usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <main+0x14f>
mov    -0x10(%rbp),%rax
mov    0x2dd5(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1258 <main+0xe8>
mov    -0x10(%rbp),%rax
mov    0x2da0(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bf <main+0x14f>
cmpl   $0x0,-0x18(%rbp)
jl     129c <main+0x12c>
cmpl   $0x8,-0x18(%rbp)
jg     129c <main+0x12c>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%edx
mov    $0x16,%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    1297 <main+0x127>
lea    0xd76(%rip),%rdi
call   1060 <perror@plt>
jmp    12b0 <main+0x140>
jmp    129c <main+0x12c>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12bf <main+0x14f>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0xffffffff,%r14d
lea    0xe76(%rip),%r15
mov    0x2e67(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <getopt@plt>
cmp    $0xffffffff,%eax
je     11c5 <main+0x55>
cmp    $0x73,%eax
jne    11a0 <main+0x30>
mov    (%r12),%edi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    11a0 <main+0x30>
mov    0x2e14(%rip),%r15
movslq (%r15),%rax
sub    %eax,%ebp
cmp    $0x1,%ebp
jne    121c <main+0xac>
mov    (%rbx,%rax,8),%rdi
mov    0x2ddf(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1232 <main+0xc2>
mov    %eax,%ebp
cmp    $0x8,%r14d
ja     1211 <main+0xa1>
mov    %ebp,%edi
mov    $0x16,%esi
mov    %r14d,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
js     1240 <main+0xd0>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1229 <main+0xb9>
mov    (%rbx),%rdi
call   1090 <print_usage@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movslq (%r15),%rax
mov    (%rbx,%rax,8),%rdi
call   1060 <perror@plt>
jmp    1224 <main+0xb4>
lea    0xdbc(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1224 <main+0xb4>

LLVM IR (-O0):
@.str = [3 x i8] c"s:\00"
@optarg = global i32 0
@optind = global i32 0
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [25 x i8] c"ioctl SLCAN_SET_BAUDRATE\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %7
br label %9
9: ; preds = %19, %2
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = call i32 @getopt(i32 %10, i8** %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %12, i32* %6
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* %6
switch i32 %15, label %19 [
i32 115, label %16
]
16: ; preds = %14
%17 = load i32, i32* @optarg
%18 = call i32 @atoi(i32 %17)
store i32 %18, i32* %7
br label %19
19: ; preds = %14, %16
br label %9
20: ; preds = %9
%21 = load i32, i32* %4
%22 = load i32, i32* @optind
%23 = sub nsw i32 %21, %22
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32 @print_usage(i8* %28)
store i32 -1, i32* %3
br label %68
30: ; preds = %20
%31 = load i8**, i8*** %5
%32 = load i32, i32* @optind
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_NOCTTY
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %35, i32 %38)
store i32 %39, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %30
%42 = load i8**, i8*** %5
%43 = load i32, i32* @optind
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8*, i8** %42, i64 %44
%46 = load i8*, i8** %45
%47 = call i32 @perror(i8* %46)
store i32 -1, i32* %3
br label %68
48: ; preds = %30
%49 = load i32, i32* %7
%50 = icmp sge i32 %49, 0
br i1 %50, label %51, label %62
51: ; preds = %48
%52 = load i32, i32* %7
%53 = icmp sle i32 %52, 8
br i1 %53, label %54, label %62
54: ; preds = %51
%55 = load i32, i32* %8
%56 = load i32, i32* %7
%57 = call i64 @ioctl(i32 %55, i32 22, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %65
61: ; preds = %54
br label %62
62: ; preds = %61, %51, %48
%63 = load i32, i32* %8
%64 = call i32 @close(i32 %63)
store i32 0, i32* %3
br label %68
65: ; preds = %59
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %3
br label %68
68: ; preds = %65, %62, %41, %25
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [3 x i8] c"s:\00"
@optarg = global i32 0
@optind = global i32 0
@O_WRONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [25 x i8] c"ioctl SLCAN_SET_BAUDRATE\00"
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %7, %2
%4 = phi i32 [ %9, %7 ], [ -1, %2 ]
br label %5
5: ; preds = %3, %5
%6 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %6, label %5 [
i32 -1, label %10
i32 115, label %7
]
7: ; preds = %5
%8 = load i32, i32* @optarg
%9 = tail call i32 @atoi(i32 %8)
br label %3
10: ; preds = %5
%11 = load i32, i32* @optind
%12 = sub nsw i32 %0, %11
%13 = icmp eq i32 %12, 1
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = load i8*, i8** %1
%16 = tail call i32 @print_usage(i8* %15)
br label %42
17: ; preds = %10
%18 = sext i32 %11 to i64
%19 = getelementptr inbounds i8*, i8** %1, i64 %18
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_NOCTTY
%23 = or i32 %22, %21
%24 = tail call i32 @open(i8* %20, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %17
%27 = load i32, i32* @optind
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %1, i64 %28
%30 = load i8*, i8** %29
%31 = tail call i32 @perror(i8* %30)
br label %42
32: ; preds = %17
%33 = icmp ult i32 %4, 9
br i1 %33, label %34, label %40
34: ; preds = %32
%35 = tail call i64 @ioctl(i32 %24, i32 22, i32 %4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%39 = tail call i32 @close(i32 %24)
br label %42
40: ; preds = %34, %32
%41 = tail call i32 @close(i32 %24)
br label %42
42: ; preds = %26, %40, %37, %14
%43 = phi i32 [ -1, %14 ], [ -1, %26 ], [ -1, %37 ], [ 0, %40 ]
ret i32 %43
}