#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_oflag;
  int c_lflag;
  int c_iflag;
};

/* Variables and functions */
int BRKINT;
int CLOCAL;
int CRDLY;
int CREAD;
int CS8;
int ECHO;
int ECHONL;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int ICRNL;
int IGNBRK;
int IGNPAR;
int INLCR;
int INPCK;
int ISIG;
int IUCLC;
int IXOFF;
int IXON;
int NLDLY;
int OCRNL;
int OFDEL;
int OFILL;
int OLCUC;
int ONLRET;
int ONOCR;
int /*<<< orphan*/ O_RDWR;
int PARMRK;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int set_term(int fd, int baud, struct termios tp) {
  fcntl(fd, F_GETFL);
  fcntl(fd, F_SETFL, O_RDWR);
  tp.c_cflag = baud | CS8 | CLOCAL | CREAD;
  tp.c_oflag = 0;
  tp.c_lflag = 0;
  tp.c_oflag &=
      ~(OLCUC | OCRNL | ONOCR | ONLRET | OFILL | OFDEL | NLDLY | CRDLY);
  tp.c_iflag = ~(IGNBRK | PARMRK | INPCK | INLCR | IUCLC | IXOFF) | BRKINT |
               IGNPAR | ICRNL | IXON | ISIG | ICANON;
  tp.c_lflag &= ~(ECHO | ECHONL);
  tcflush(fd, TCIFLUSH);
  cfsetospeed(&tp, baud);
  cfsetispeed(&tp, baud);
  if (tcsetattr(fd, TCSANOW, &tp) < 0) {
    perror("Couldn't set term attributes");
    return (-1);
  }
  return 1;
}


