#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 termios2 {
  int c_ospeed;
  int c_ispeed;
  int /*<<< orphan*/ c_cflag;
};

/* Variables and functions */
int /*<<< orphan*/ BOTHER;
int /*<<< orphan*/ CBAUD;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCGETS2;
int /*<<< orphan*/ TCSETS2;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct termios2 *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int set_baud(char *devfile, int baud) {
  struct termios2 t;
  int fd;
  fd = open(devfile, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    fprintf(stderr, "error opening %s: %s", devfile, strerror(errno));
    return 2;
  }
  if (ioctl(fd, TCGETS2, &t)) {
    perror("TCGETS2");
    return 3;
  }
  printf("SetSerial: Reported speed before update %d\n", t.c_ospeed);
  t.c_cflag &= ~CBAUD;
  t.c_cflag |= BOTHER;
  t.c_ispeed = baud;
  t.c_ospeed = baud;
  if (ioctl(fd, TCSETS2, &t)) {
    perror("TCSETS2");
    return 4;
  }
  if (ioctl(fd, TCGETS2, &t)) {
    perror("TCGETS2");
    return 5;
  }
  printf("SetSerial: Reported speed after update %d\n", t.c_ospeed);
  return 0;
}


x86 assembly (clang, -O0):
<set_baud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11e4 <set_baud+0x84>
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe2d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12db <set_baud+0x17b>
mov    -0x24(%rbp),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     121b <set_baud+0xbb>
lea    0xe0b(%rip),%rdi
call   1070 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12db <set_baud+0x17b>
mov    -0x20(%rbp),%esi
lea    0xdf8(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     128e <set_baud+0x12e>
lea    0xdcc(%rip),%rdi
call   1070 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    12db <set_baud+0x17b>
mov    -0x24(%rbp),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     12c5 <set_baud+0x165>
lea    0xd61(%rip),%rdi
call   1070 <perror@plt>
movl   $0x5,-0x4(%rbp)
jmp    12db <set_baud+0x17b>
mov    -0x20(%rbp),%esi
lea    0xd82(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_baud>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e79(%rip),%rcx
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e29(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1229 <set_baud+0xc9>
mov    %eax,%ebx
mov    0x2e36(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1262 <set_baud+0x102>
mov    (%rsp),%esi
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2dcb(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,0x4(%rsp)
mov    %r14d,(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1275 <set_baud+0x115>
mov    0x0(%rbp),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jne    1288 <set_baud+0x128>
mov    (%rsp),%esi
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
jmp    1259 <set_baud+0xf9>
mov    0x2d90(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdb9(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x2,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdac(%rip),%rdi
call   1070 <perror@plt>
mov    $0x3,%eax
jmp    1259 <set_baud+0xf9>
lea    0xdcd(%rip),%rdi
call   1070 <perror@plt>
mov    $0x4,%eax
jmp    1259 <set_baud+0xf9>
lea    0xd86(%rip),%rdi
call   1070 <perror@plt>
mov    $0x5,%eax
jmp    1259 <set_baud+0xf9>

LLVM IR (-O0):
%struct.termios2 = type { i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"error opening %s: %s\00"
@errno = global i32 0
@TCGETS2 = global i32 0
@.str.1 = [8 x i8] c"TCGETS2\00"
@.str.2 = [44 x i8] c"SetSerial: Reported speed before update %d\0A\00"
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
@.str.3 = [8 x i8] c"TCSETS2\00"
@.str.4 = [43 x i8] c"SetSerial: Reported speed after update %d\0A\00"
define i32 @set_baud(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios2
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOCTTY
%11 = or i32 %9, %10
%12 = load i32, i32* @O_NDELAY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %4
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %21)
store i32 2, i32* %3
br label %64
23: ; preds = %2
%24 = load i32, i32* %7
%25 = load i32, i32* @TCGETS2
%26 = call i64 @ioctl(i32 %24, i32 %25, %struct.termios2* %6)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %3
br label %64
30: ; preds = %23
%31 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* @CBAUD
%35 = xor i32 %34, -1
%36 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 2
%37 = load i32, i32* %36
%38 = and i32 %37, %35
store i32 %38, i32* %36
%39 = load i32, i32* @BOTHER
%40 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 2
%41 = load i32, i32* %40
%42 = or i32 %41, %39
store i32 %42, i32* %40
%43 = load i32, i32* %5
%44 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 1
store i32 %43, i32* %44
%45 = load i32, i32* %5
%46 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 0
store i32 %45, i32* %46
%47 = load i32, i32* %7
%48 = load i32, i32* @TCSETS2
%49 = call i64 @ioctl(i32 %47, i32 %48, %struct.termios2* %6)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %30
%52 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 4, i32* %3
br label %64
53: ; preds = %30
%54 = load i32, i32* %7
%55 = load i32, i32* @TCGETS2
%56 = call i64 @ioctl(i32 %54, i32 %55, %struct.termios2* %6)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 5, i32* %3
br label %64
60: ; preds = %53
%61 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %62)
store i32 0, i32* %3
br label %64
64: ; preds = %60, %58, %51, %28, %17
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.termios2 = type { i32, i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"error opening %s: %s\00"
@errno = global i32 0
@TCGETS2 = global i32 0
@.str.1 = [8 x i8] c"TCGETS2\00"
@.str.2 = [44 x i8] c"SetSerial: Reported speed before update %d\0A\00"
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
@.str.3 = [8 x i8] c"TCSETS2\00"
@.str.4 = [43 x i8] c"SetSerial: Reported speed after update %d\0A\00"
define i32 @set_baud(i8* %0, i32 %1) {
%3 = alloca %struct.termios2
%4 = bitcast %struct.termios2* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_NDELAY
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
br label %49
17: ; preds = %2
%18 = load i32, i32* @TCGETS2
%19 = call i64 @ioctl(i32 %10, i32 %18, %struct.termios2* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %49
23: ; preds = %17
%24 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @CBAUD
%28 = xor i32 %27, -1
%29 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 2
%30 = load i32, i32* %29
%31 = and i32 %30, %28
%32 = load i32, i32* @BOTHER
%33 = or i32 %32, %31
store i32 %33, i32* %29
%34 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 1
store i32 %1, i32* %34
store i32 %1, i32* %24
%35 = load i32, i32* @TCSETS2
%36 = call i64 @ioctl(i32 %10, i32 %35, %struct.termios2* %3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %23
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %49
40: ; preds = %23
%41 = load i32, i32* @TCGETS2
%42 = call i64 @ioctl(i32 %10, i32 %41, %struct.termios2* %3)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %49
46: ; preds = %40
%47 = load i32, i32* %24
%48 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %46, %44, %38, %21, %12
%50 = phi i32 [ 2, %12 ], [ 3, %21 ], [ 4, %38 ], [ 5, %44 ], [ 0, %46 ]
ret i32 %50
}