#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*/ STDIN_FILENO;
int /*<<< orphan*/ TCSADRAIN;
int /*<<< orphan*/ abort();
int asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(struct termios *);
int /*<<< orphan*/ stderr;
int tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int restore_character_mode(struct termios *old, char **errmsg) {
  if (tcsetattr(STDIN_FILENO, TCSADRAIN, old) == -1) {
    if (asprintf(errmsg, "tcgetattr() failed: %d", errno) == -1) {
      fprintf(stderr, "Out of memory.\n");
      abort();
    }
    return -1;
  }
  free(old);
  return 0;
}


x86 assembly (clang, -O0):
<restore_character_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    11ce <restore_character_mode+0x7e>
mov    -0x18(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
lea    0xe68(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11c2 <restore_character_mode+0x72>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1040 <abort@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <restore_character_mode+0x8e>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<restore_character_mode>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     1185 <restore_character_mode+0x35>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11a9 <restore_character_mode+0x59>
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
lea    0xe6b(%rip),%rsi
mov    %r14,%rdi
call   1060 <asprintf@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     11b1 <restore_character_mode+0x61>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@TCSADRAIN = global i32 0
@.str = [23 x i8] c"tcgetattr() failed: %d\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"Out of memory.\0A\00"
define i32 @restore_character_mode(%struct.termios* %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.termios*
%5 = alloca i8**
store %struct.termios* %0, %struct.termios** %4
store i8** %1, i8*** %5
%6 = load i32, i32* @STDIN_FILENO
%7 = load i32, i32* @TCSADRAIN
%8 = load %struct.termios*, %struct.termios** %4
%9 = call i32 @tcsetattr(i32 %6, i32 %7, %struct.termios* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = load i32, i32* @errno
%14 = call i32 @asprintf(i8** %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 (...) @abort()
unreachable
20: ; preds = %11
store i32 -1, i32* %3
br label %24
21: ; preds = %2
%22 = load %struct.termios*, %struct.termios** %4
%23 = call i32 @free(%struct.termios* %22)
store i32 0, i32* %3
br label %24
24: ; preds = %21, %20
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@TCSADRAIN = global i32 0
@.str = [23 x i8] c"tcgetattr() failed: %d\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"Out of memory.\0A\00"
define i32 @restore_character_mode(%struct.termios* %0, i8** %1) {
%3 = load i32, i32* @STDIN_FILENO
%4 = load i32, i32* @TCSADRAIN
%5 = tail call i32 @tcsetattr(i32 %3, i32 %4, %struct.termios* %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %2
%16 = tail call i32 @free(%struct.termios* %0)
br label %17
17: ; preds = %7, %15
%18 = phi i32 [ 0, %15 ], [ -1, %7 ]
ret i32 %18
}