#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);
  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    1431 <main+0x221>
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    1431 <main+0x221>
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)
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    1363 <main+0x153>
jmp    1425 <main+0x215>
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1377 <main+0x167>
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    13b5 <main+0x1a5>
lea    0xc95(%rip),%rdi
call   1110 <perror@plt>
mov    -0x30(%rbp),%edi
call   1080 <close@plt>
jmp    1331 <main+0x121>
cmpl   $0x0,-0x34(%rbp)
jne    140a <main+0x1fa>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    $0x0,%al
call   1130 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    13f1 <main+0x1e1>
lea    0xc56(%rip),%rdi
call   1110 <perror@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
cmpl   $0x0,-0x34(%rbp)
jne    1403 <main+0x1f3>
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    1331 <main+0x121>
movl   $0x0,-0x4(%rbp)
jmp    1454 <main+0x244>
mov    0x2b98(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xc06(%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>
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    0xd26(%rip),%r12
jmp    1329 <main+0x119>
mov    %r12,%rdi
call   1110 <perror@plt>
nopl   0x0(%rax)
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     1314 <main+0x104>
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 %108
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 %108
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 = call i32 @dup2(i32 %53, i32 0)
%55 = load i32, i32* %6
%56 = call i32 @dup2(i32 %55, i32 1)
%57 = call i32 @close(i32 3)
%58 = call i32 @closefrom(i32 5)
br label %59
59: ; preds = %50, %78, %102
store i32 1, i32* %13
%60 = load i32, i32* %8
%61 = call i32 @receive_message(i32 %60, i8* %10, i32 1, i32* %11, i32* %13)
store i32 %61, i32* %14
%62 = load i32, i32* %14
%63 = icmp ne i32 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %59
br label %107
65: ; preds = %59
%66 = load i32, i32* %14
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %65
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %65
%71 = load i32, i32* %13
%72 = icmp eq i32 %71, 1
%73 = zext i1 %72 to i32
%74 = call i32 @assert(i32 %73)
%75 = call i32 (...) @fork()
store i32 %75, i32* %12
%76 = load i32, i32* %12
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %82
78: ; preds = %70
%79 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%80 = load i32, i32* %11
%81 = call i32 @close(i32 %80)
br label %59
82: ; preds = %70
%83 = load i32, i32* %12
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %102
85: ; preds = %82
%86 = load i32, i32* %8
%87 = call i32 @close(i32 %86)
%88 = call i32 (...) @fork()
store i32 %88, i32* %12
%89 = load i32, i32* %12
%90 = icmp eq i32 %89, -1
br i1 %90, label %91, label %94
91: ; preds = %85
%92 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%93 = call i32 @_exit(i32 1)
unreachable
94: ; preds = %85
%95 = load i32, i32* %12
%96 = icmp eq i32 %95, 0
br i1 %96, label %97, label %100
97: ; preds = %94
%98 = load i32, i32* %11
%99 = call i32 @process_owner(i32 %98)
br label %100
100: ; preds = %97, %94
%101 = call i32 @_exit(i32 0)
unreachable
102: ; preds = %82
%103 = load i32, i32* %12
%104 = call i32 @waitpid(i32 %103, i32* null, i32 0)
%105 = load i32, i32* %11
%106 = call i32 @close(i32 %105)
br label %59
107: ; preds = %64
store i32 0, i32* %3
br label %114
108: ; preds = %33, %24
%109 = load i32, i32* @stderr
%110 = load i8**, i8*** %5
%111 = getelementptr inbounds i8*, i8** %110, i64 0
%112 = load i8*, i8** %111
%113 = call i32 @fprintf(i32 %109, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %112)
store i32 1, i32* %3
br label %114
114: ; preds = %108, %107
%115 = load i32, i32* %3
ret i32 %115
}

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 %74
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 %74, 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 %74
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 = call i32 @dup2(i32 %38, i32 0)
%40 = call i32 @dup2(i32 %38, i32 1)
%41 = call i32 @close(i32 3)
%42 = call i32 @closefrom(i32 5)
%43 = bitcast i32* %6 to i8*
%44 = bitcast i32* %7 to i8*
br label %45
45: ; preds = %71, %36
store i32 1, i32* %7
%46 = call i32 @receive_message(i32 4, i8* %5, i32 1, i32* %6, i32* %7)
switch i32 %46, label %50 [
i32 0, label %47
i32 -1, label %48
]
47: ; preds = %45
br label %78
48: ; preds = %45
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %45
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, 1
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = call i32 (...) @fork()
switch i32 %55, label %69 [
i32 -1, label %56
i32 0, label %58
]
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %71
58: ; preds = %50
%59 = call i32 @close(i32 4)
%60 = call i32 (...) @fork()
switch i32 %60, label %67 [
i32 -1, label %61
i32 0, label %64
]
61: ; preds = %58
%62 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%63 = call i32 @_exit(i32 1)
unreachable
64: ; preds = %58
%65 = load i32, i32* %6
%66 = call i32 @process_owner(i32 %65)
br label %67
67: ; preds = %58, %64
%68 = call i32 @_exit(i32 0)
unreachable
69: ; preds = %50
%70 = call i32 @waitpid(i32 %55, i32* null, i32 0)
br label %71
71: ; preds = %69, %56
%72 = load i32, i32* %6
%73 = call i32 @close(i32 %72)
br label %45
74: ; preds = %16, %2, %11
%75 = load i32, i32* @stderr
%76 = load i8*, i8** %1
%77 = tail call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %76)
br label %78
78: ; preds = %47, %74
%79 = phi i32 [ 1, %74 ], [ 0, %47 ]
ret i32 %79
}