#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 sembuf {
  int member_1;
  int member_2;
  int /*<<< orphan*/ member_0;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int IPC_CREAT;
int IPC_NOWAIT;
int /*<<< orphan*/ IPC_RMID;
double RAND_MAX;
int SEM_UNDO;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ abs(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ ftok(char *, char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
double rand();
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int);
int semget(int /*<<< orphan*/, int, int);
int semop(int, struct sembuf *, int);
int /*<<< orphan*/ sleep(int /*<<< orphan*/);
scalar_t__ wait(int *);
int main() {
  int semid;
  int i, ret, sta;
  pid_t pid;
  key_t key;
  struct sembuf lock = {0, -1, SEM_UNDO};
  struct sembuf unlock = {0, 1, SEM_UNDO | IPC_NOWAIT};
  key = ftok("/Users/xiaobai/shadowsocks.json", 'b');
  semid = semget(key, 1, IPC_CREAT | 0666);
  if (semid < 0) {
    perror("semget error");
    exit(1);
  }
  ret = semctl(semid, 0, SETVAL, 1);
  if (ret < 0) {
    perror("semctl error");
    exit(1);
  }
  pid = fork();
  if (pid < 0) {
    printf("fork error\n");
    exit(1);
  }
  if (pid == 0) {
    for (i = 0; i < 3; i++) {
      sleep(abs((int)(3.0 * rand() / (RAND_MAX + 1.0))));
      ret = semop(semid, &lock, 1);
      if (ret == -1) {
        perror("lock error");
        exit(1);
      }
      printf("Child process access the resources\n");
      sleep(abs((int)(3.0 * rand() / (RAND_MAX + 1.0))));
      printf("Complete!\n");
      ret = semop(semid, &unlock, 1);
      if (ret == -1) {
        perror("unlock error");
        exit(1);
      }
    }
  } else {
    for (i = 0; i < 3; i++) {
      sleep(abs((int)(3.0 * rand() / (RAND_MAX + 1.0))));
      ret = semop(semid, &lock, 1);
      if (ret == -1) {
        perror("lock error");
        exit(1);
      }
      printf("Parent process access the resources\n");
      sleep(abs((int)(3.0 * rand() / (RAND_MAX + 1.0))));
      printf("Complete!\n");
      ret = semop(semid, &unlock, 1);
      if (ret == -1) {
        perror("unlock error");
        exit(1);
      }
    }
    if (pid != wait(&sta)) {
      printf("wait error\n");
      exit(1);
    }
    ret = semctl(semid, 0, IPC_RMID, 0);
    if (ret == -1) {
      perror("semctl error");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    0x2de1(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
lea    0xdfd(%rip),%rdi
mov    $0x62,%esi
call   10a0 <ftok@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    125f <main+0x9f>
lea    0xde0(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x1,%ecx
call   1070 <semctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    129a <main+0xda>
lea    0xdb2(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    12c6 <main+0x106>
lea    0xd93(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    13f4 <main+0x234>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    13ef <main+0x22f>
mov    $0x0,%al
call   10e0 <rand@plt>
movaps %xmm0,%xmm1
movsd  0xd14(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xd00(%rip),%xmm1
mov    0x2cd1(%rip),%rax
addsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <abs@plt>
mov    %eax,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1353 <main+0x193>
lea    0xd43(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xcfc(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10e0 <rand@plt>
movaps %xmm0,%xmm1
movsd  0xc97(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xc83(%rip),%xmm1
mov    0x2c54(%rip),%rax
addsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <abs@plt>
mov    %eax,%edi
call   10b0 <sleep@plt>
lea    0xcd7(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    13dc <main+0x21c>
lea    0xcb8(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    13e1 <main+0x221>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12d8 <main+0x118>
jmp    1586 <main+0x3c6>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    1512 <main+0x352>
mov    $0x0,%al
call   10e0 <rand@plt>
movaps %xmm0,%xmm1
movsd  0xbf1(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xbdd(%rip),%xmm1
mov    0x2bae(%rip),%rax
addsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <abs@plt>
mov    %eax,%edi
call   10b0 <sleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1476 <main+0x2b6>
lea    0xc20(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xc15(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10e0 <rand@plt>
movaps %xmm0,%xmm1
movsd  0xb74(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xb60(%rip),%xmm1
mov    0x2b31(%rip),%rax
addsd  (%rax),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1040 <abs@plt>
mov    %eax,%edi
call   10b0 <sleep@plt>
lea    0xbb4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    14ff <main+0x33f>
lea    0xb95(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1504 <main+0x344>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    13fb <main+0x23b>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
je     1549 <main+0x389>
lea    0xb7d(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2a75(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1070 <semctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1581 <main+0x3c1>
lea    0xacb(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1586 <main+0x3c6>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movabs $0xffffffff00000000,%rax
ff ff ff
mov    %rax,0x10(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movabs $0x100000000,%rcx
mov    %rcx,(%rsp)
mov    0x2dfe(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
lea    0xe19(%rip),%rdi
mov    $0x62,%esi
call   1090 <ftok@plt>
mov    0x2db8(%rip),%rcx
mov    $0x1b6,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
test   %eax,%eax
js     1674 <main+0x4c4>
mov    %eax,%ebx
mov    0x2d8c(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x1,%ecx
call   1060 <semctl@plt>
test   %eax,%eax
js     1495 <main+0x2e5>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     168a <main+0x4da>
mov    %rax,%r14
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xd9a(%rip),%xmm0
mov    0x2d6b(%rip),%r15
movsd  (%r15),%xmm1
addsd  0xd8e(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
test   %r14,%r14
je     14a1 <main+0x2f1>
cmp    $0xffffffff,%eax
je     1662 <main+0x4b2>
lea    0xdaa(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xd34(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xd2f(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0xdb5(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     166b <main+0x4bb>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xce0(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xcdb(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     1662 <main+0x4b2>
lea    0xd00(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xc8a(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xc85(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0xd0b(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     166b <main+0x4bb>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xc36(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xc31(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     1662 <main+0x4b2>
lea    0xc56(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xbe0(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xbdb(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0xc61(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     166b <main+0x4bb>
lea    0x1c(%rsp),%rdi
call   10b0 <wait@plt>
cmp    %rax,%r14
jne    1693 <main+0x4e3>
mov    0x2b49(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <semctl@plt>
cmp    $0xffffffff,%eax
jne    1656 <main+0x4a6>
lea    0xba1(%rip),%rdi
jmp    167b <main+0x4cb>
cmp    $0xffffffff,%eax
je     1662 <main+0x4b2>
lea    0xbd5(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xb3b(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xb36(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0xbbc(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     166b <main+0x4bb>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xae7(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xae2(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     1662 <main+0x4b2>
lea    0xb2b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xa91(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xa8c(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0xb12(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     166b <main+0x4bb>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0xa3d(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0xa38(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     1662 <main+0x4b2>
lea    0xa85(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10d0 <rand@plt>
mulsd  0x9eb(%rip),%xmm0
movsd  (%r15),%xmm1
addsd  0x9e6(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,%edi
neg    %edi
cmovs  %eax,%edi
call   10a0 <sleep@plt>
lea    0xa6c(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
cmp    $0xffffffff,%eax
je     166b <main+0x4bb>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0x9e3(%rip),%rdi
jmp    167b <main+0x4cb>
lea    0x9d8(%rip),%rdi
jmp    167b <main+0x4cb>
lea    0x9b5(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xa22(%rip),%rdi
jmp    169a <main+0x4ea>
lea    0x9bd(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@SEM_UNDO = global i32 0
@IPC_NOWAIT = global i32 0
@.str = [32 x i8] c"/Users/xiaobai/shadowsocks.json\00"
@IPC_CREAT = global i32 0
@.str.1 = [13 x i8] c"semget error\00"
@SETVAL = global i32 0
@.str.2 = [13 x i8] c"semctl error\00"
@.str.3 = [12 x i8] c"fork error\0A\00"
@RAND_MAX = global double 0.000000e+00
@.str.4 = [11 x i8] c"lock error\00"
@.str.5 = [36 x i8] c"Child process access the resources\0A\00"
@.str.6 = [11 x i8] c"Complete!\0A\00"
@.str.7 = [13 x i8] c"unlock error\00"
@.str.8 = [37 x i8] c"Parent process access the resources\0A\00"
@.str.9 = [12 x i8] c"wait error\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.sembuf
%9 = alloca %struct.sembuf
store i32 0, i32* %1
%10 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %8, i32 0, i32 0
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %8, i32 0, i32 1
store i32 -1, i32* %11
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %8, i32 0, i32 2
%13 = load i32, i32* @SEM_UNDO
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 2
%17 = load i32, i32* @SEM_UNDO
%18 = load i32, i32* @IPC_NOWAIT
%19 = or i32 %17, %18
store i32 %19, i32* %16
%20 = call i32 @ftok(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8 signext 98)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = load i32, i32* @IPC_CREAT
%23 = or i32 %22, 438
%24 = call i32 @semget(i32 %21, i32 1, i32 %23)
store i32 %24, i32* %2
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %0
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %0
%31 = load i32, i32* %2
%32 = load i32, i32* @SETVAL
%33 = call i32 @semctl(i32 %31, i32 0, i32 %32, i32 1)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = call i64 (...) @fork()
store i64 %40, i64* %6
%41 = load i64, i64* %6
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = load i64, i64* %6
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %92
49: ; preds = %46
store i32 0, i32* %3
br label %50
50: ; preds = %88, %49
%51 = load i32, i32* %3
%52 = icmp slt i32 %51, 3
br i1 %52, label %53, label %91
53: ; preds = %50
%54 = call double (...) @rand()
%55 = fmul double 3.000000e+00, %54
%56 = load double, double* @RAND_MAX
%57 = fadd double %56, 1.000000e+00
%58 = fdiv double %55, %57
%59 = fptosi double %58 to i32
%60 = call i32 @abs(i32 %59)
%61 = call i32 @sleep(i32 %60)
%62 = load i32, i32* %2
%63 = call i32 @semop(i32 %62, %struct.sembuf* %8, i32 1)
store i32 %63, i32* %4
%64 = load i32, i32* %4
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %53
%67 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %53
%70 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%71 = call double (...) @rand()
%72 = fmul double 3.000000e+00, %71
%73 = load double, double* @RAND_MAX
%74 = fadd double %73, 1.000000e+00
%75 = fdiv double %72, %74
%76 = fptosi double %75 to i32
%77 = call i32 @abs(i32 %76)
%78 = call i32 @sleep(i32 %77)
%79 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%80 = load i32, i32* %2
%81 = call i32 @semop(i32 %80, %struct.sembuf* %9, i32 1)
store i32 %81, i32* %4
%82 = load i32, i32* %4
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %87
84: ; preds = %69
%85 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%86 = call i32 @exit(i32 1)
unreachable
87: ; preds = %69
br label %88
88: ; preds = %87
%89 = load i32, i32* %3
%90 = add nsw i32 %89, 1
store i32 %90, i32* %3
br label %50
91: ; preds = %50
br label %151
92: ; preds = %46
store i32 0, i32* %3
br label %93
93: ; preds = %131, %92
%94 = load i32, i32* %3
%95 = icmp slt i32 %94, 3
br i1 %95, label %96, label %134
96: ; preds = %93
%97 = call double (...) @rand()
%98 = fmul double 3.000000e+00, %97
%99 = load double, double* @RAND_MAX
%100 = fadd double %99, 1.000000e+00
%101 = fdiv double %98, %100
%102 = fptosi double %101 to i32
%103 = call i32 @abs(i32 %102)
%104 = call i32 @sleep(i32 %103)
%105 = load i32, i32* %2
%106 = call i32 @semop(i32 %105, %struct.sembuf* %8, i32 1)
store i32 %106, i32* %4
%107 = load i32, i32* %4
%108 = icmp eq i32 %107, -1
br i1 %108, label %109, label %112
109: ; preds = %96
%110 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%111 = call i32 @exit(i32 1)
unreachable
112: ; preds = %96
%113 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
%114 = call double (...) @rand()
%115 = fmul double 3.000000e+00, %114
%116 = load double, double* @RAND_MAX
%117 = fadd double %116, 1.000000e+00
%118 = fdiv double %115, %117
%119 = fptosi double %118 to i32
%120 = call i32 @abs(i32 %119)
%121 = call i32 @sleep(i32 %120)
%122 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%123 = load i32, i32* %2
%124 = call i32 @semop(i32 %123, %struct.sembuf* %9, i32 1)
store i32 %124, i32* %4
%125 = load i32, i32* %4
%126 = icmp eq i32 %125, -1
br i1 %126, label %127, label %130
127: ; preds = %112
%128 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%129 = call i32 @exit(i32 1)
unreachable
130: ; preds = %112
br label %131
131: ; preds = %130
%132 = load i32, i32* %3
%133 = add nsw i32 %132, 1
store i32 %133, i32* %3
br label %93
134: ; preds = %93
%135 = load i64, i64* %6
%136 = call i64 @wait(i32* %5)
%137 = icmp ne i64 %135, %136
br i1 %137, label %138, label %141
138: ; preds = %134
%139 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0))
%140 = call i32 @exit(i32 1)
unreachable
141: ; preds = %134
%142 = load i32, i32* %2
%143 = load i32, i32* @IPC_RMID
%144 = call i32 @semctl(i32 %142, i32 0, i32 %143, i32 0)
store i32 %144, i32* %4
%145 = load i32, i32* %4
%146 = icmp eq i32 %145, -1
br i1 %146, label %147, label %150
147: ; preds = %141
%148 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%149 = call i32 @exit(i32 1)
unreachable
150: ; preds = %141
br label %151
151: ; preds = %150, %91
ret i32 0
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@SEM_UNDO = global i32 0
@IPC_NOWAIT = global i32 0
@.str = [32 x i8] c"/Users/xiaobai/shadowsocks.json\00"
@IPC_CREAT = global i32 0
@.str.1 = [13 x i8] c"semget error\00"
@SETVAL = global i32 0
@.str.2 = [13 x i8] c"semctl error\00"
@RAND_MAX = global double 0.000000e+00
@.str.4 = [11 x i8] c"lock error\00"
@.str.7 = [13 x i8] c"unlock error\00"
@IPC_RMID = global i32 0
@str = [11 x i8] c"wait error\00"
@str.10 = [36 x i8] c"Parent process access the resources\00"
@str.12 = [35 x i8] c"Child process access the resources\00"
@str.13 = [10 x i8] c"Complete!\00"
@str.14 = [11 x i8] c"fork error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sembuf
%3 = alloca %struct.sembuf
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sembuf* %2 to i8*
%6 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 0
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 1
store i32 -1, i32* %7
%8 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 2
%9 = load i32, i32* @SEM_UNDO
store i32 %9, i32* %8
%10 = bitcast %struct.sembuf* %3 to i8*
%11 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
%14 = load i32, i32* @IPC_NOWAIT
%15 = or i32 %14, %9
store i32 %15, i32* %13
%16 = tail call i32 @ftok(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8 signext 98)
%17 = load i32, i32* @IPC_CREAT
%18 = or i32 %17, 438
%19 = tail call i32 @semget(i32 %16, i32 1, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %0
%22 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %0
%25 = load i32, i32* @SETVAL
%26 = tail call i32 @semctl(i32 %19, i32 0, i32 %25, i32 1)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = tail call i64 (...) @fork()
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.14, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %31
%38 = icmp eq i64 %32, 0
%39 = call double (...) @rand()
%40 = fmul double %39, 3.000000e+00
%41 = load double, double* @RAND_MAX
%42 = fadd double %41, 1.000000e+00
%43 = fdiv double %40, %42
%44 = fptosi double %43 to i32
%45 = call i32 @llvm.abs.i32(i32 %44, i1 true)
%46 = call i32 @sleep(i32 %45)
%47 = call i32 @semop(i32 %19, %struct.sembuf* %2, i32 1)
%48 = icmp eq i32 %47, -1
br i1 %38, label %50, label %49
49: ; preds = %37
br i1 %48, label %169, label %172
50: ; preds = %37
br i1 %48, label %99, label %102
51: ; preds = %102
%52 = call double (...) @rand()
%53 = fmul double %52, 3.000000e+00
%54 = load double, double* @RAND_MAX
%55 = fadd double %54, 1.000000e+00
%56 = fdiv double %53, %55
%57 = fptosi double %56 to i32
%58 = call i32 @llvm.abs.i32(i32 %57, i1 true)
%59 = call i32 @sleep(i32 %58)
%60 = call i32 @semop(i32 %19, %struct.sembuf* %2, i32 1)
%61 = icmp eq i32 %60, -1
br i1 %61, label %99, label %62
62: ; preds = %51
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.12, i64 0, i64 0))
%64 = call double (...) @rand()
%65 = fmul double %64, 3.000000e+00
%66 = load double, double* @RAND_MAX
%67 = fadd double %66, 1.000000e+00
%68 = fdiv double %65, %67
%69 = fptosi double %68 to i32
%70 = call i32 @llvm.abs.i32(i32 %69, i1 true)
%71 = call i32 @sleep(i32 %70)
%72 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
%73 = call i32 @semop(i32 %19, %struct.sembuf* %3, i32 1)
%74 = icmp eq i32 %73, -1
br i1 %74, label %115, label %75
75: ; preds = %62
%76 = call double (...) @rand()
%77 = fmul double %76, 3.000000e+00
%78 = load double, double* @RAND_MAX
%79 = fadd double %78, 1.000000e+00
%80 = fdiv double %77, %79
%81 = fptosi double %80 to i32
%82 = call i32 @llvm.abs.i32(i32 %81, i1 true)
%83 = call i32 @sleep(i32 %82)
%84 = call i32 @semop(i32 %19, %struct.sembuf* %2, i32 1)
%85 = icmp eq i32 %84, -1
br i1 %85, label %99, label %86
86: ; preds = %75
%87 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.12, i64 0, i64 0))
%88 = call double (...) @rand()
%89 = fmul double %88, 3.000000e+00
%90 = load double, double* @RAND_MAX
%91 = fadd double %90, 1.000000e+00
%92 = fdiv double %89, %91
%93 = fptosi double %92 to i32
%94 = call i32 @llvm.abs.i32(i32 %93, i1 true)
%95 = call i32 @sleep(i32 %94)
%96 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
%97 = call i32 @semop(i32 %19, %struct.sembuf* %3, i32 1)
%98 = icmp eq i32 %97, -1
br i1 %98, label %115, label %198
99: ; preds = %75, %51, %50
%100 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%101 = call i32 @exit(i32 1)
unreachable
102: ; preds = %50
%103 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.12, i64 0, i64 0))
%104 = call double (...) @rand()
%105 = fmul double %104, 3.000000e+00
%106 = load double, double* @RAND_MAX
%107 = fadd double %106, 1.000000e+00
%108 = fdiv double %105, %107
%109 = fptosi double %108 to i32
%110 = call i32 @llvm.abs.i32(i32 %109, i1 true)
%111 = call i32 @sleep(i32 %110)
%112 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
%113 = call i32 @semop(i32 %19, %struct.sembuf* %3, i32 1)
%114 = icmp eq i32 %113, -1
br i1 %114, label %115, label %51
115: ; preds = %86, %62, %102
%116 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%117 = call i32 @exit(i32 1)
unreachable
118: ; preds = %172
%119 = call double (...) @rand()
%120 = fmul double %119, 3.000000e+00
%121 = load double, double* @RAND_MAX
%122 = fadd double %121, 1.000000e+00
%123 = fdiv double %120, %122
%124 = fptosi double %123 to i32
%125 = call i32 @llvm.abs.i32(i32 %124, i1 true)
%126 = call i32 @sleep(i32 %125)
%127 = call i32 @semop(i32 %19, %struct.sembuf* %2, i32 1)
%128 = icmp eq i32 %127, -1
br i1 %128, label %169, label %129
129: ; preds = %118
%130 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.10, i64 0, i64 0))
%131 = call double (...) @rand()
%132 = fmul double %131, 3.000000e+00
%133 = load double, double* @RAND_MAX
%134 = fadd double %133, 1.000000e+00
%135 = fdiv double %132, %134
%136 = fptosi double %135 to i32
%137 = call i32 @llvm.abs.i32(i32 %136, i1 true)
%138 = call i32 @sleep(i32 %137)
%139 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
%140 = call i32 @semop(i32 %19, %struct.sembuf* %3, i32 1)
%141 = icmp eq i32 %140, -1
br i1 %141, label %185, label %142
142: ; preds = %129
%143 = call double (...) @rand()
%144 = fmul double %143, 3.000000e+00
%145 = load double, double* @RAND_MAX
%146 = fadd double %145, 1.000000e+00
%147 = fdiv double %144, %146
%148 = fptosi double %147 to i32
%149 = call i32 @llvm.abs.i32(i32 %148, i1 true)
%150 = call i32 @sleep(i32 %149)
%151 = call i32 @semop(i32 %19, %struct.sembuf* %2, i32 1)
%152 = icmp eq i32 %151, -1
br i1 %152, label %169, label %153
153: ; preds = %142
%154 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.10, i64 0, i64 0))
%155 = call double (...) @rand()
%156 = fmul double %155, 3.000000e+00
%157 = load double, double* @RAND_MAX
%158 = fadd double %157, 1.000000e+00
%159 = fdiv double %156, %158
%160 = fptosi double %159 to i32
%161 = call i32 @llvm.abs.i32(i32 %160, i1 true)
%162 = call i32 @sleep(i32 %161)
%163 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
%164 = call i32 @semop(i32 %19, %struct.sembuf* %3, i32 1)
%165 = icmp eq i32 %164, -1
br i1 %165, label %185, label %166
166: ; preds = %153
%167 = call i64 @wait(i32* %1)
%168 = icmp eq i64 %32, %167
br i1 %168, label %191, label %188
169: ; preds = %142, %118, %49
%170 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%171 = call i32 @exit(i32 1)
unreachable
172: ; preds = %49
%173 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.10, i64 0, i64 0))
%174 = call double (...) @rand()
%175 = fmul double %174, 3.000000e+00
%176 = load double, double* @RAND_MAX
%177 = fadd double %176, 1.000000e+00
%178 = fdiv double %175, %177
%179 = fptosi double %178 to i32
%180 = call i32 @llvm.abs.i32(i32 %179, i1 true)
%181 = call i32 @sleep(i32 %180)
%182 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.13, i64 0, i64 0))
%183 = call i32 @semop(i32 %19, %struct.sembuf* %3, i32 1)
%184 = icmp eq i32 %183, -1
br i1 %184, label %185, label %118
185: ; preds = %153, %129, %172
%186 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%187 = call i32 @exit(i32 1)
unreachable
188: ; preds = %166
%189 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%190 = call i32 @exit(i32 1)
unreachable
191: ; preds = %166
%192 = load i32, i32* @IPC_RMID
%193 = call i32 @semctl(i32 %19, i32 0, i32 %192, i32 0)
%194 = icmp eq i32 %193, -1
br i1 %194, label %195, label %198
195: ; preds = %191
%196 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%197 = call i32 @exit(i32 1)
unreachable
198: ; preds = %86, %191
ret i32 0
}