#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_iflag;
  int *c_cc;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};

/* Variables and functions */
int CLOCAL;
int CREAD;
int HUPCL;
int IGNBRK;
int IGNPAR;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ cfsetispeed(struct termios *, int);
int /*<<< orphan*/ cfsetospeed(struct termios *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void setline(int fd, int flags, int speed) {
  struct termios t;
  int result;
  result = tcgetattr(fd, &t);
  if (result) {
    perror("Unable to tcgetattr");
    exit(1);
  }
  t.c_cflag = flags | CREAD | HUPCL | CLOCAL;
  t.c_iflag = IGNBRK | IGNPAR;
  t.c_oflag = 0;
  t.c_lflag = 0;
  t.c_cc[VMIN] = 1;
  t.c_cc[VTIME] = 0;
  cfsetispeed(&t, speed);
  cfsetospeed(&t, speed);
  tcsetattr(fd, TCSANOW, &t);
}


x86 assembly (clang, -O0):
<setline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11a0 <setline+0x40>
lea    0xe6f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%eax
mov    0x2e36(%rip),%rcx
or     (%rcx),%eax
mov    0x2e05(%rip),%rcx
or     (%rcx),%eax
mov    0x2de4(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x28(%rbp),%rax
mov    0x2dba(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1060 <cfsetispeed@plt>
mov    -0xc(%rbp),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    -0x4(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1050 <tcsetattr@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setline>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %rsp,%rsi
call   1040 <tcgetattr@plt>
test   %eax,%eax
jne    1216 <setline+0xb6>
mov    0x2e5a(%rip),%rax
or     (%rax),%ebx
mov    0x2e29(%rip),%rax
or     (%rax),%ebx
mov    0x2e08(%rip),%rax
or     (%rax),%ebx
mov    %ebx,(%rsp)
mov    0x2e24(%rip),%rax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x4(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2de8(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <cfsetispeed@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <cfsetospeed@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1050 <tcsetattr@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde3(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [20 x i8] c"Unable to tcgetattr\00"
@CREAD = global i32 0
@HUPCL = global i32 0
@CLOCAL = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define void @setline(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = call i32 @tcgetattr(i32 %9, %struct.termios* %7)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = load i32, i32* %5
%18 = load i32, i32* @CREAD
%19 = or i32 %17, %18
%20 = load i32, i32* @HUPCL
%21 = or i32 %19, %20
%22 = load i32, i32* @CLOCAL
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @IGNBRK
%26 = load i32, i32* @IGNPAR
%27 = or i32 %25, %26
%28 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 4
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 3
store i64 0, i64* %30
%31 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%32 = load i32*, i32** %31
%33 = load i64, i64* @VMIN
%34 = getelementptr inbounds i32, i32* %32, i64 %33
store i32 1, i32* %34
%35 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 2
%36 = load i32*, i32** %35
%37 = load i64, i64* @VTIME
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 0, i32* %38
%39 = load i32, i32* %6
%40 = call i32 @cfsetispeed(%struct.termios* %7, i32 %39)
%41 = load i32, i32* %6
%42 = call i32 @cfsetospeed(%struct.termios* %7, i32 %41)
%43 = load i32, i32* %4
%44 = load i32, i32* @TCSANOW
%45 = call i32 @tcsetattr(i32 %43, i32 %44, %struct.termios* %7)
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32*, i64, i64 }
@.str = [20 x i8] c"Unable to tcgetattr\00"
@CREAD = global i32 0
@HUPCL = global i32 0
@CLOCAL = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define void @setline(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = call i32 @tcgetattr(i32 %0, %struct.termios* %4)
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = load i32, i32* @CREAD
%13 = or i32 %12, %1
%14 = load i32, i32* @HUPCL
%15 = or i32 %13, %14
%16 = load i32, i32* @CLOCAL
%17 = or i32 %15, %16
%18 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @IGNBRK
%20 = load i32, i32* @IGNPAR
%21 = or i32 %20, %19
%22 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 3
%24 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
%25 = bitcast i64* %23 to i8*
%26 = load i32*, i32** %24
%27 = load i64, i64* @VMIN
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32 1, i32* %28
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %26, i64 %29
store i32 0, i32* %30
%31 = call i32 @cfsetispeed(%struct.termios* %4, i32 %2)
%32 = call i32 @cfsetospeed(%struct.termios* %4, i32 %2)
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %0, i32 %33, %struct.termios* %4)
ret void
}