#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 */
struct timespec {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ sem_t;
typedef int /*<<< orphan*/ proc_name;

/* Variables and functions */
int /*<<< orphan*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ runCmd(char *, char const *);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/);
scalar_t__ sem_timedwait(int /*<<< orphan*/ *, struct timespec *);
int /*<<< orphan*/ sem_unlink(char *);
int /*<<< orphan*/ sleep(int);
scalar_t__ time(int /*<<< orphan*/ *);
int sp_sync_process() {
  const char *proc_name[] = {
      "spMonDaemon",
      NULL,
  };
  int i;
  int sync_proc_cnt = sizeof(proc_name) / sizeof(char *);
  sem_t *sync_sem;
  struct timespec tv;
  if ((sync_sem = sem_open("/SEM_SYNC", O_CREAT, 0600, 0)) == SEM_FAILED) {
    sem_unlink("/SEM_SYNC");
    return -1;
  }
  sleep(5);
  tv.tv_sec = time(NULL) + 20;
  tv.tv_nsec = 0;
  for (i = 0; i < sync_proc_cnt && proc_name[i]; i++) {
    runCmd("killall -SIGUSR1 %s", proc_name[i]);
  }
  printf("sync_proc_cnt = %d\n", sync_proc_cnt);
  while (sync_proc_cnt--) {
    if (sem_timedwait(sync_sem, &tv) < 0) {
      perror("sync_sem:");
      break;
    }
  }
  sem_close(sync_sem);
  sem_unlink("/SEM_SYNC");
  return 0;
}


