#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 */
typedef int off_t;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free_lock(int, int);
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read_char(int, int);
int /*<<< orphan*/ read_locks(int);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ set_r_lock(int, int);
int /*<<< orphan*/ set_w_lock(int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_char(int, int);
int run(char operator, int fd, short *flag, off_t max_offset) {
  int offset = 0;
  switch (operator) {
  case '1':
  case '2':
  case '4':
  case '5':
  case '6':
    printf("Set the offset: ");
    scanf("%d", &offset);
  case '3':
    getchar();
  default:
    break;
  }
  if (offset > max_offset) {
    fprintf(stderr, "Offset outside the file");
    return 0;
  }
  switch (operator) {
  case '1':
    set_r_lock(fd, offset);
    break;
  case '2':
    set_w_lock(fd, offset);
    break;
  case '3':
    read_locks(fd);
    break;
  case '4':
    free_lock(fd, offset);
    break;
  case '5':
    read_char(fd, offset);
    break;
  case '6':
    write_char(fd, offset);
    break;
  default:
    *flag = 0;
    break;
  }
  if (errno != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movsbl -0x5(%rbp),%eax
mov    %eax,-0x24(%rbp)
add    $0xffffffcf,%eax
sub    $0x2,%eax
jb     11fc <run+0x5c>
jmp    11d7 <run+0x37>
mov    -0x24(%rbp),%eax
sub    $0x33,%eax
je     1218 <run+0x78>
jmp    11e8 <run+0x48>
mov    -0x24(%rbp),%eax
add    $0xffffffcc,%eax
sub    $0x2,%eax
ja     121f <run+0x7f>
jmp    11fc <run+0x5c>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
lea    0xe1a(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <scanf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
jmp    1224 <run+0x84>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1251 <run+0xb1>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1305 <run+0x165>
movsbl -0x5(%rbp),%eax
add    $0xffffffcf,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x5,%eax
ja     12d8 <run+0x138>
mov    -0x30(%rbp),%rax
lea    0xd8e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%esi
call   10c0 <set_r_lock@plt>
jmp    12e1 <run+0x141>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1050 <set_w_lock@plt>
jmp    12e1 <run+0x141>
mov    -0xc(%rbp),%edi
call   10b0 <read_locks@plt>
jmp    12e1 <run+0x141>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1030 <free_lock@plt>
jmp    12e1 <run+0x141>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1090 <read_char@plt>
jmp    12e1 <run+0x141>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1080 <write_char@plt>
jmp    12e1 <run+0x141>
mov    -0x18(%rbp),%rax
movw   $0x0,(%rax)
mov    0x2cf0(%rip),%rax
cmpq   $0x0,(%rax)
je     12fe <run+0x15e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1305 <run+0x165>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
xor    %esi,%esi
cmp    $0x36,%edi
ja     11fa <run+0x5a>
mov    %ebx,%eax
movabs $0x76000000000000,%rcx
bt     %rax,%rcx
jae    1239 <run+0x99>
lea    0xe3f(%rip),%rdi
call   1040 <printf@plt>
lea    0xe44(%rip),%rdi
lea    0x4(%rsp),%rsi
call   10a0 <scanf@plt>
xor    %eax,%eax
call   1060 <getchar@plt>
mov    0x4(%rsp),%esi
cmp    %ebp,%esi
jle    1217 <run+0x77>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
jmp    1287 <run+0xe7>
add    $0xffffffcf,%ebx
cmp    $0x5,%ebx
ja     1241 <run+0xa1>
lea    0xdda(%rip),%rax
movslq (%rax,%rbx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %r15d,%edi
call   10c0 <set_r_lock@plt>
jmp    1279 <run+0xd9>
cmp    $0x33,%rax
je     11ef <run+0x4f>
jmp    11fa <run+0x5a>
movw   $0x0,(%r14)
jmp    1279 <run+0xd9>
mov    %r15d,%edi
call   1050 <set_w_lock@plt>
jmp    1279 <run+0xd9>
mov    %r15d,%edi
call   10b0 <read_locks@plt>
jmp    1279 <run+0xd9>
mov    %r15d,%edi
call   1030 <free_lock@plt>
jmp    1279 <run+0xd9>
mov    %r15d,%edi
call   1090 <read_char@plt>
jmp    1279 <run+0xd9>
mov    %r15d,%edi
call   1080 <write_char@plt>
mov    0x2d58(%rip),%rax
xor    %ecx,%ecx
cmp    (%rax),%rcx
sbb    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Set the offset: \00"
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Offset outside the file\00"
@errno = global i64 0
define i32 @run(i8 signext %0, i32 %1, i16* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
%8 = alloca i16*
%9 = alloca i32
%10 = alloca i32
store i8 %0, i8* %6
store i32 %1, i32* %7
store i16* %2, i16** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%11 = load i8, i8* %6
%12 = sext i8 %11 to i32
switch i32 %12, label %18 [
i32 49, label %13
i32 50, label %13
i32 52, label %13
i32 53, label %13
i32 54, label %13
i32 51, label %16
]
13: ; preds = %4, %4, %4, %4, %4
%14 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %10)
br label %16
16: ; preds = %4, %13
%17 = call i32 (...) @getchar()
br label %18
18: ; preds = %4, %16
br label %19
19: ; preds = %18
%20 = load i32, i32* %10
%21 = load i32, i32* %9
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %5
br label %59
26: ; preds = %19
%27 = load i8, i8* %6
%28 = sext i8 %27 to i32
switch i32 %28, label %52 [
i32 49, label %29
i32 50, label %33
i32 51, label %37
i32 52, label %40
i32 53, label %44
i32 54, label %48
]
29: ; preds = %26
%30 = load i32, i32* %7
%31 = load i32, i32* %10
%32 = call i32 @set_r_lock(i32 %30, i32 %31)
br label %54
33: ; preds = %26
%34 = load i32, i32* %7
%35 = load i32, i32* %10
%36 = call i32 @set_w_lock(i32 %34, i32 %35)
br label %54
37: ; preds = %26
%38 = load i32, i32* %7
%39 = call i32 @read_locks(i32 %38)
br label %54
40: ; preds = %26
%41 = load i32, i32* %7
%42 = load i32, i32* %10
%43 = call i32 @free_lock(i32 %41, i32 %42)
br label %54
44: ; preds = %26
%45 = load i32, i32* %7
%46 = load i32, i32* %10
%47 = call i32 @read_char(i32 %45, i32 %46)
br label %54
48: ; preds = %26
%49 = load i32, i32* %7
%50 = load i32, i32* %10
%51 = call i32 @write_char(i32 %49, i32 %50)
br label %54
52: ; preds = %26
%53 = load i16*, i16** %8
store i16 0, i16* %53
br label %54
54: ; preds = %52, %48, %44, %40, %37, %33, %29
%55 = load i64, i64* @errno
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %54
store i32 -1, i32* %5
br label %59
58: ; preds = %54
store i32 0, i32* %5
br label %59
59: ; preds = %58, %57, %23
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
@.str = [17 x i8] c"Set the offset: \00"
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"Offset outside the file\00"
@errno = global i64 0
define i32 @run(i8 signext %0, i32 %1, i16* nocapture writeonly %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%7 = sext i8 %0 to i32
switch i32 %7, label %14 [
i32 49, label %8
i32 50, label %8
i32 52, label %8
i32 53, label %8
i32 54, label %8
i32 51, label %11
]
8: ; preds = %4, %4, %4, %4, %4
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
br label %11
11: ; preds = %4, %8
%12 = call i32 (...) @getchar()
%13 = load i32, i32* %5
br label %14
14: ; preds = %11, %4
%15 = phi i32 [ %13, %11 ], [ 0, %4 ]
%16 = icmp sgt i32 %15, %3
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %38
20: ; preds = %14
switch i32 %7, label %33 [
i32 49, label %21
i32 50, label %23
i32 51, label %25
i32 52, label %27
i32 53, label %29
i32 54, label %31
]
21: ; preds = %20
%22 = call i32 @set_r_lock(i32 %1, i32 %15)
br label %34
23: ; preds = %20
%24 = call i32 @set_w_lock(i32 %1, i32 %15)
br label %34
25: ; preds = %20
%26 = call i32 @read_locks(i32 %1)
br label %34
27: ; preds = %20
%28 = call i32 @free_lock(i32 %1, i32 %15)
br label %34
29: ; preds = %20
%30 = call i32 @read_char(i32 %1, i32 %15)
br label %34
31: ; preds = %20
%32 = call i32 @write_char(i32 %1, i32 %15)
br label %34
33: ; preds = %20
store i16 0, i16* %2
br label %34
34: ; preds = %33, %31, %29, %27, %25, %23, %21
%35 = load i64, i64* @errno
%36 = icmp ne i64 %35, 0
%37 = sext i1 %36 to i32
br label %38
38: ; preds = %34, %17
%39 = phi i32 [ 0, %17 ], [ %37, %34 ]
ret i32 %39
}