#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 /*<<< orphan*/ sem_t;

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
scalar_t__ errno;
int getpid();
scalar_t__ getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sem_unlink(char *);
scalar_t__ set_nonroot();
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  sem_t *mysemp;
  char semname[50];
  if (getuid() == 0) {
    if (set_nonroot() != 0) {
      printf("Cannot run this test as non-root user\n");
      return 5;
    }
  }
  sprintf(semname,
          "/"
          "sem_open"
          "_"
          "3-1"
          "_%d",
          getpid());
  mysemp = sem_open(semname, O_CREAT, 0444, 1);
  mysemp = sem_open(semname, O_CREAT, 0222, 1);
  if (mysemp != SEM_FAILED) {
    perror("unexpected error: "
           "sem_open"
           " "
           "3-1"
           ": "
           "sem_open");
    return 2;
  }
  if (errno == EACCES) {
    puts("TEST PASSED");
    sem_unlink(semname);
    return 0;
  } else {
    puts("TEST FAILED");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
jne    11de <main+0x4e>
mov    $0x0,%al
call   1080 <set_nonroot@plt>
cmp    $0x0,%rax
je     11d9 <main+0x49>
lea    0xe38(%rip),%rdi
call   1060 <printf@plt>
movl   $0x5,-0x4(%rbp)
jmp    12b9 <main+0x129>
jmp    11de <main+0x4e>
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xe2d(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    $0x124,%edx
mov    $0x1,%ecx
call   1070 <sem_open@plt>
mov    %rax,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    $0x92,%edx
mov    $0x1,%ecx
call   1070 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%rax
je     126b <main+0xdb>
lea    0xdde(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12b9 <main+0x129>
mov    0x2d56(%rip),%rax
mov    (%rax),%rax
mov    0x2d5c(%rip),%rcx
cmp    (%rcx),%rax
jne    12a6 <main+0x116>
lea    0xdd5(%rip),%rdi
call   1030 <puts@plt>
lea    -0x50(%rbp),%rdi
call   10b0 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b9 <main+0x129>
lea    0xdc0(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     121d <main+0x9d>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe5b(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    0x2e3f(%rip),%r14
mov    (%r14),%esi
mov    %rbx,%rdi
mov    $0x124,%edx
mov    $0x1,%ecx
call   1060 <sem_open@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
mov    $0x92,%edx
mov    $0x1,%ecx
call   1060 <sem_open@plt>
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
jne    1259 <main+0xd9>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rax
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
jne    1240 <main+0xc0>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   10a0 <sem_unlink@plt>
xor    %eax,%eax
jmp    1251 <main+0xd1>
xor    %eax,%eax
call   1070 <set_nonroot@plt>
test   %rax,%rax
je     1197 <main+0x17>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%eax
jmp    1251 <main+0xd1>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb1(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
jmp    1251 <main+0xd1>

LLVM IR (-O0):
@.str = [39 x i8] c"Cannot run this test as non-root user\0A\00"
@.str.1 = [17 x i8] c"/sem_open_3-1_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [41 x i8] c"unexpected error: sem_open 3-1: sem_open\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.3 = [12 x i8] c"TEST PASSED\00"
@.str.4 = [12 x i8] c"TEST FAILED\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [50 x i8]
store i32 0, i32* %1
%4 = call i64 (...) @getuid()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = call i64 (...) @set_nonroot()
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 5, i32* %1
br label %37
11: ; preds = %6
br label %12
12: ; preds = %11, %0
%13 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%14 = call i32 (...) @getpid()
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%17 = load i32, i32* @O_CREAT
%18 = call i32* @sem_open(i8* %16, i32 %17, i32 292, i32 1)
store i32* %18, i32** %2
%19 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%20 = load i32, i32* @O_CREAT
%21 = call i32* @sem_open(i8* %19, i32 %20, i32 146, i32 1)
store i32* %21, i32** %2
%22 = load i32*, i32** %2
%23 = load i32*, i32** @SEM_FAILED
%24 = icmp ne i32* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %37
27: ; preds = %12
%28 = load i64, i64* @errno
%29 = load i64, i64* @EACCES
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%33 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%34 = call i32 @sem_unlink(i8* %33)
store i32 0, i32* %1
br label %37
35: ; preds = %27
%36 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %37
37: ; preds = %35, %31, %25, %9
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str.1 = [17 x i8] c"/sem_open_3-1_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [41 x i8] c"unexpected error: sem_open 3-1: sem_open\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.3 = [12 x i8] c"TEST PASSED\00"
@.str.4 = [12 x i8] c"TEST FAILED\00"
@str = [38 x i8] c"Cannot run this test as non-root user\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @getuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = tail call i64 (...) @set_nonroot()
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %30
10: ; preds = %5, %0
%11 = tail call i32 (...) @getpid()
%12 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @O_CREAT
%14 = call i32* @sem_open(i8* %2, i32 %13, i32 292, i32 1)
%15 = load i32, i32* @O_CREAT
%16 = call i32* @sem_open(i8* %2, i32 %15, i32 146, i32 1)
%17 = load i32*, i32** @SEM_FAILED
%18 = icmp eq i32* %16, %17
br i1 %18, label %21, label %19
19: ; preds = %10
%20 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %30
21: ; preds = %10
%22 = load i64, i64* @errno
%23 = load i64, i64* @EACCES
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @sem_unlink(i8* %2)
br label %30
28: ; preds = %21
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %28, %25, %19, %8
%31 = phi i32 [ 5, %8 ], [ 2, %19 ], [ 0, %25 ], [ 1, %28 ]
ret i32 %31
}