#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ VT_ACTIVATE;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open_console(int /*<<< orphan*/);
int twiddler_console(char *s) {
  int consolenr = atoi(s);
  int fd;
  if (consolenr == 0)
    return 0;
  fd = open_console(O_RDONLY);
  if (fd < 0)
    return -1;
  if (ioctl(fd, VT_ACTIVATE, consolenr) < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<twiddler_console>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    116e <twiddler_console+0x2e>
movl   $0x0,-0x4(%rbp)
jmp    11d6 <twiddler_console+0x96>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1050 <open_console@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1195 <twiddler_console+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <twiddler_console+0x96>
mov    -0x18(%rbp),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11c7 <twiddler_console+0x87>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <twiddler_console+0x96>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<twiddler_console>:
push   %rbp
push   %rbx
push   %rax
call   1060 <atoi@plt>
test   %eax,%eax
je     1184 <twiddler_console+0x44>
mov    %eax,%ebx
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
call   1050 <open_console@plt>
test   %eax,%eax
js     1188 <twiddler_console+0x48>
mov    %eax,%ebp
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <ioctl@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
sar    $0x3f,%rbx
jmp    118d <twiddler_console+0x4d>
xor    %ebx,%ebx
jmp    118d <twiddler_console+0x4d>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@VT_ACTIVATE = global i32 0
define i32 @twiddler_console(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @atoi(i8* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 0, i32* %2
br label %29
11: ; preds = %1
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open_console(i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 -1, i32* %2
br label %29
17: ; preds = %11
%18 = load i32, i32* %5
%19 = load i32, i32* @VT_ACTIVATE
%20 = load i32, i32* %4
%21 = call i64 @ioctl(i32 %18, i32 %19, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
br label %29
26: ; preds = %17
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
store i32 0, i32* %2
br label %29
29: ; preds = %26, %23, %16, %10
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@VT_ACTIVATE = global i32 0
define i32 @twiddler_console(i8* nocapture %0) {
%2 = tail call i32 @atoi(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open_console(i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = load i32, i32* @VT_ACTIVATE
%10 = tail call i64 @ioctl(i32 %6, i32 %9, i32 %2)
%11 = tail call i32 @close(i32 %6)
%12 = ashr i64 %10, 63
%13 = trunc i64 %12 to i32
br label %14
14: ; preds = %8, %4, %1
%15 = phi i32 [ 0, %1 ], [ -1, %4 ], [ %13, %8 ]
ret i32 %15
}