#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 */

/* Variables and functions */
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int atoi(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getopt(int, char **, char *);
int /*<<< orphan*/ help();
int loop(char *);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int main(int argc, char *argv[]) {
  int opt;
  char user[4096] = {0};
  int lock_fd = -1;
  int flags;
  while ((opt = getopt(argc, argv, "u:l:h")) != -1) {
    switch (opt) {
    case 'u':
      strncpy(user, optarg, 4096);
      break;
    case 'l':
      lock_fd = atoi(optarg);
      break;
    case 'h':
      help();
      return 1;
    }
  }
  if (user[0] == 0) {
    help();
    return 1;
  }
  if (lock_fd > 0) {
    flags = fcntl(lock_fd, F_GETFD);
    if (flags < 0) {
      fprintf(stderr, "Could not get flags for PID file, fd is %d\n", lock_fd);
      return 1;
    }
    flags |= FD_CLOEXEC;
    if (fcntl(lock_fd, F_SETFD, flags) == -1) {
      fprintf(stderr, "Could not set flags for PID file, fd is %d\n", lock_fd);
      return 1;
    }
  }
  return loop(user);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
movl   $0xffffffff,-0x1024(%rbp)
ff ff ff
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe3c(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1264 <main+0xe4>
mov    -0x14(%rbp),%eax
mov    %eax,-0x102c(%rbp)
sub    $0x68,%eax
je     124c <main+0xcc>
jmp    11ec <main+0x6c>
mov    -0x102c(%rbp),%eax
sub    $0x6c,%eax
je     1233 <main+0xb3>
jmp    1200 <main+0x80>
mov    -0x102c(%rbp),%eax
sub    $0x75,%eax
jne    125f <main+0xdf>
jmp    1214 <main+0x94>
lea    -0x1020(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
jmp    125f <main+0xdf>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10a0 <atoi@plt>
mov    %eax,-0x1024(%rbp)
jmp    125f <main+0xdf>
mov    $0x0,%al
call   1090 <help@plt>
movl   $0x1,-0x4(%rbp)
jmp    1359 <main+0x1d9>
jmp    11b6 <main+0x36>
movsbl -0x1020(%rbp),%eax
cmp    $0x0,%eax
jne    1287 <main+0x107>
mov    $0x0,%al
call   1090 <help@plt>
movl   $0x1,-0x4(%rbp)
jmp    1359 <main+0x1d9>
cmpl   $0x0,-0x1024(%rbp)
jle    134a <main+0x1ca>
mov    -0x1024(%rbp),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x1028(%rbp)
cmpl   $0x0,-0x1028(%rbp)
jge    12e4 <main+0x164>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
mov    -0x1024(%rbp),%edx
lea    0xd33(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1359 <main+0x1d9>
mov    0x2cdd(%rip),%rax
mov    (%rax),%eax
or     -0x1028(%rbp),%eax
mov    %eax,-0x1028(%rbp)
mov    -0x1024(%rbp),%edi
mov    0x2cca(%rip),%rax
mov    (%rax),%esi
mov    -0x1028(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1345 <main+0x1c5>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
mov    -0x1024(%rbp),%edx
lea    0xcfe(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1359 <main+0x1d9>
jmp    134a <main+0x1ca>
lea    -0x1020(%rbp),%rdi
call   1050 <loop@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%r15
mov    $0x1000,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    $0xffffffff,%r14d
lea    0xe4b(%rip),%r12
mov    0x2e3c(%rip),%r13
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1080 <getopt@plt>
cmp    $0x74,%eax
jle    11f0 <main+0x70>
cmp    $0x75,%eax
jne    11c0 <main+0x40>
mov    0x0(%r13),%esi
mov    %r15,%rdi
mov    $0x1000,%edx
call   1030 <strncpy@plt>
jmp    11c0 <main+0x40>
nopw   0x0(%rax,%rax,1)
cmp    $0xffffffff,%eax
je     120d <main+0x8d>
cmp    $0x68,%eax
je     125c <main+0xdc>
cmp    $0x6c,%eax
jne    11c0 <main+0x40>
mov    0x0(%r13),%edi
call   10a0 <atoi@plt>
mov    %eax,%r14d
jmp    11c0 <main+0x40>
cmpb   $0x0,(%rsp)
je     125c <main+0xdc>
test   %r14d,%r14d
jle    1252 <main+0xd2>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     127a <main+0xfa>
mov    0x2d92(%rip),%rcx
or     (%rcx),%eax
mov    0x2d91(%rip),%rcx
mov    (%rcx),%esi
mov    %r14d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     128c <main+0x10c>
mov    %rsp,%rdi
call   1050 <loop@plt>
jmp    1268 <main+0xe8>
xor    %eax,%eax
call   1090 <help@plt>
mov    $0x1,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
jmp    129c <main+0x11c>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xd96(%rip),%rsi
mov    %r14d,%edx
call   1070 <fprintf@plt>
jmp    1263 <main+0xe3>

LLVM IR (-O0):
@.str = [6 x i8] c"u:l:h\00"
@optarg = global i32 0
@F_GETFD = global i32 0
@stderr = global i32 0
@.str.1 = [44 x i8] c"Could not get flags for PID file, fd is %d\0A\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.2 = [44 x i8] c"Could not set flags for PID file, fd is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [4096 x i8]* %7 to i8*
store i32 -1, i32* %8
br label %11
11: ; preds = %27, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %6
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %28
16: ; preds = %11
%17 = load i32, i32* %6
switch i32 %17, label %27 [
i32 117, label %18
i32 108, label %22
i32 104, label %25
]
18: ; preds = %16
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%20 = load i32, i32* @optarg
%21 = call i32 @strncpy(i8* %19, i32 %20, i32 4096)
br label %27
22: ; preds = %16
%23 = load i32, i32* @optarg
%24 = call i32 @atoi(i32 %23)
store i32 %24, i32* %8
br label %27
25: ; preds = %16
%26 = call i32 (...) @help()
store i32 1, i32* %3
br label %65
27: ; preds = %16, %22, %18
br label %11
28: ; preds = %11
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 (...) @help()
store i32 1, i32* %3
br label %65
35: ; preds = %28
%36 = load i32, i32* %8
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %62
38: ; preds = %35
%39 = load i32, i32* %8
%40 = load i32, i32* @F_GETFD
%41 = call i32 (i32, i32, ...) @fcntl(i32 %39, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* @stderr
%46 = load i32, i32* %8
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %46)
store i32 1, i32* %3
br label %65
48: ; preds = %38
%49 = load i32, i32* @FD_CLOEXEC
%50 = load i32, i32* %9
%51 = or i32 %50, %49
store i32 %51, i32* %9
%52 = load i32, i32* %8
%53 = load i32, i32* @F_SETFD
%54 = load i32, i32* %9
%55 = call i32 (i32, i32, ...) @fcntl(i32 %52, i32 %53, i32 %54)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %61
57: ; preds = %48
%58 = load i32, i32* @stderr
%59 = load i32, i32* %8
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %59)
store i32 1, i32* %3
br label %65
61: ; preds = %48
br label %62
62: ; preds = %61, %35
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%64 = call i32 @loop(i8* %63)
store i32 %64, i32* %3
br label %65
65: ; preds = %62, %57, %44, %33, %25
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [6 x i8] c"u:l:h\00"
@optarg = global i32 0
@F_GETFD = global i32 0
@stderr = global i32 0
@.str.1 = [44 x i8] c"Could not get flags for PID file, fd is %d\0A\00"
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.2 = [44 x i8] c"Could not set flags for PID file, fd is %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %13, %2
%6 = phi i32 [ %15, %13 ], [ -1, %2 ]
br label %7
7: ; preds = %9, %5
%8 = call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %8, label %9 [
i32 -1, label %18
i32 117, label %10
i32 108, label %13
i32 104, label %16
]
9: ; preds = %7, %10
br label %7
10: ; preds = %7
%11 = load i32, i32* @optarg
%12 = call i32 @strncpy(i8* %4, i32 %11, i32 4096)
br label %9
13: ; preds = %7
%14 = load i32, i32* @optarg
%15 = call i32 @atoi(i32 %14)
br label %5
16: ; preds = %7
%17 = call i32 (...) @help()
br label %43
18: ; preds = %7
%19 = load i8, i8* %4
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 (...) @help()
br label %43
23: ; preds = %18
%24 = icmp sgt i32 %6, 0
br i1 %24, label %25, label %41
25: ; preds = %23
%26 = load i32, i32* @F_GETFD
%27 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %43
32: ; preds = %25
%33 = load i32, i32* @FD_CLOEXEC
%34 = or i32 %33, %27
%35 = load i32, i32* @F_SETFD
%36 = call i32 (i32, i32, ...) @fcntl(i32 %6, i32 %35, i32 %34)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %6)
br label %43
41: ; preds = %32, %23
%42 = call i32 @loop(i8* %4)
br label %43
43: ; preds = %41, %38, %29, %21, %16
%44 = phi i32 [ 1, %16 ], [ 1, %21 ], [ 1, %29 ], [ 1, %38 ], [ %42, %41 ]
ret i32 %44
}