#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 c_cflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};
typedef int /*<<< orphan*/ options;

/* Variables and functions */
int /*<<< orphan*/ B115200;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int cfsetispeed(struct termios *, int /*<<< orphan*/);
int cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_termopts(int fd) {
  struct termios options;
  int rc;
  rc = 1;
  do {
    memset(&options, 0, sizeof(options));
    tcgetattr(fd, &options);
    if (-1 == cfsetispeed(&options, B115200)) {
      fprintf(stderr, "error, could not set baud rate\n");
      break;
    }
    if (-1 == cfsetospeed(&options, B115200)) {
      fprintf(stderr, "error, could not set baud rate\n");
      break;
    }
    options.c_iflag = 0;
    options.c_oflag = 0;
    options.c_cflag = CS8 | CREAD | CLOCAL;
    options.c_lflag = 0;
    options.c_cc[VMIN] = 1;
    options.c_cc[VTIME] = 5;
    tcflush(fd, TCIFLUSH);
    if (-1 == tcsetattr(fd, TCSANOW, &options)) {
      fprintf(stderr, "error, could not tcsetattr\n");
      break;
    }
    rc = 0;
  } while (0);
  return rc;
}


x86 assembly (clang, -O0):
<set_termopts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1090 <cfsetispeed@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11d9 <set_termopts+0x69>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12ca <set_termopts+0x15a>
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1214 <set_termopts+0xa4>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12ca <set_termopts+0x15a>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    0x2dac(%rip),%rcx
or     (%rcx),%eax
mov    0x2d5b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d7b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2d3e(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x4(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12c3 <set_termopts+0x153>
mov    0x2d08(%rip),%rax
mov    (%rax),%edi
lea    0xd67(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12ca <set_termopts+0x15a>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_termopts>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <tcgetattr@plt>
mov    0x2e0f(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1090 <cfsetispeed@plt>
lea    0xe4d(%rip),%rbx
cmp    $0xffffffff,%eax
je     1260 <set_termopts+0xf0>
mov    0x0(%rbp),%esi
lea    0x8(%rsp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0xffffffff,%eax
je     1260 <set_termopts+0xf0>
mov    0x2dff(%rip),%rax
mov    0x2e00(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xorps  %xmm0,%xmm0
mov    0x2daa(%rip),%rax
or     (%rax),%ecx
movups %xmm0,0x20(%rsp)
mov    %ecx,0x8(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dc2(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d89(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <tcflush@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1080 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1276 <set_termopts+0x106>
lea    0xdc0(%rip),%rbx
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@B115200 = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"error, could not set baud rate\0A\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [28 x i8] c"error, could not tcsetattr\0A\00"
define i32 @set_termopts(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca i32
store i32 %0, i32* %2
store i32 1, i32* %4
br label %5
5: ; preds = %1
%6 = call i32 @memset(%struct.termios* %3, i32 0, i32 40)
%7 = load i32, i32* %2
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %3)
%9 = load i32, i32* @B115200
%10 = call i32 @cfsetispeed(%struct.termios* %3, i32 %9)
%11 = icmp eq i32 -1, %10
br i1 %11, label %12, label %15
12: ; preds = %5
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %51
15: ; preds = %5
%16 = load i32, i32* @B115200
%17 = call i32 @cfsetospeed(%struct.termios* %3, i32 %16)
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %51
22: ; preds = %15
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 4
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 3
store i64 0, i64* %24
%25 = load i32, i32* @CS8
%26 = load i32, i32* @CREAD
%27 = or i32 %25, %26
%28 = load i32, i32* @CLOCAL
%29 = or i32 %27, %28
%30 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 2
store i64 0, i64* %31
%32 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%33 = load i32*, i32** %32
%34 = load i64, i64* @VMIN
%35 = getelementptr inbounds i32, i32* %33, i64 %34
store i32 1, i32* %35
%36 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%37 = load i32*, i32** %36
%38 = load i64, i64* @VTIME
%39 = getelementptr inbounds i32, i32* %37, i64 %38
store i32 5, i32* %39
%40 = load i32, i32* %2
%41 = load i32, i32* @TCIFLUSH
%42 = call i32 @tcflush(i32 %40, i32 %41)
%43 = load i32, i32* %2
%44 = load i32, i32* @TCSANOW
%45 = call i32 @tcsetattr(i32 %43, i32 %44, %struct.termios* %3)
%46 = icmp eq i32 -1, %45
br i1 %46, label %47, label %50
47: ; preds = %22
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %51
50: ; preds = %22
store i32 0, i32* %4
br label %51
51: ; preds = %50, %47, %19, %12
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i64, i64 }
@B115200 = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"error, could not set baud rate\0A\00"
@CS8 = global i32 0
@CREAD = global i32 0
@CLOCAL = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [28 x i8] c"error, could not tcsetattr\0A\00"
define i32 @set_termopts(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @memset(%struct.termios* %2, i32 0, i32 40)
%5 = call i32 @tcgetattr(i32 %0, %struct.termios* %2)
%6 = load i32, i32* @B115200
%7 = call i32 @cfsetispeed(%struct.termios* %2, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %34, label %9
9: ; preds = %1
%10 = load i32, i32* @B115200
%11 = call i32 @cfsetospeed(%struct.termios* %2, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %34, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 3
%15 = bitcast i64* %14 to i8*
%16 = load i32, i32* @CS8
%17 = load i32, i32* @CREAD
%18 = or i32 %17, %16
%19 = load i32, i32* @CLOCAL
%20 = or i32 %18, %19
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 2
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%24 = load i32*, i32** %23
%25 = load i64, i64* @VMIN
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 1, i32* %26
%27 = load i64, i64* @VTIME
%28 = getelementptr inbounds i32, i32* %24, i64 %27
store i32 5, i32* %28
%29 = load i32, i32* @TCIFLUSH
%30 = call i32 @tcflush(i32 %0, i32 %29)
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 %0, i32 %31, %struct.termios* %2)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %13, %9, %1
%35 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %9 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %13 ]
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* %35)
br label %38
38: ; preds = %34, %13
%39 = phi i32 [ 0, %13 ], [ 1, %34 ]
ret i32 %39
}