#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*/ sigset_t;
typedef int /*<<< orphan*/ c;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closefrom(int);
int daemon(int /*<<< orphan*/, int);
int dup2(int, int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ process_owner(int);
int receive_message(int, char *, int, int *, int *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int strtol(char *, char **, int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int nullfd;
  char *end;
  int cfd;
  sigset_t empty;
  if (argc != 2 || !argv[1][0])
    goto out_inval;
  cfd = strtol(argv[1], &end, 10);
  if (*end)
    goto out_inval;
  if (daemon(0, 1) == -1) {
    perror("ulockmgr_server: daemon");
    exit(1);
  }
  sigemptyset(&empty);
  sigprocmask(SIG_SETMASK, &empty, NULL);
  if (dup2(cfd, 4) == -1) {
    perror("ulockmgr_server: dup2");
    exit(1);
  }
  cfd = 4;
  nullfd = open("/dev/null", O_RDWR);
  if (nullfd >= 0) {
    dup2(nullfd, 0);
    dup2(nullfd, 1);
  }
  close(3);
  closefrom(5);
  while (1) {
    char c;
    int sock;
    int pid;
    int numfds = 1;
    int res = receive_message(cfd, &c, sizeof(c), &sock, &numfds);
    if (!res)
      break;
    if (res == -1)
      exit(1);
    assert(numfds == 1);
    pid = fork();
    if (pid == -1) {
      perror("ulockmgr_server: fork");
      close(sock);
      continue;
    }
    if (pid == 0) {
      close(cfd);
      pid = fork();
      if (pid == -1) {
        perror("ulockmgr_server: fork");
        _exit(1);
      }
      if (pid == 0)
        process_owner(sock);
      _exit(0);
    }
    waitpid(pid, NULL, 0);
    close(sock);
  }
  return 0;
out_inval:
  fprintf(stderr, "%s should be started by libulockmgr\n", argv[0]);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1241 <main+0x31>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
cmpb   $0x0,(%rax)
jne    1246 <main+0x36>
jmp    143b <main+0x22b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1271 <main+0x61>
jmp    143b <main+0x22b>
xor    %edi,%edi
mov    $0x1,%esi
call   10e0 <daemon@plt>
cmp    $0xffffffff,%eax
jne    129c <main+0x8c>
lea    0xd73(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    -0x28(%rbp),%rdi
call   10b0 <sigemptyset@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x24(%rbp),%edi
mov    $0x4,%esi
call   1070 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12e7 <main+0xd7>
lea    0xd40(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
movl   $0x4,-0x24(%rbp)
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
lea    0xd30(%rip),%rdi
call   1100 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1327 <main+0x117>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1070 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1070 <dup2@plt>
mov    $0x3,%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   1050 <closefrom@plt>
movl   $0x1,-0x38(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
lea    -0x30(%rbp),%rcx
lea    -0x38(%rbp),%r8
call   10d0 <receive_message@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    136d <main+0x15d>
jmp    142f <main+0x21f>
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1381 <main+0x171>
mov    $0x1,%edi
call   1120 <exit@plt>
cmpl   $0x1,-0x38(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    13bf <main+0x1af>
lea    0xc8b(%rip),%rdi
call   1110 <perror@plt>
mov    -0x30(%rbp),%edi
call   1080 <close@plt>
jmp    133b <main+0x12b>
cmpl   $0x0,-0x34(%rbp)
jne    1414 <main+0x204>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    13fb <main+0x1eb>
lea    0xc4c(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
cmpl   $0x0,-0x34(%rbp)
jne    140d <main+0x1fd>
mov    -0x30(%rbp),%edi
call   1060 <process_owner@plt>
xor    %edi,%edi
call   1040 <_exit@plt>
mov    -0x34(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10f0 <waitpid@plt>
mov    -0x30(%rbp),%edi
call   1080 <close@plt>
jmp    133b <main+0x12b>
movl   $0x0,-0x4(%rbp)
jmp    145e <main+0x24e>
mov    0x2b8e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xbfc(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1248 <main+0x38>
mov    0x8(%rbx),%rdi
cmpb   $0x0,(%rdi)
je     1248 <main+0x38>
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%rax
cmpb   $0x0,(%rax)
je     1272 <main+0x62>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdf3(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
mov    $0x1,%esi
call   10e0 <daemon@plt>
cmp    $0xffffffff,%eax
je     13d0 <main+0x1c0>
lea    0x1c(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <sigemptyset@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %ebp,%edi
mov    $0x4,%esi
call   1070 <dup2@plt>
cmp    $0xffffffff,%eax
je     13d9 <main+0x1c9>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
lea    0xd62(%rip),%rdi
call   1100 <open@plt>
test   %eax,%eax
js     12ec <main+0xdc>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1070 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <dup2@plt>
mov    $0x3,%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   1050 <closefrom@plt>
lea    0x7(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%rbx
lea    0xd22(%rip),%r12
jmp    1329 <main+0x119>
mov    %r12,%rdi
call   1110 <perror@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
movl   $0x1,0x8(%rsp)
mov    $0x4,%edi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
mov    %rbx,%r8
call   10d0 <receive_message@plt>
test   %eax,%eax
je     137f <main+0x16f>
cmp    $0xffffffff,%eax
je     1386 <main+0x176>
xor    %edi,%edi
cmpl   $0x1,0x8(%rsp)
sete   %dil
call   1090 <assert@plt>
xor    %eax,%eax
call   1130 <fork@plt>
cmp    $0xffffffff,%eax
je     1318 <main+0x108>
test   %eax,%eax
je     1390 <main+0x180>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10f0 <waitpid@plt>
jmp    1320 <main+0x110>
xor    %eax,%eax
jmp    1265 <main+0x55>
mov    $0x1,%edi
call   1120 <exit@plt>
mov    $0x4,%edi
call   1080 <close@plt>
xor    %eax,%eax
call   1130 <fork@plt>
test   %eax,%eax
je     13c0 <main+0x1b0>
cmp    $0xffffffff,%eax
jne    13c9 <main+0x1b9>
lea    0xc87(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
mov    0xc(%rsp),%edi
call   1060 <process_owner@plt>
xor    %edi,%edi
call   1040 <_exit@plt>
lea    0xc29(%rip),%rdi
jmp    13e0 <main+0x1d0>
lea    0xc38(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"ulockmgr_server: daemon\00"
@SIG_SETMASK = global i32 0
@.str.1 = [22 x i8] c"ulockmgr_server: dup2\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [22 x i8] c"ulockmgr_server: fork\00"
@stderr = global i32 0
@.str.4 = [37 x i8] c"%s should be started by libulockmgr\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %24, label %17
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %17, %2
br label %112
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strtol(i8* %28, i8** %7, i32 10)
store i32 %29, i32* %8
%30 = load i8*, i8** %7
%31 = load i8, i8* %30
%32 = icmp ne i8 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
br label %112
34: ; preds = %25
%35 = call i32 @daemon(i32 0, i32 1)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %34
%41 = call i32 @sigemptyset(i32* %9)
%42 = load i32, i32* @SIG_SETMASK
%43 = call i32 @sigprocmask(i32 %42, i32* %9, i32* null)
%44 = load i32, i32* %8
%45 = call i32 @dup2(i32 %44, i32 4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %40
store i32 4, i32* %8
%51 = load i32, i32* @O_RDWR
%52 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %51)
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = icmp sge i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i32, i32* %6
%57 = call i32 @dup2(i32 %56, i32 0)
%58 = load i32, i32* %6
%59 = call i32 @dup2(i32 %58, i32 1)
br label %60
60: ; preds = %55, %50
%61 = call i32 @close(i32 3)
%62 = call i32 @closefrom(i32 5)
br label %63
63: ; preds = %60, %82, %106
store i32 1, i32* %13
%64 = load i32, i32* %8
%65 = call i32 @receive_message(i32 %64, i8* %10, i32 1, i32* %11, i32* %13)
store i32 %65, i32* %14
%66 = load i32, i32* %14
%67 = icmp ne i32 %66, 0
br i1 %67, label %69, label %68
68: ; preds = %63
br label %111
69: ; preds = %63
%70 = load i32, i32* %14
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %74
72: ; preds = %69
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %69
%75 = load i32, i32* %13
%76 = icmp eq i32 %75, 1
%77 = zext i1 %76 to i32
%78 = call i32 @assert(i32 %77)
%79 = call i32 (...) @fork()
store i32 %79, i32* %12
%80 = load i32, i32* %12
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %86
82: ; preds = %74
%83 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%84 = load i32, i32* %11
%85 = call i32 @close(i32 %84)
br label %63
86: ; preds = %74
%87 = load i32, i32* %12
%88 = icmp eq i32 %87, 0
br i1 %88, label %89, label %106
89: ; preds = %86
%90 = load i32, i32* %8
%91 = call i32 @close(i32 %90)
%92 = call i32 (...) @fork()
store i32 %92, i32* %12
%93 = load i32, i32* %12
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %98
95: ; preds = %89
%96 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%97 = call i32 @_exit(i32 1)
unreachable
98: ; preds = %89
%99 = load i32, i32* %12
%100 = icmp eq i32 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %98
%102 = load i32, i32* %11
%103 = call i32 @process_owner(i32 %102)
br label %104
104: ; preds = %101, %98
%105 = call i32 @_exit(i32 0)
unreachable
106: ; preds = %86
%107 = load i32, i32* %12
%108 = call i32 @waitpid(i32 %107, i32* null, i32 0)
%109 = load i32, i32* %11
%110 = call i32 @close(i32 %109)
br label %63
111: ; preds = %68
store i32 0, i32* %3
br label %118
112: ; preds = %33, %24
%113 = load i32, i32* @stderr
%114 = load i8**, i8*** %5
%115 = getelementptr inbounds i8*, i8** %114, i64 0
%116 = load i8*, i8** %115
%117 = call i32 @fprintf(i32 %113, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %116)
store i32 1, i32* %3
br label %118
118: ; preds = %112, %111
%119 = load i32, i32* %3
ret i32 %119
}

LLVM IR (-O2):
@.str = [24 x i8] c"ulockmgr_server: daemon\00"
@SIG_SETMASK = global i32 0
@.str.1 = [22 x i8] c"ulockmgr_server: dup2\00"
@.str.2 = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
@.str.3 = [22 x i8] c"ulockmgr_server: fork\00"
@stderr = global i32 0
@.str.4 = [37 x i8] c"%s should be started by libulockmgr\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i8** %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = icmp eq i32 %0, 2
br i1 %10, label %11, label %77
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %77, label %16
16: ; preds = %11
%17 = call i32 @strtol(i8* %13, i8** %3, i32 10)
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %77
21: ; preds = %16
%22 = tail call i32 @daemon(i32 0, i32 1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = call i32 @sigemptyset(i32* %4)
%29 = load i32, i32* @SIG_SETMASK
%30 = call i32 @sigprocmask(i32 %29, i32* %4, i32* null)
%31 = call i32 @dup2(i32 %17, i32 4)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = icmp sgt i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @dup2(i32 %38, i32 0)
%42 = call i32 @dup2(i32 %38, i32 1)
br label %43
43: ; preds = %40, %36
%44 = call i32 @close(i32 3)
%45 = call i32 @closefrom(i32 5)
%46 = bitcast i32* %6 to i8*
%47 = bitcast i32* %7 to i8*
br label %48
48: ; preds = %74, %43
store i32 1, i32* %7
%49 = call i32 @receive_message(i32 4, i8* %5, i32 1, i32* %6, i32* %7)
switch i32 %49, label %53 [
i32 0, label %50
i32 -1, label %51
]
50: ; preds = %48
br label %81
51: ; preds = %48
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %48
%54 = load i32, i32* %7
%55 = icmp eq i32 %54, 1
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
%58 = call i32 (...) @fork()
switch i32 %58, label %72 [
i32 -1, label %59
i32 0, label %61
]
59: ; preds = %53
%60 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %74
61: ; preds = %53
%62 = call i32 @close(i32 4)
%63 = call i32 (...) @fork()
switch i32 %63, label %70 [
i32 -1, label %64
i32 0, label %67
]
64: ; preds = %61
%65 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%66 = call i32 @_exit(i32 1)
unreachable
67: ; preds = %61
%68 = load i32, i32* %6
%69 = call i32 @process_owner(i32 %68)
br label %70
70: ; preds = %61, %67
%71 = call i32 @_exit(i32 0)
unreachable
72: ; preds = %53
%73 = call i32 @waitpid(i32 %58, i32* null, i32 0)
br label %74
74: ; preds = %72, %59
%75 = load i32, i32* %6
%76 = call i32 @close(i32 %75)
br label %48
77: ; preds = %16, %2, %11
%78 = load i32, i32* @stderr
%79 = load i8*, i8** %1
%80 = tail call i32 @fprintf(i32 %78, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %50, %77
%82 = phi i32 [ 1, %77 ], [ 0, %50 ]
ret i32 %82
}