#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_WRONLY;
scalar_t__ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int write(int, char *, int);
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("/proc/sys/kernel/sysrq", O_WRONLY);
  if (fd < 0) {
    perror("open");
    success = 0;
  }
  if (fd >= 0 && write(fd, (lock ? "0" : "1"), 1) != 1) {
    perror("write");
    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    0x2df7(%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    0xe24(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     124a <main+0x10a>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%ecx
lea    0xe08(%rip),%rsi
lea    0xdff(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     124a <main+0x10a>
lea    0xde2(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1279 <main+0x139>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    1279 <main+0x139>
lea    0xdb9(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1294 <main+0x154>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    12a0 <main+0x160>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%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    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c6 <main+0x86>
mov    %eax,%ebx
test   %ebp,%ebp
lea    0xe90(%rip),%rax
lea    0xe87(%rip),%rsi
cmove  %rax,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    11cf <main+0x8f>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
js     11e7 <main+0xa7>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4a(%rip),%rdi
jmp    11ee <main+0xae>
lea    0xe4a(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jns    11f3 <main+0xb3>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
mov    0x2de6(%rip),%rax
jmp    11bd <main+0x7d>

LLVM IR (-O0):
@.str = [23 x i8] c"/proc/sys/kernel/sysrq\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [2 x i8] c"0\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [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_WRONLY
%37 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 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 %55
45: ; preds = %42
%46 = load i32, i32* %7
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
%49 = zext i1 %48 to i64
%50 = select i1 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%51 = call i32 @write(i32 %46, i8* %50, i32 1)
%52 = icmp ne i32 %51, 1
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %8
br label %55
55: ; preds = %53, %45, %42
%56 = load i32, i32* %7
%57 = icmp sge i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %55
%59 = load i32, i32* %7
%60 = call i64 @close(i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %8
br label %64
64: ; preds = %62, %58, %55
%65 = load i32, i32* %8
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = load i32, i32* @EXIT_SUCCESS
br label %71
69: ; preds = %64
%70 = load i32, i32* @EXIT_FAILURE
br label %71
71: ; preds = %69, %67
%72 = phi i32 [ %68, %67 ], [ %70, %69 ]
ret i32 %72
}

LLVM IR (-O2):
@.str = [23 x i8] c"/proc/sys/kernel/sysrq\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [2 x i8] c"0\00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [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_WRONLY
%20 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %17
%23 = icmp eq i32 %18, 0
%24 = select i1 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%25 = tail call i32 @write(i32 %20, i8* %24, i32 1)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = tail call i64 @close(i32 %20)
%29 = icmp slt i64 %28, 0
br i1 %29, label %34, label %37
30: ; preds = %22
%31 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%32 = tail call i64 @close(i32 %20)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27, %30, %17
%35 = 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.5, i64 0, i64 0), %30 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), %27 ]
%36 = tail call i32 @perror(i8* %35)
br label %37
37: ; preds = %34, %30, %27
%38 = phi i32* [ @EXIT_SUCCESS, %27 ], [ @EXIT_FAILURE, %30 ], [ @EXIT_FAILURE, %34 ]
%39 = load i32, i32* %38
ret i32 %39
}