#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;
  scalar_t__ c_iflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int ISIG;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ TCSANOW;
scalar_t__ close(int);
int /*<<< orphan*/ getenv(char *);
int open(char *, int, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strncat(char *, int /*<<< orphan*/, int);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int write(int, struct termios *, int);
int setTerm(void) {
  struct termios t;
  int fd;
  char fname[1024];
  if (tcgetattr(0, &t))
    return 1;
  fname[0] = 0;
  strncat(fname, getenv("HOME"), 1000);
  strcat(fname, "/.term_settings");
  fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd == -1)
    return 2;
  if (write(fd, &t, sizeof(struct termios)) != sizeof(struct termios))
    return 3;
  if (close(fd))
    return 4;
  t.c_lflag &= ~(ICANON | ECHO | ISIG);
  t.c_iflag = 0;
  if (tcsetattr(0, TCSANOW, &t))
    return 5;
  return 0;
}


x86 assembly (clang, -O0):
<setTerm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
je     11ac <setTerm+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    12f2 <setTerm+0x172>
movb   $0x0,-0x420(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    0xe38(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x428(%rbp),%rdi
mov    %eax,%esi
mov    $0x3e8,%edx
call   1050 <strncat@plt>
lea    -0x420(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x420(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2dee(%rip),%rax
or     (%rax),%esi
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    0x2da4(%rip),%rax
or     (%rax),%edx
mov    0x2d8b(%rip),%rax
or     (%rax),%edx
mov    0x2d7a(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    124e <setTerm+0xce>
movl   $0x2,-0x4(%rbp)
jmp    12f2 <setTerm+0x172>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1040 <write@plt>
cltq
cmp    $0x10,%rax
je     1277 <setTerm+0xf7>
movl   $0x3,-0x4(%rbp)
jmp    12f2 <setTerm+0x172>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     1295 <setTerm+0x115>
movl   $0x4,-0x4(%rbp)
jmp    12f2 <setTerm+0x172>
mov    0x2cfc(%rip),%rax
mov    (%rax),%eax
mov    0x2d2b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d1a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     12eb <setTerm+0x16b>
movl   $0x5,-0x4(%rbp)
jmp    12f2 <setTerm+0x172>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setTerm>:
push   %rbx
sub    $0x410,%rsp
mov    %rsp,%rsi
xor    %edi,%edi
call   1070 <tcgetattr@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
je     11a8 <setTerm+0x28>
add    $0x410,%rsp
pop    %rbx
ret
movb   $0x0,0x10(%rsp)
lea    0xe4c(%rip),%rdi
call   1030 <getenv@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x3e8,%edx
call   1050 <strncat@plt>
lea    0xe31(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    0x2dfd(%rip),%rax
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dab(%rip),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da9(%rip),%rax
or     (%rax),%edx
mov    0x2d98(%rip),%rax
or     (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12a8 <setTerm+0x128>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1040 <write@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    $0x10,%ecx
jne    119f <setTerm+0x1f>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %rax,%rcx
mov    $0x4,%eax
test   %rcx,%rcx
jne    119f <setTerm+0x1f>
mov    0x2d35(%rip),%rax
mov    0x2d66(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d53(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1080 <tcsetattr@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
lea    (%rcx,%rcx,4),%eax
jmp    119f <setTerm+0x1f>
mov    $0x2,%eax
jmp    119f <setTerm+0x1f>

LLVM IR (-O0):
%struct.termios = type { i32, i64 }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [16 x i8] c"/.term_settings\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
define i32 @setTerm() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = call i64 @tcgetattr(i32 0, %struct.termios* %2)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 1, i32* %1
br label %57
8: ; preds = %0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 0, i8* %9
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @strncat(i8* %10, i32 %11, i32 1000)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IROTH
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %15, i32 %18, i32 %25)
store i32 %26, i32* %3
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %8
store i32 2, i32* %1
br label %57
30: ; preds = %8
%31 = load i32, i32* %3
%32 = call i32 @write(i32 %31, %struct.termios* %2, i32 16)
%33 = sext i32 %32 to i64
%34 = icmp ne i64 %33, 16
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 3, i32* %1
br label %57
36: ; preds = %30
%37 = load i32, i32* %3
%38 = call i64 @close(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 4, i32* %1
br label %57
41: ; preds = %36
%42 = load i32, i32* @ICANON
%43 = load i32, i32* @ECHO
%44 = or i32 %42, %43
%45 = load i32, i32* @ISIG
%46 = or i32 %44, %45
%47 = xor i32 %46, -1
%48 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%49 = load i32, i32* %48
%50 = and i32 %49, %47
store i32 %50, i32* %48
%51 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
store i64 0, i64* %51
%52 = load i32, i32* @TCSANOW
%53 = call i64 @tcsetattr(i32 0, i32 %52, %struct.termios* %2)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %41
store i32 5, i32* %1
br label %57
56: ; preds = %41
store i32 0, i32* %1
br label %57
57: ; preds = %56, %55, %40, %35, %29, %7
%58 = load i32, i32* %1
ret i32 %58
}

LLVM IR (-O2):
%struct.termios = type { i32, i64 }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [16 x i8] c"/.term_settings\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
define i32 @setTerm() {
%1 = alloca %struct.termios
%2 = alloca [1024 x i8]
%3 = bitcast %struct.termios* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = call i64 @tcgetattr(i32 0, %struct.termios* %1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %44
7: ; preds = %0
store i8 0, i8* %4
%8 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @strncat(i8* %4, i32 %8, i32 1000)
%10 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = load i32, i32* @S_IRGRP
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IROTH
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %4, i32 %13, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %44, label %23
23: ; preds = %7
%24 = call i32 @write(i32 %21, %struct.termios* %1, i32 16)
%25 = icmp eq i32 %24, 16
br i1 %25, label %26, label %44
26: ; preds = %23
%27 = call i64 @close(i32 %21)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %44
29: ; preds = %26
%30 = load i32, i32* @ICANON
%31 = load i32, i32* @ECHO
%32 = or i32 %31, %30
%33 = load i32, i32* @ISIG
%34 = or i32 %32, %33
%35 = xor i32 %34, -1
%36 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%37 = load i32, i32* %36
%38 = and i32 %37, %35
store i32 %38, i32* %36
%39 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
store i64 0, i64* %39
%40 = load i32, i32* @TCSANOW
%41 = call i64 @tcsetattr(i32 0, i32 %40, %struct.termios* %1)
%42 = icmp eq i64 %41, 0
%43 = select i1 %42, i32 0, i32 5
br label %44
44: ; preds = %29, %26, %23, %7, %0
%45 = phi i32 [ 1, %0 ], [ 2, %7 ], [ 3, %23 ], [ 4, %26 ], [ %43, %29 ]
ret i32 %45
}