#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 uid_t;

/* Variables and functions */
int EINVAL;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int errno;
int geteuid();
int open(char *, int, int);
int /*<<< orphan*/ seteuid(int);
int opentab(int uid, char *file, int how) {
  uid_t safe_uid;
  int flags, r, err;
  switch (how) {
  case 'r':
    flags = O_RDONLY;
    break;
  case 'w':
    flags = O_WRONLY | O_CREAT | O_TRUNC;
    break;
  default:
    errno = EINVAL;
    return -1;
  }
  safe_uid = geteuid();
  seteuid(uid);
  r = open(file, flags, 0666);
  err = errno;
  seteuid(safe_uid);
  errno = err;
  return r;
}


x86 assembly (clang, -O0):
<opentab>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x72,%eax
je     1167 <opentab+0x37>
jmp    1156 <opentab+0x26>
mov    -0x28(%rbp),%eax
sub    $0x77,%eax
je     1178 <opentab+0x48>
jmp    119b <opentab+0x6b>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b9 <opentab+0x89>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    0x2e70(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    11b9 <opentab+0x89>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <opentab+0xd5>
mov    $0x0,%al
call   1030 <geteuid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <seteuid@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,-0x20(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <seteuid@plt>
mov    -0x24(%rbp),%ecx
mov    0x2dcb(%rip),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opentab>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
cmp    $0x77,%edx
je     1153 <opentab+0x23>
cmp    $0x72,%edx
jne    11a4 <opentab+0x74>
mov    0x2e89(%rip),%rax
mov    (%rax),%ebx
jmp    116e <opentab+0x3e>
mov    0x2e56(%rip),%rax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e84(%rip),%rax
or     (%rax),%ebx
xor    %eax,%eax
call   1030 <geteuid@plt>
mov    %eax,%r12d
mov    %r15d,%edi
call   1050 <seteuid@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,%ebx
mov    0x2e30(%rip),%rax
mov    (%rax),%ebp
mov    %r12d,%edi
call   1050 <seteuid@plt>
jmp    11b2 <opentab+0x82>
mov    0x2e15(%rip),%rax
mov    (%rax),%ebp
mov    $0xffffffff,%ebx
mov    0x2e0f(%rip),%rax
mov    %ebp,(%rax)
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @opentab(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32, i32* %7
switch i32 %12, label %21 [
i32 114, label %13
i32 119, label %15
]
13: ; preds = %3
%14 = load i32, i32* @O_RDONLY
store i32 %14, i32* %9
br label %23
15: ; preds = %3
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
store i32 %20, i32* %9
br label %23
21: ; preds = %3
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
store i32 -1, i32* %4
br label %35
23: ; preds = %15, %13
%24 = call i32 (...) @geteuid()
store i32 %24, i32* %8
%25 = load i32, i32* %5
%26 = call i32 @seteuid(i32 %25)
%27 = load i8*, i8** %6
%28 = load i32, i32* %9
%29 = call i32 @open(i8* %27, i32 %28, i32 438)
store i32 %29, i32* %10
%30 = load i32, i32* @errno
store i32 %30, i32* %11
%31 = load i32, i32* %8
%32 = call i32 @seteuid(i32 %31)
%33 = load i32, i32* %11
store i32 %33, i32* @errno
%34 = load i32, i32* %10
store i32 %34, i32* %4
br label %35
35: ; preds = %23, %21
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @opentab(i32 %0, i8* nocapture %1, i32 %2) {
switch i32 %2, label %12 [
i32 114, label %4
i32 119, label %6
]
4: ; preds = %3
%5 = load i32, i32* @O_RDONLY
br label %14
6: ; preds = %3
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
br label %14
12: ; preds = %3
%13 = load i32, i32* @EINVAL
br label %21
14: ; preds = %6, %4
%15 = phi i32 [ %11, %6 ], [ %5, %4 ]
%16 = tail call i32 (...) @geteuid()
%17 = tail call i32 @seteuid(i32 %0)
%18 = tail call i32 @open(i8* %1, i32 %15, i32 438)
%19 = load i32, i32* @errno
%20 = tail call i32 @seteuid(i32 %16)
br label %21
21: ; preds = %14, %12
%22 = phi i32 [ %19, %14 ], [ %13, %12 ]
%23 = phi i32 [ %18, %14 ], [ -1, %12 ]
store i32 %22, i32* @errno
ret i32 %23
}