x86 assembly (clang, -O0):
<set_term>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x10(%rbp)
mov    %edi,-0x1c(%rbp)
mov    %esi,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%eax
mov    0x2db9(%rip),%rcx
or     (%rcx),%eax
mov    0x2d38(%rip),%rcx
or     (%rcx),%eax
mov    0x2daf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    0x2e0c(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcb(%rip),%rcx
or     (%rcx),%eax
mov    0x2d1a(%rip),%rcx
or     (%rcx),%eax
mov    0x2d41(%rip),%rcx
or     (%rcx),%eax
mov    0x2cf8(%rip),%rcx
or     (%rcx),%eax
mov    0x2d9f(%rip),%rcx
or     (%rcx),%eax
mov    0x2d86(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2cfc(%rip),%rax
mov    (%rax),%eax
mov    0x2db3(%rip),%rcx
or     (%rcx),%eax
mov    0x2d12(%rip),%rcx
or     (%rcx),%eax
mov    0x2d81(%rip),%rcx
or     (%rcx),%eax
mov    0x2cc8(%rip),%rcx
or     (%rcx),%eax
mov    0x2d67(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
mov    0x2d1b(%rip),%rcx
or     (%rcx),%eax
mov    0x2cb2(%rip),%rcx
or     (%rcx),%eax
mov    0x2d59(%rip),%rcx
or     (%rcx),%eax
mov    0x2d28(%rip),%rcx
or     (%rcx),%eax
mov    0x2cff(%rip),%rcx
or     (%rcx),%eax
mov    0x2c76(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0xc(%rbp)
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    0x2c89(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2c9c(%rip),%rax
mov    (%rax),%esi
call   1050 <tcflush@plt>
mov    -0x20(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1040 <cfsetospeed@plt>
mov    -0x20(%rbp),%esi
lea    -0x18(%rbp),%rdi
call   1070 <cfsetispeed@plt>
mov    -0x1c(%rbp),%edi
mov    0x2cb3(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1312 <set_term+0x1b2>
lea    0xcff(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1319 <set_term+0x1b9>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_term>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
or     %r14d,%eax
mov    0x2d95(%rip),%rcx
or     (%rcx),%eax
mov    0x2dec(%rip),%rcx
or     (%rcx),%eax
mov    0x2dab(%rip),%rdx
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rdx),%ecx
mov    0x2db1(%rip),%rdx
or     (%rdx),%ecx
mov    0x2df8(%rip),%rdx
or     (%rdx),%ecx
mov    0x2d77(%rip),%rdx
or     (%rdx),%ecx
mov    0x2dde(%rip),%rdx
or     (%rdx),%ecx
not    %ecx
mov    0x2db3(%rip),%rdx
or     (%rdx),%ecx
mov    0x2d62(%rip),%rdx
or     (%rdx),%ecx
mov    0x2dd1(%rip),%rdx
or     (%rdx),%ecx
mov    0x2db0(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,(%rsp)
mov    0x2d94(%rip),%rax
or     (%rax),%ecx
movl   $0x0,0x4(%rsp)
mov    0x2d23(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <tcflush@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <cfsetospeed@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1070 <cfsetispeed@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <tcsetattr@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
js     128d <set_term+0x12d>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd6c(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1284 <set_term+0x124>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_RDWR = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@OLCUC = global i32 0
@OCRNL = global i32 0
@ONOCR = global i32 0
@ONLRET = global i32 0
@OFILL = global i32 0
@OFDEL = global i32 0
@NLDLY = global i32 0
@CRDLY = global i32 0
@IGNBRK = global i32 0
@PARMRK = global i32 0
@INPCK = global i32 0
@INLCR = global i32 0
@IUCLC = global i32 0
@IXOFF = global i32 0
@BRKINT = global i32 0
@IGNPAR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str = [29 x i8] c"Couldn't set term attributes\00"
define i32 @set_term(i32 %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
%8 = alloca i32
%9 = bitcast %struct.termios* %6 to { i64, i64 }*
%10 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %9, i32 0, i32 0
store i64 %2, i64* %10
%11 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %9, i32 0, i32 1
store i64 %3, i64* %11
store i32 %0, i32* %7
store i32 %1, i32* %8
%12 = load i32, i32* %7
%13 = load i32, i32* @F_GETFL
%14 = call i32 (i32, i32, ...) @fcntl(i32 %12, i32 %13)
%15 = load i32, i32* %7
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* @O_RDWR
%18 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16, i32 %17)
%19 = load i32, i32* %8
%20 = load i32, i32* @CS8
%21 = or i32 %19, %20
%22 = load i32, i32* @CLOCAL
%23 = or i32 %21, %22
%24 = load i32, i32* @CREAD
%25 = or i32 %23, %24
%26 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
store i32 0, i32* %27
%28 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i32 0, i32* %28
%29 = load i32, i32* @OLCUC
%30 = load i32, i32* @OCRNL
%31 = or i32 %29, %30
%32 = load i32, i32* @ONOCR
%33 = or i32 %31, %32
%34 = load i32, i32* @ONLRET
%35 = or i32 %33, %34
%36 = load i32, i32* @OFILL
%37 = or i32 %35, %36
%38 = load i32, i32* @OFDEL
%39 = or i32 %37, %38
%40 = load i32, i32* @NLDLY
%41 = or i32 %39, %40
%42 = load i32, i32* @CRDLY
%43 = or i32 %41, %42
%44 = xor i32 %43, -1
%45 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%46 = load i32, i32* %45
%47 = and i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* @IGNBRK
%49 = load i32, i32* @PARMRK
%50 = or i32 %48, %49
%51 = load i32, i32* @INPCK
%52 = or i32 %50, %51
%53 = load i32, i32* @INLCR
%54 = or i32 %52, %53
%55 = load i32, i32* @IUCLC
%56 = or i32 %54, %55
%57 = load i32, i32* @IXOFF
%58 = or i32 %56, %57
%59 = xor i32 %58, -1
%60 = load i32, i32* @BRKINT
%61 = or i32 %59, %60
%62 = load i32, i32* @IGNPAR
%63 = or i32 %61, %62
%64 = load i32, i32* @ICRNL
%65 = or i32 %63, %64
%66 = load i32, i32* @IXON
%67 = or i32 %65, %66
%68 = load i32, i32* @ISIG
%69 = or i32 %67, %68
%70 = load i32, i32* @ICANON
%71 = or i32 %69, %70
%72 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 3
store i32 %71, i32* %72
%73 = load i32, i32* @ECHO
%74 = load i32, i32* @ECHONL
%75 = or i32 %73, %74
%76 = xor i32 %75, -1
%77 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%78 = load i32, i32* %77
%79 = and i32 %78, %76
store i32 %79, i32* %77
%80 = load i32, i32* %7
%81 = load i32, i32* @TCIFLUSH
%82 = call i32 @tcflush(i32 %80, i32 %81)
%83 = load i32, i32* %8
%84 = call i32 @cfsetospeed(%struct.termios* %6, i32 %83)
%85 = load i32, i32* %8
%86 = call i32 @cfsetispeed(%struct.termios* %6, i32 %85)
%87 = load i32, i32* %7
%88 = load i32, i32* @TCSANOW
%89 = call i64 @tcsetattr(i32 %87, i32 %88, %struct.termios* %6)
%90 = icmp slt i64 %89, 0
br i1 %90, label %91, label %93
91: ; preds = %4
%92 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %94
93: ; preds = %4
store i32 1, i32* %5
br label %94
94: ; preds = %93, %91
%95 = load i32, i32* %5
ret i32 %95
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_RDWR = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@OLCUC = global i32 0
@OCRNL = global i32 0
@ONOCR = global i32 0
@ONLRET = global i32 0
@OFILL = global i32 0
@OFDEL = global i32 0
@NLDLY = global i32 0
@CRDLY = global i32 0
@IGNBRK = global i32 0
@PARMRK = global i32 0
@INPCK = global i32 0
@INLCR = global i32 0
@IUCLC = global i32 0
@IXOFF = global i32 0
@BRKINT = global i32 0
@IGNPAR = global i32 0
@ICRNL = global i32 0
@IXON = global i32 0
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ECHONL = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str = [29 x i8] c"Couldn't set term attributes\00"
define i32 @set_term(i32 %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca { i64, i64 }
%6 = bitcast { i64, i64 }* %5 to %struct.termios*
%7 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %5, i64 0, i32 1
%8 = load i32, i32* @F_GETFL
%9 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %8)
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %10, i32 %11)
%13 = load i32, i32* @CS8
%14 = or i32 %13, %1
%15 = load i32, i32* @CLOCAL
%16 = or i32 %14, %15
%17 = load i32, i32* @CREAD
%18 = or i32 %16, %17
%19 = bitcast { i64, i64 }* %5 to i32*
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 1
%21 = bitcast i64* %7 to i32*
store i32 0, i32* %20
%22 = load i32, i32* @IGNBRK
%23 = load i32, i32* @PARMRK
%24 = or i32 %23, %22
%25 = load i32, i32* @INPCK
%26 = or i32 %24, %25
%27 = load i32, i32* @INLCR
%28 = or i32 %26, %27
%29 = load i32, i32* @IUCLC
%30 = or i32 %28, %29
%31 = load i32, i32* @IXOFF
%32 = or i32 %30, %31
%33 = xor i32 %32, -1
%34 = load i32, i32* @BRKINT
%35 = or i32 %34, %33
%36 = load i32, i32* @IGNPAR
%37 = or i32 %35, %36
%38 = load i32, i32* @ICRNL
%39 = or i32 %37, %38
%40 = load i32, i32* @IXON
%41 = or i32 %39, %40
%42 = load i32, i32* @ISIG
%43 = or i32 %41, %42
%44 = load i32, i32* @ICANON
%45 = or i32 %43, %44
%46 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 3
store i32 %45, i32* %46
store i32 0, i32* %21
%47 = load i32, i32* @TCIFLUSH
%48 = tail call i32 @tcflush(i32 %0, i32 %47)
%49 = call i32 @cfsetospeed(%struct.termios* %6, i32 %1)
%50 = call i32 @cfsetispeed(%struct.termios* %6, i32 %1)
%51 = load i32, i32* @TCSANOW
%52 = call i64 @tcsetattr(i32 %0, i32 %51, %struct.termios* %6)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %4
%55 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %56
56: ; preds = %4, %54
%57 = phi i32 [ -1, %54 ], [ 1, %4 ]
ret i32 %57
}