#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 = 0;
  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;
    }
  }
  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>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     126e <main+0xfe>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1269 <main+0xf9>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e8 <main+0x178>
jmp    126e <main+0xfe>
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 %70
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 %70
27: ; preds = %19
store i32 0, i32* %6
%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 %70
45: ; preds = %30
br label %46
46: ; preds = %45, %27
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @strcmp(i8* %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%51 = icmp ne i32 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %46
%53 = load i32, i32* %6
%54 = call i32 @lock_tty(i32 %53)
store i32 %54, i32* %3
br label %70
55: ; preds = %46
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i64 1
%58 = load i8*, i8** %57
%59 = call i32 @strcmp(i8* %58, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%60 = icmp ne i32 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %55
%62 = load i32, i32* %6
%63 = call i32 @unlock_tty(i32 %62)
store i32 %63, i32* %3
br label %70
64: ; preds = %55
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %67)
%69 = load i32, i32* @EINVAL
store i32 %69, i32* %3
br label %70
70: ; preds = %64, %61, %52, %38, %25, %9
%71 = load i32, i32* %3
ret i32 %71
}

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
}