#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 EBADF;
int EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lock_tty(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ print_help();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int unlock_tty(int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Insufficient arguments\n");
    return EINVAL;
  }
  if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
    print_help();
    return 0;
  }
  int fd;
  if (argc >= 3) {
    fd = open(argv[2], O_RDONLY);
    if (fd < 0) {
      fprintf(stderr, "Unable to open specified tty \"%s\"\n", argv[2]);
      return EBADF;
    }
  } else {
    fd = 0;
  }
  if (!strcmp(argv[1], "lock")) {
    return lock_tty(fd);
  } else if (!strcmp(argv[1], "unlock")) {
    return unlock_tty(fd);
  } else {
    printf("Unrecognized argument \"%s\"\n", argv[1]);
    return EINVAL;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x48>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e8 <main+0x178>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe51(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
je     11f2 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe37(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1205 <main+0x95>
mov    $0x0,%al
call   1080 <print_help@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e8 <main+0x178>
cmpl   $0x3,-0x8(%rbp)
jl     1267 <main+0xf7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1262 <main+0xf2>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e8 <main+0x178>
jmp    126e <main+0xfe>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdca(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    129b <main+0x12b>
mov    -0x14(%rbp),%edi
call   1090 <lock_tty@plt>
mov    %eax,-0x4(%rbp)
jmp    12e8 <main+0x178>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd9b(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    12c8 <main+0x158>
mov    -0x14(%rbp),%edi
call   1040 <unlock_tty@plt>
mov    %eax,-0x4(%rbp)
jmp    12e8 <main+0x178>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd75(%rip),%rdi
call   1030 <printf@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11a1 <main+0x31>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%ebx
jmp    1238 <main+0xc8>
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%r14
lea    0xe67(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     122f <main+0xbf>
lea    0xe57(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     122f <main+0xbf>
xor    %r15d,%r15d
cmp    $0x3,%ebp
jb     11f5 <main+0x85>
mov    0x10(%rbx),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1257 <main+0xe7>
mov    %eax,%r15d
mov    0x8(%rbx),%r14
lea    0xe4b(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     1245 <main+0xd5>
lea    0xe36(%rip),%rsi
mov    %r14,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     127e <main+0x10e>
lea    0xe2a(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
jmp    1193 <main+0x23>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <print_help@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <lock_tty@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xdb7(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d4f(%rip),%rax
jmp    119a <main+0x2a>
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <unlock_tty@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Insufficient arguments\0A\00"
@EINVAL = global i32 0
@.str.1 = [3 x i8] c"-h\00"
@.str.2 = [7 x i8] c"--help\00"
@O_RDONLY = global i32 0
@.str.3 = [35 x i8] c"Unable to open specified tty \22%s\22\0A\00"
@EBADF = global i32 0
@.str.4 = [5 x i8] c"lock\00"
@.str.5 = [7 x i8] c"unlock\00"
@.str.6 = [28 x i8] c"Unrecognized argument \22%s\22\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EINVAL
store i32 %12, i32* %3
br label %71
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strcmp(i8* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %19, %13
%26 = call i32 (...) @print_help()
store i32 0, i32* %3
br label %71
27: ; preds = %19
%28 = load i32, i32* %4
%29 = icmp sge i32 %28, 3
br i1 %29, label %30, label %46
30: ; preds = %27
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %30
%39 = load i32, i32* @stderr
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @EBADF
store i32 %44, i32* %3
br label %71
45: ; preds = %30
br label %47
46: ; preds = %27
store i32 0, i32* %6
br label %47
47: ; preds = %46, %45
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = call i32 @strcmp(i8* %50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%52 = icmp ne i32 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %47
%54 = load i32, i32* %6
%55 = call i32 @lock_tty(i32 %54)
store i32 %55, i32* %3
br label %71
56: ; preds = %47
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i32 @strcmp(i8* %59, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%61 = icmp ne i32 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %56
%63 = load i32, i32* %6
%64 = call i32 @unlock_tty(i32 %63)
store i32 %64, i32* %3
br label %71
65: ; preds = %56
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 1
%68 = load i8*, i8** %67
%69 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %68)
%70 = load i32, i32* @EINVAL
store i32 %70, i32* %3
br label %71
71: ; preds = %65, %62, %53, %38, %25, %9
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Insufficient arguments\0A\00"
@EINVAL = global i32 0
@.str.1 = [3 x i8] c"-h\00"
@.str.2 = [7 x i8] c"--help\00"
@O_RDONLY = global i32 0
@.str.3 = [35 x i8] c"Unable to open specified tty \22%s\22\0A\00"
@EBADF = global i32 0
@.str.4 = [5 x i8] c"lock\00"
@.str.5 = [7 x i8] c"unlock\00"
@.str.6 = [28 x i8] c"Unrecognized argument \22%s\22\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EINVAL
br label %48
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = tail call i32 @strcmp(i8* dereferenceable(1) %10, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13, %8
%17 = tail call i32 (...) @print_help()
br label %48
18: ; preds = %13
%19 = icmp ugt i32 %0, 2
br i1 %19, label %20, label %33
20: ; preds = %18
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @open(i8* %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %20
%27 = load i8*, i8** %9
br label %33
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %21
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i32, i32* @EBADF
br label %48
33: ; preds = %26, %18
%34 = phi i8* [ %27, %26 ], [ %10, %18 ]
%35 = phi i32 [ %24, %26 ], [ 0, %18 ]
%36 = tail call i32 @strcmp(i8* dereferenceable(1) %34, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = tail call i32 @lock_tty(i32 %35)
br label %48
40: ; preds = %33
%41 = tail call i32 @strcmp(i8* dereferenceable(1) %34, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = tail call i32 @unlock_tty(i32 %35)
br label %48
45: ; preds = %40
%46 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %34)
%47 = load i32, i32* @EINVAL
br label %48
48: ; preds = %28, %38, %43, %45, %16, %4
%49 = phi i32 [ %7, %4 ], [ 0, %16 ], [ %32, %28 ], [ %47, %45 ], [ %44, %43 ], [ %39, %38 ]
ret i32 %49
}