#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_SUCCESS;
int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getopt(int, char **, char *);
int open_device(unsigned char);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, unsigned char, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage();
int work_mode(int, unsigned char, int);
int main(int argc, char *argv[]) {
  unsigned char mode = 0;
  int pin = -1;
  int c;
  while ((c = getopt(argc, argv, "rwp:h")) != -1) {
    switch (c) {
    case 'r':
      mode |= 0x0002;
      break;
    case 'w':
      mode |= 0x0001;
      break;
    case 'p':
      pin = atoi(optarg);
      break;
    case 'h':
      usage();
      break;
    default:
      fprintf(stderr, "ERROR: no valid argument\n");
      usage();
    }
  }
  printf("Used mode %d and pin %d\n", mode, pin);
  int fd = open_device(mode);
  if (fd == -1)
    usage();
  if (work_mode(fd, mode, pin) == -1)
    usage();
  close(fd);
  return EXIT_SUCCESS;
}


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)
movb   $0x0,-0x11(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdx
call   1090 <getopt@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
je     124a <main+0xca>
mov    -0x1c(%rbp),%eax
add    $0xffffff98,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0xf,%eax
ja     1229 <main+0xa9>
mov    -0x28(%rbp),%rax
lea    0xe20(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movzbl -0x11(%rbp),%eax
or     $0x2,%eax
mov    %al,-0x11(%rbp)
jmp    1245 <main+0xc5>
movzbl -0x11(%rbp),%eax
or     $0x1,%eax
mov    %al,-0x11(%rbp)
jmp    1245 <main+0xc5>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1245 <main+0xc5>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    1245 <main+0xc5>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x0,%al
call   1080 <usage@plt>
jmp    11a1 <main+0x21>
mov    -0x11(%rbp),%al
mov    -0x18(%rbp),%edx
lea    0xe09(%rip),%rdi
movzbl %al,%esi
call   1040 <printf@plt>
movzbl -0x11(%rbp),%edi
call   1070 <open_device@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    127c <main+0xfc>
mov    $0x0,%al
call   1080 <usage@plt>
mov    -0x20(%rbp),%edi
mov    -0x11(%rbp),%al
mov    -0x18(%rbp),%edx
movzbl %al,%esi
call   1030 <work_mode@plt>
cmp    $0xffffffff,%eax
jne    129d <main+0x11d>
mov    $0x0,%al
call   1080 <usage@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0xffffffff,0x4(%rsp)
ff
lea    0xea1(%rip),%r15
mov    0x2e32(%rip),%r13
lea    0xe99(%rip),%r12
lea    0xe4c(%rip),%r14
movl   $0x0,(%rsp)
jmp    11d8 <main+0x58>
nopl   (%rax)
cmp    $0xffffffff,%eax
je     121e <main+0x9e>
mov    0x0(%r13),%edi
mov    %r12,%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
call   1080 <usage@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1090 <getopt@plt>
lea    -0x68(%rax),%ecx
cmp    $0xf,%ecx
ja     11c0 <main+0x40>
movslq (%r14,%rcx,4),%rax
add    %r14,%rax
jmp    *%rax
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,0x4(%rsp)
jmp    11d8 <main+0x58>
mov    (%rsp),%eax
or     $0x2,%al
mov    %eax,(%rsp)
jmp    11d8 <main+0x58>
mov    (%rsp),%eax
or     $0x1,%al
mov    %eax,(%rsp)
jmp    11d8 <main+0x58>
movzbl (%rsp),%ebp
lea    0xe37(%rip),%rdi
mov    %ebp,%esi
mov    0x4(%rsp),%r14d
mov    %r14d,%edx
call   1040 <printf@plt>
mov    %ebp,%edi
call   1070 <open_device@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    124d <main+0xcd>
xor    %eax,%eax
call   1080 <usage@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r14d,%edx
call   1030 <work_mode@plt>
cmp    $0xffffffff,%eax
jne    1265 <main+0xe5>
xor    %eax,%eax
call   1080 <usage@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"rwp:h\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"ERROR: no valid argument\0A\00"
@.str.2 = [25 x i8] c"Used mode %d and pin %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 0, i8* %6
store i32 -1, i32* %7
br label %10
10: ; preds = %36, %2
%11 = load i32, i32* %4
%12 = load i8**, i8*** %5
%13 = call i32 @getopt(i32 %11, i8** %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %13, i32* %8
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %37
15: ; preds = %10
%16 = load i32, i32* %8
switch i32 %16, label %32 [
i32 114, label %17
i32 119, label %22
i32 112, label %27
i32 104, label %30
]
17: ; preds = %15
%18 = load i8, i8* %6
%19 = zext i8 %18 to i32
%20 = or i32 %19, 2
%21 = trunc i32 %20 to i8
store i8 %21, i8* %6
br label %36
22: ; preds = %15
%23 = load i8, i8* %6
%24 = zext i8 %23 to i32
%25 = or i32 %24, 1
%26 = trunc i32 %25 to i8
store i8 %26, i8* %6
br label %36
27: ; preds = %15
%28 = load i32, i32* @optarg
%29 = call i32 @atoi(i32 %28)
store i32 %29, i32* %7
br label %36
30: ; preds = %15
%31 = call i32 (...) @usage()
br label %36
32: ; preds = %15
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 (...) @usage()
br label %36
36: ; preds = %32, %30, %27, %22, %17
br label %10
37: ; preds = %10
%38 = load i8, i8* %6
%39 = load i32, i32* %7
%40 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %38, i32 %39)
%41 = load i8, i8* %6
%42 = call i32 @open_device(i8 zeroext %41)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 (...) @usage()
br label %47
47: ; preds = %45, %37
%48 = load i32, i32* %9
%49 = load i8, i8* %6
%50 = load i32, i32* %7
%51 = call i32 @work_mode(i32 %48, i8 zeroext %49, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 (...) @usage()
br label %55
55: ; preds = %53, %47
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* @EXIT_SUCCESS
ret i32 %58
}

LLVM IR (-O2):
@.str = [6 x i8] c"rwp:h\00"
@optarg = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"ERROR: no valid argument\0A\00"
@.str.2 = [25 x i8] c"Used mode %d and pin %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %16, %2
%4 = phi i32 [ %18, %16 ], [ -1, %2 ]
%5 = phi i8 [ %7, %16 ], [ 0, %2 ]
br label %6
6: ; preds = %14, %3
%7 = phi i8 [ %5, %3 ], [ %15, %14 ]
br label %8
8: ; preds = %25, %6
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %9, label %21 [
i32 -1, label %26
i32 114, label %10
i32 119, label %12
i32 112, label %16
i32 104, label %19
]
10: ; preds = %8
%11 = or i8 %7, 2
br label %14
12: ; preds = %8
%13 = or i8 %7, 1
br label %14
14: ; preds = %12, %10
%15 = phi i8 [ %11, %10 ], [ %13, %12 ]
br label %6
16: ; preds = %8
%17 = load i32, i32* @optarg
%18 = tail call i32 @atoi(i32 %17)
br label %3
19: ; preds = %8
%20 = tail call i32 (...) @usage()
br label %25
21: ; preds = %8
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 (...) @usage()
br label %25
25: ; preds = %21, %19
br label %8
26: ; preds = %8
%27 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %7, i32 %4)
%28 = tail call i32 @open_device(i8 zeroext %7)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = tail call i32 (...) @usage()
br label %32
32: ; preds = %30, %26
%33 = tail call i32 @work_mode(i32 %28, i8 zeroext %7, i32 %4)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = tail call i32 (...) @usage()
br label %37
37: ; preds = %35, %32
%38 = tail call i32 @close(i32 %28)
%39 = load i32, i32* @EXIT_SUCCESS
ret i32 %39
}