#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 /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
int flock(int, int /*<<< orphan*/);
int geteuid();
int getpid();
int getuid();
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ print_ids();
int /*<<< orphan*/ printf(char *, ...);
int setreuid(int, int);
int main(int argc, char *argv[]) {
  printf("in child %u\n", getpid());
  if (argc < 2) {
    print_ids();
    return -1;
  }
  int ret = 0;
  char const *lock = argv[1];
  int uid = getuid();
  int euid = geteuid();
  print_ids();
  ret = setreuid(euid, uid);
  if (ret == -1)
    err_sys("setreuid");
  printf("setuid back to %d.%d\n", euid, uid);
  print_ids();
  int fd = open(lock, O_RDWR);
  if (fd == -1)
    err_sys("open");
  printf("open lock file : %d\n", fd);
  ret = flock(fd, LOCK_EX);
  if (ret == -1)
    err_sys("flock");
  printf("lock file\n");
  ret = flock(fd, LOCK_UN);
  if (ret == -1)
    err_sys("funlock");
  close(fd);
  printf("uucp exit \n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11ea <main+0x4a>
mov    $0x0,%al
call   1090 <print_ids@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <main+0x172>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <print_ids@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10a0 <setreuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    123c <main+0x9c>
lea    0xdd6(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1090 <print_ids@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1282 <main+0xe2>
lea    0xdaf(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x2c(%rbp),%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d43(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12bd <main+0x11d>
lea    0xd8e(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12f5 <main+0x155>
lea    0xd67(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe39(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x1,%ebp
jg     11e6 <main+0x36>
xor    %eax,%eax
call   10a0 <print_ids@plt>
mov    $0xffffffff,%eax
jmp    12cd <main+0x11d>
mov    0x8(%rbx),%r14
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <geteuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <print_ids@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   10b0 <setreuid@plt>
cmp    $0xffffffff,%eax
jne    121d <main+0x6d>
lea    0xdf5(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdf2(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10a0 <print_ids@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10c0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    125a <main+0xaa>
lea    0xdd7(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdd0(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10d0 <flock@plt>
cmp    $0xffffffff,%eax
jne    128b <main+0xdb>
lea    0xdc0(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10d0 <flock@plt>
cmp    $0xffffffff,%eax
jne    12b8 <main+0x108>
lea    0xd99(%rip),%rdi
call   1040 <err_sys@plt>
mov    %ebx,%edi
call   1090 <close@plt>
lea    0xd98(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"in child %u\0A\00"
@.str.1 = [9 x i8] c"setreuid\00"
@.str.2 = [22 x i8] c"setuid back to %d.%d\0A\00"
@O_RDWR = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [21 x i8] c"open lock file : %d\0A\00"
@LOCK_EX = global i32 0
@.str.5 = [6 x i8] c"flock\00"
@.str.6 = [11 x i8] c"lock file\0A\00"
@LOCK_UN = global i32 0
@.str.7 = [8 x i8] c"funlock\00"
@.str.8 = [12 x i8] c"uucp exit \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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @getpid()
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (...) @print_ids()
store i32 -1, i32* %3
br label %66
17: ; preds = %2
store i32 0, i32* %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %7
%21 = call i32 (...) @getuid()
store i32 %21, i32* %8
%22 = call i32 (...) @geteuid()
store i32 %22, i32* %9
%23 = call i32 (...) @print_ids()
%24 = load i32, i32* %9
%25 = load i32, i32* %8
%26 = call i32 @setreuid(i32 %24, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = call i32 @err_sys(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %17
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
%35 = call i32 (...) @print_ids()
%36 = load i8*, i8** %7
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %31
%42 = call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %31
%44 = load i32, i32* %10
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %10
%47 = load i32, i32* @LOCK_EX
%48 = call i32 @flock(i32 %46, i32 %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %43
%52 = call i32 @err_sys(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %43
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%55 = load i32, i32* %10
%56 = load i32, i32* @LOCK_UN
%57 = call i32 @flock(i32 %55, i32 %56)
store i32 %57, i32* %6
%58 = load i32, i32* %6
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %53
%61 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %62
62: ; preds = %60, %53
%63 = load i32, i32* %10
%64 = call i32 @close(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %3
br label %66
66: ; preds = %62, %15
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [13 x i8] c"in child %u\0A\00"
@.str.1 = [9 x i8] c"setreuid\00"
@.str.2 = [22 x i8] c"setuid back to %d.%d\0A\00"
@O_RDWR = global i32 0
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [21 x i8] c"open lock file : %d\0A\00"
@LOCK_EX = global i32 0
@.str.5 = [6 x i8] c"flock\00"
@LOCK_UN = global i32 0
@.str.7 = [8 x i8] c"funlock\00"
@str = [10 x i8] c"lock file\00"
@str.9 = [11 x i8] c"uucp exit \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (...) @getpid()
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @print_ids()
br label %43
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 (...) @getuid()
%12 = tail call i32 (...) @geteuid()
%13 = tail call i32 (...) @print_ids()
%14 = tail call i32 @setreuid(i32 %12, i32 %11)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @err_sys(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %8
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %12, i32 %11)
%20 = tail call i32 (...) @print_ids()
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* %10, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = tail call i32 @err_sys(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%28 = load i32, i32* @LOCK_EX
%29 = tail call i32 @flock(i32 %22, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = tail call i32 @err_sys(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%35 = load i32, i32* @LOCK_UN
%36 = tail call i32 @flock(i32 %22, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = tail call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = tail call i32 @close(i32 %22)
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0))
br label %43
43: ; preds = %40, %6
%44 = phi i32 [ -1, %6 ], [ 0, %40 ]
ret i32 %44
}