#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_lflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int term_attr(int fd, int set, struct termios *oldattr) {
  int ret;
  if (set) {
    struct termios newattr;
    newattr = *oldattr;
    newattr.c_lflag &= ~(ICANON | ECHO | ISIG);
    return tcsetattr(fd, TCSANOW, &newattr);
  } else {
    ret = tcgetattr(fd, oldattr);
  }
  return ret;
}


x86 assembly (clang, -O0):
<term_attr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1186 <term_attr+0x66>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    0x2e8b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e7a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    %eax,-0x4(%rbp)
jmp    119b <term_attr+0x7b>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<term_attr>:
test   %esi,%esi
je     115a <term_attr+0x3a>
push   %rax
mov    0x2e9c(%rip),%rax
mov    0x2ead(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e9a(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    (%rdx),%ecx
mov    %ecx,(%rsp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <tcsetattr@plt>
pop    %rcx
ret
mov    %rdx,%rsi
jmp    1030 <tcgetattr@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
define i32 @term_attr(i32 %0, i32 %1, %struct.termios* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios*
%8 = alloca i32
%9 = alloca %struct.termios
store i32 %0, i32* %5
store i32 %1, i32* %6
store %struct.termios* %2, %struct.termios** %7
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %3
%13 = load %struct.termios*, %struct.termios** %7
%14 = bitcast %struct.termios* %9 to i8*
%15 = bitcast %struct.termios* %13 to i8*
%16 = load i32, i32* @ICANON
%17 = load i32, i32* @ECHO
%18 = or i32 %16, %17
%19 = load i32, i32* @ISIG
%20 = or i32 %18, %19
%21 = xor i32 %20, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%23 = load i32, i32* %22
%24 = and i32 %23, %21
store i32 %24, i32* %22
%25 = load i32, i32* %5
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 %25, i32 %26, %struct.termios* %9)
store i32 %27, i32* %4
br label %34
28: ; preds = %3
%29 = load i32, i32* %5
%30 = load %struct.termios*, %struct.termios** %7
%31 = call i32 @tcgetattr(i32 %29, %struct.termios* %30)
store i32 %31, i32* %8
br label %32
32: ; preds = %28
%33 = load i32, i32* %8
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %12
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
define i32 @term_attr(i32 %0, i32 %1, %struct.termios* %2) {
%4 = alloca %struct.termios
%5 = icmp eq i32 %1, 0
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = bitcast %struct.termios* %4 to i8*
%8 = getelementptr %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%10 = load i32, i32* %8
%11 = load i32, i32* @ICANON
%12 = load i32, i32* @ECHO
%13 = or i32 %12, %11
%14 = load i32, i32* @ISIG
%15 = or i32 %13, %14
%16 = xor i32 %15, -1
%17 = and i32 %10, %16
store i32 %17, i32* %9
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 %0, i32 %18, %struct.termios* %4)
br label %22
20: ; preds = %3
%21 = tail call i32 @tcgetattr(i32 %0, %struct.termios* %2)
br label %22
22: ; preds = %20, %6
%23 = phi i32 [ %19, %6 ], [ %21, %20 ]
ret i32 %23
}