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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSANOW;
scalar_t__ close(int);
int /*<<< orphan*/ getenv(char *);
int open(char *, int /*<<< orphan*/);
int read(int, struct termios *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strncat(char *, int /*<<< orphan*/, int);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
scalar_t__ unlink(char *);
int restoreTerm(void) {
  struct termios t;
  int fd;
  char fname[1024];
  fname[0] = 0;
  strncat(fname, getenv("HOME"), 1000);
  strcat(fname, "/.term_settings");
  fd = open(fname, O_RDWR);
  if (fd == -1)
    return 2;
  if (read(fd, &t, sizeof(struct termios)) != sizeof(struct termios))
    return 3;
  if (close(fd))
    return 4;
  if (tcsetattr(0, TCSANOW, &t))
    return 5;
  if (unlink(fname))
    return 6;
  return 0;
}


x86 assembly (clang, -O0):
<restoreTerm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movb   $0x0,-0x410(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    0xe59(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x418(%rbp),%rdi
mov    %eax,%esi
mov    $0x3e8,%edx
call   1050 <strncat@plt>
lea    -0x410(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1200 <restoreTerm+0x80>
movl   $0x2,-0x4(%rbp)
jmp    129a <restoreTerm+0x11a>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cltq
cmp    $0x4,%rax
je     1229 <restoreTerm+0xa9>
movl   $0x3,-0x4(%rbp)
jmp    129a <restoreTerm+0x11a>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     1247 <restoreTerm+0xc7>
movl   $0x4,-0x4(%rbp)
jmp    129a <restoreTerm+0x11a>
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
je     1271 <restoreTerm+0xf1>
movl   $0x5,-0x4(%rbp)
jmp    129a <restoreTerm+0x11a>
lea    -0x410(%rbp),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
je     1293 <restoreTerm+0x113>
movl   $0x6,-0x4(%rbp)
jmp    129a <restoreTerm+0x11a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<restoreTerm>:
push   %rbx
sub    $0x410,%rsp
movb   $0x0,0x10(%rsp)
lea    0xe6c(%rip),%rdi
call   1030 <getenv@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x3e8,%edx
call   1050 <strncat@plt>
lea    0xe51(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1240 <restoreTerm+0xc0>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    $0x3,%eax
cmp    $0x4,%ecx
jne    1245 <restoreTerm+0xc5>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %rax,%rcx
mov    $0x4,%eax
test   %rcx,%rcx
jne    1245 <restoreTerm+0xc5>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1080 <tcsetattr@plt>
mov    %rax,%rcx
mov    $0x5,%eax
test   %rcx,%rcx
jne    1245 <restoreTerm+0xc5>
lea    0x10(%rsp),%rdi
call   1040 <unlink@plt>
test   %rax,%rax
mov    $0x6,%eax
cmove  %ebx,%eax
jmp    1245 <restoreTerm+0xc5>
mov    $0x2,%eax
add    $0x410,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [16 x i8] c"/.term_settings\00"
@O_RDWR = global i32 0
@TCSANOW = global i32 0
define i32 @restoreTerm() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 0, i8* %5
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @strncat(i8* %6, i32 %7, i32 1000)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %0
store i32 2, i32* %1
br label %39
17: ; preds = %0
%18 = load i32, i32* %3
%19 = call i32 @read(i32 %18, %struct.termios* %2, i32 4)
%20 = sext i32 %19 to i64
%21 = icmp ne i64 %20, 4
br i1 %21, label %22, label %23
22: ; preds = %17
store i32 3, i32* %1
br label %39
23: ; preds = %17
%24 = load i32, i32* %3
%25 = call i64 @close(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 4, i32* %1
br label %39
28: ; preds = %23
%29 = load i32, i32* @TCSANOW
%30 = call i64 @tcsetattr(i32 0, i32 %29, %struct.termios* %2)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 5, i32* %1
br label %39
33: ; preds = %28
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%35 = call i64 @unlink(i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 6, i32* %1
br label %39
38: ; preds = %33
store i32 0, i32* %1
br label %39
39: ; preds = %38, %37, %32, %27, %22, %16
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [5 x i8] c"HOME\00"
@.str.1 = [16 x i8] c"/.term_settings\00"
@O_RDWR = global i32 0
@TCSANOW = global i32 0
define i32 @restoreTerm() {
%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
store i8 0, i8* %4
%5 = tail call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @strncat(i8* %4, i32 %5, i32 1000)
%7 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %4, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %25, label %11
11: ; preds = %0
%12 = call i32 @read(i32 %9, %struct.termios* %1, i32 4)
%13 = icmp eq i32 %12, 4
br i1 %13, label %14, label %25
14: ; preds = %11
%15 = call i64 @close(i32 %9)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i32, i32* @TCSANOW
%19 = call i64 @tcsetattr(i32 0, i32 %18, %struct.termios* %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i64 @unlink(i8* %4)
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i32 0, i32 6
br label %25
25: ; preds = %21, %17, %14, %11, %0
%26 = phi i32 [ 2, %0 ], [ 3, %11 ], [ 4, %14 ], [ 5, %17 ], [ %24, %21 ]
ret i32 %26
}