x86 assembly (clang, -O0):
<sp_sync_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2c61(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c5e(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x28(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
mov    $0x180,%edx
xor    %ecx,%ecx
call   1050 <sem_open@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    11fd <sp_sync_process+0x6d>
lea    0xe20(%rip),%rdi
call   10b0 <sem_unlink@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e7 <sp_sync_process+0x157>
mov    $0x5,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
add    $0x14,%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
cmp    -0x28(%rbp),%ecx
mov    %al,-0x41(%rbp)
jge    1248 <sp_sync_process+0xb8>
movslq -0x24(%rbp),%rax
cmpq   $0x0,-0x20(%rbp,%rax,8)
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1258 <sp_sync_process+0xc8>
jmp    127b <sp_sync_process+0xeb>
movslq -0x24(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rsi
lea    0xdae(%rip),%rdi
call   1090 <runCmd@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1227 <sp_sync_process+0x97>
mov    -0x28(%rbp),%esi
lea    0xda5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x28(%rbp)
cmp    $0x0,%eax
je     12cb <sp_sync_process+0x13b>
mov    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1030 <sem_timedwait@plt>
cmp    $0x0,%rax
jge    12c6 <sp_sync_process+0x136>
lea    0xd82(%rip),%rdi
call   1080 <perror@plt>
jmp    12cb <sp_sync_process+0x13b>
jmp    128a <sp_sync_process+0xfa>
mov    -0x30(%rbp),%rdi
call   1070 <sem_close@plt>
lea    0xd31(%rip),%rdi
call   10b0 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sp_sync_process>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
mov    $0x180,%edx
xor    %ecx,%ecx
call   1050 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e25(%rip),%rax
cmp    (%rax),%rbx
je     123e <sp_sync_process+0xae>
mov    $0x5,%edi
call   10a0 <sleep@plt>
xor    %edi,%edi
call   1060 <time@plt>
add    $0x14,%rax
mov    %rax,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0xe2d(%rip),%rdi
lea    0xe10(%rip),%rsi
call   1090 <runCmd@plt>
lea    0xe2e(%rip),%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <sem_timedwait@plt>
test   %rax,%rax
js     1255 <sp_sync_process+0xc5>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <sem_timedwait@plt>
test   %rax,%rax
js     1255 <sp_sync_process+0xc5>
mov    %rbx,%rdi
call   1070 <sem_close@plt>
lea    0xdd7(%rip),%rdi
call   10b0 <sem_unlink@plt>
xor    %eax,%eax
jmp    124f <sp_sync_process+0xbf>
lea    0xdc7(%rip),%rdi
call   10b0 <sem_unlink@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
jmp    1226 <sp_sync_process+0x96>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@.str = [12 x i8] c"spMonDaemon\00"
@__const.sp_sync_process.proc_name = [2 x i8*] [i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [10 x i8] c"/SEM_SYNC\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [20 x i8] c"killall -SIGUSR1 %s\00"
@.str.3 = [20 x i8] c"sync_proc_cnt = %d\0A\00"
@.str.4 = [10 x i8] c"sync_sem:\00"
define i32 @sp_sync_process() {
%1 = alloca i32
%2 = alloca [2 x i8*]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.timespec
%7 = bitcast [2 x i8*]* %2 to i8*
store i32 2, i32* %4
%8 = load i32, i32* @O_CREAT
%9 = call i32* @sem_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 384, i32 0)
store i32* %9, i32** %5
%10 = load i32*, i32** @SEM_FAILED
%11 = icmp eq i32* %9, %10
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @sem_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
14: ; preds = %0
%15 = call i32 @sleep(i32 5)
%16 = call i64 @time(i32* null)
%17 = add nsw i64 %16, 20
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 %17, i64* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i64 0, i64* %19
store i32 0, i32* %3
br label %20
20: ; preds = %38, %14
%21 = load i32, i32* %3
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %30
24: ; preds = %20
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 %26
%28 = load i8*, i8** %27
%29 = icmp ne i8* %28, null
br label %30
30: ; preds = %24, %20
%31 = phi i1 [ false, %20 ], [ %29, %24 ]
br i1 %31, label %32, label %41
32: ; preds = %30
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @runCmd(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %32
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %20
41: ; preds = %30
%42 = load i32, i32* %4
%43 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %54, %41
%45 = load i32, i32* %4
%46 = add nsw i32 %45, -1
store i32 %46, i32* %4
%47 = icmp ne i32 %45, 0
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = load i32*, i32** %5
%50 = call i64 @sem_timedwait(i32* %49, %struct.timespec* %6)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %55
54: ; preds = %48
br label %44
55: ; preds = %52, %44
%56 = load i32*, i32** %5
%57 = call i32 @sem_close(i32* %56)
%58 = call i32 @sem_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %59
59: ; preds = %55, %12
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@.str = [12 x i8] c"spMonDaemon\00"
@.str.1 = [10 x i8] c"/SEM_SYNC\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [20 x i8] c"killall -SIGUSR1 %s\00"
@.str.3 = [20 x i8] c"sync_proc_cnt = %d\0A\00"
@.str.4 = [10 x i8] c"sync_sem:\00"
define i32 @sp_sync_process() {
%1 = alloca %struct.timespec
%2 = bitcast %struct.timespec* %1 to i8*
%3 = load i32, i32* @O_CREAT
%4 = tail call i32* @sem_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 384, i32 0)
%5 = load i32*, i32** @SEM_FAILED
%6 = icmp eq i32* %4, %5
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %27
9: ; preds = %0
%10 = tail call i32 @sleep(i32 5)
%11 = tail call i64 @time(i32* null)
%12 = add nsw i64 %11, 20
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i64 %12, i64* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i64 0, i64* %14
%15 = tail call i32 @runCmd(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 2)
%17 = call i64 @sem_timedwait(i32* %4, %struct.timespec* %1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %9
%20 = call i64 @sem_timedwait(i32* %4, %struct.timespec* %1)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19, %9
%23 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %19, %22
%25 = call i32 @sem_close(i32* %4)
%26 = call i32 @sem_unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %24, %7
%28 = phi i32 [ -1, %7 ], [ 0, %24 ]
ret i32 %28
}