#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ VT_LOCKSWITCH;
int /*<<< orphan*/ VT_UNLOCKSWITCH;
scalar_t__ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  int lock, fd, success;
  success = 1;
  lock = (argc > 1 && argv && argv[0] && argv[1] && argv[1][0] == 'u') ? 0 : 1;
  fd = open("/dev/console", O_RDWR);
  if (fd < 0) {
    perror("open");
    success = 0;
  }
  if (fd >= 0 && ioctl(fd, (lock ? VT_LOCKSWITCH : VT_UNLOCKSWITCH)) < 0) {
    perror("ioctl");
    success = 0;
  }
  if (fd >= 0 && close(fd) < 0) {
    perror("close");
    success = 0;
  }
  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x8(%rbp)
mov    %al,-0x1d(%rbp)
jle    11b7 <main+0x77>
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x1d(%rbp)
je     11b7 <main+0x77>
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmpq   $0x0,(%rcx)
mov    %al,-0x1d(%rbp)
je     11b7 <main+0x77>
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmpq   $0x0,0x8(%rcx)
mov    %al,-0x1d(%rbp)
je     11b7 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x75,%eax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%dl
mov    $0x1,%eax
xor    %ecx,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ff <main+0xbf>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     125e <main+0x11e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     122a <main+0xea>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    1236 <main+0xf6>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    125e <main+0x11e>
lea    0xdc0(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     128d <main+0x14d>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    128d <main+0x14d>
lea    0xd97(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12a8 <main+0x168>
mov    0x2d22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    12b4 <main+0x174>
mov    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x1,%ebp
cmp    $0x2,%edi
jl     116a <main+0x2a>
test   %rsi,%rsi
je     116a <main+0x2a>
cmpq   $0x0,(%rsi)
je     116a <main+0x2a>
mov    0x8(%rsi),%rax
test   %rax,%rax
je     116a <main+0x2a>
xor    %ebp,%ebp
cmpb   $0x75,(%rax)
setne  %bpl
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c3 <main+0x83>
mov    %eax,%ebx
test   %ebp,%ebp
jne    1192 <main+0x52>
mov    0x2e48(%rip),%rax
jmp    1199 <main+0x59>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11cc <main+0x8c>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     11e4 <main+0xa4>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe43(%rip),%rdi
jmp    11eb <main+0xab>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    11f0 <main+0xb0>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd1(%rip),%rax
jmp    11ba <main+0x7a>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@VT_LOCKSWITCH = global i32 0
@VT_UNLOCKSWITCH = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %32
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = icmp ne i8** %12, null
br i1 %13, label %14, label %32
14: ; preds = %11
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %32
19: ; preds = %14
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8, i8* %27, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 117
br label %32
32: ; preds = %24, %19, %14, %11, %2
%33 = phi i1 [ false, %19 ], [ false, %14 ], [ false, %11 ], [ false, %2 ], [ %31, %24 ]
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 0, i32 1
store i32 %35, i32* %6
%36 = load i32, i32* @O_RDWR
%37 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %8
br label %42
42: ; preds = %40, %32
%43 = load i32, i32* %7
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %59
45: ; preds = %42
%46 = load i32, i32* %7
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = load i32, i32* @VT_LOCKSWITCH
br label %53
51: ; preds = %45
%52 = load i32, i32* @VT_UNLOCKSWITCH
br label %53
53: ; preds = %51, %49
%54 = phi i32 [ %50, %49 ], [ %52, %51 ]
%55 = call i64 @ioctl(i32 %46, i32 %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
br label %59
59: ; preds = %57, %53, %42
%60 = load i32, i32* %7
%61 = icmp sge i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i32, i32* %7
%64 = call i64 @close(i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %8
br label %68
68: ; preds = %66, %62, %59
%69 = load i32, i32* %8
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %68
%72 = load i32, i32* @EXIT_SUCCESS
br label %75
73: ; preds = %68
%74 = load i32, i32* @EXIT_FAILURE
br label %75
75: ; preds = %73, %71
%76 = phi i32 [ %72, %71 ], [ %74, %73 ]
ret i32 %76
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@VT_LOCKSWITCH = global i32 0
@VT_UNLOCKSWITCH = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [6 x i8] c"close\00"
@EXIT_SUCCESS = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = icmp sgt i32 %0, 1
%4 = icmp ne i8** %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %17
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = icmp eq i8* %7, null
br i1 %8, label %17, label %9
9: ; preds = %6
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = load i8, i8* %11
%15 = icmp ne i8 %14, 117
%16 = zext i1 %15 to i32
br label %17
17: ; preds = %13, %9, %6, %2
%18 = phi i32 [ 1, %9 ], [ 1, %6 ], [ 1, %2 ], [ %16, %13 ]
%19 = load i32, i32* @O_RDWR
%20 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %36, label %22
22: ; preds = %17
%23 = icmp eq i32 %18, 0
%24 = load i32, i32* @VT_LOCKSWITCH
%25 = load i32, i32* @VT_UNLOCKSWITCH
%26 = select i1 %23, i32 %25, i32 %24
%27 = tail call i64 @ioctl(i32 %20, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i64 @close(i32 %20)
%32 = icmp slt i64 %31, 0
br i1 %32, label %36, label %39
33: ; preds = %22
%34 = tail call i64 @close(i32 %20)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29, %33, %17
%37 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %33 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %29 ]
%38 = tail call i32 @perror(i8* %37)
br label %39
39: ; preds = %36, %29, %33
%40 = phi i32* [ @EXIT_SUCCESS, %33 ], [ @EXIT_FAILURE, %29 ], [ @EXIT_FAILURE, %36 ]
%41 = load i32, i32* %40
ret i32 %41
}