#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 vt_stat {
  struct vt_stat *v_active;
};

/* Variables and functions */
int /*<<< orphan*/ VT_ACTIVATE;
int /*<<< orphan*/ VT_GETSTATE;
int /*<<< orphan*/ VT_WAITACTIVE;
int ioctl(int, int /*<<< orphan*/, struct vt_stat *);
int /*<<< orphan*/ perror(char *);
int fb_activate_current(int tty) {
  struct vt_stat vts;
  if (-1 == ioctl(tty, VT_GETSTATE, &vts)) {
    perror("ioctl VT_GETSTATE");
    return -1;
  }
  if (-1 == ioctl(tty, VT_ACTIVATE, vts.v_active)) {
    perror("ioctl VT_ACTIVATE");
    return -1;
  }
  if (-1 == ioctl(tty, VT_WAITACTIVE, vts.v_active)) {
    perror("ioctl VT_WAITACTIVE");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fb_activate_current>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1167 <fb_activate_current+0x47>
lea    0xeaa(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <fb_activate_current+0xc6>
mov    -0x8(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11a3 <fb_activate_current+0x83>
lea    0xe80(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <fb_activate_current+0xc6>
mov    -0x8(%rbp),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11df <fb_activate_current+0xbf>
lea    0xe56(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <fb_activate_current+0xc6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fb_activate_current>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     117d <fb_activate_current+0x5d>
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1186 <fb_activate_current+0x66>
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     118f <fb_activate_current+0x6f>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe7c(%rip),%rdi
jmp    1196 <fb_activate_current+0x76>
lea    0xe85(%rip),%rdi
jmp    1196 <fb_activate_current+0x76>
lea    0xe8e(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1177 <fb_activate_current+0x57>

LLVM IR (-O0):
%struct.vt_stat = type { %struct.vt_stat* }
@VT_GETSTATE = global i32 0
@.str = [18 x i8] c"ioctl VT_GETSTATE\00"
@VT_ACTIVATE = global i32 0
@.str.1 = [18 x i8] c"ioctl VT_ACTIVATE\00"
@VT_WAITACTIVE = global i32 0
@.str.2 = [20 x i8] c"ioctl VT_WAITACTIVE\00"
define i32 @fb_activate_current(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.vt_stat
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @VT_GETSTATE
%7 = call i32 @ioctl(i32 %5, i32 %6, %struct.vt_stat* %4)
%8 = icmp eq i32 -1, %7
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
11: ; preds = %1
%12 = load i32, i32* %3
%13 = load i32, i32* @VT_ACTIVATE
%14 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %4, i32 0, i32 0
%15 = load %struct.vt_stat*, %struct.vt_stat** %14
%16 = call i32 @ioctl(i32 %12, i32 %13, %struct.vt_stat* %15)
%17 = icmp eq i32 -1, %16
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
20: ; preds = %11
%21 = load i32, i32* %3
%22 = load i32, i32* @VT_WAITACTIVE
%23 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %4, i32 0, i32 0
%24 = load %struct.vt_stat*, %struct.vt_stat** %23
%25 = call i32 @ioctl(i32 %21, i32 %22, %struct.vt_stat* %24)
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %30
29: ; preds = %20
store i32 0, i32* %2
br label %30
30: ; preds = %29, %27, %18, %9
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.vt_stat = type { %struct.vt_stat* }
@VT_GETSTATE = global i32 0
@.str = [18 x i8] c"ioctl VT_GETSTATE\00"
@VT_ACTIVATE = global i32 0
@.str.1 = [18 x i8] c"ioctl VT_ACTIVATE\00"
@VT_WAITACTIVE = global i32 0
@.str.2 = [20 x i8] c"ioctl VT_WAITACTIVE\00"
define i32 @fb_activate_current(i32 %0) {
%2 = alloca %struct.vt_stat
%3 = bitcast %struct.vt_stat* %2 to i8*
%4 = load i32, i32* @VT_GETSTATE
%5 = call i32 @ioctl(i32 %0, i32 %4, %struct.vt_stat* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %18, label %7
7: ; preds = %1
%8 = load i32, i32* @VT_ACTIVATE
%9 = getelementptr inbounds %struct.vt_stat, %struct.vt_stat* %2, i64 0, i32 0
%10 = load %struct.vt_stat*, %struct.vt_stat** %9
%11 = call i32 @ioctl(i32 %0, i32 %8, %struct.vt_stat* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = load i32, i32* @VT_WAITACTIVE
%15 = load %struct.vt_stat*, %struct.vt_stat** %9
%16 = call i32 @ioctl(i32 %0, i32 %14, %struct.vt_stat* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %13, %7, %1
%19 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %7 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %13 ]
%20 = call i32 @perror(i8* %19)
br label %21
21: ; preds = %18, %13
%22 = phi i32 [ 0, %13 ], [ -1, %18 ]
ret i32 %22
}