#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*/ create_uucp(char const *);
int /*<<< orphan*/ err_sys(char *);
int flock(int, int /*<<< orphan*/);
int geteuid();
int getuid();
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ print_ids();
int /*<<< orphan*/ printf(char *, ...);
int setreuid(int, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: tip lockfile\n");
    print_ids();
    return -1;
  }
  int ret = 0;
  char const *lock = argv[1];
  int uid = getuid();
  int euid = geteuid();
  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");
  sleep(10);
  ret = setreuid(euid, uid);
  if (ret == -1)
    err_sys("setreuid");
  printf("setreuid back to %d.%d\n", euid, uid);
  print_ids();
  printf("do something...\n");
  create_uucp(lock);
  sleep(10);
  ret = setreuid(uid, euid);
  if (ret == -1)
    err_sys("setreuid");
  printf("setreuid to %d.%d\n", uid, euid);
  print_ids();
  ret = flock(fd, LOCK_UN);
  if (ret == -1)
    err_sys("funlock");
  close(fd);
  sleep(10);
  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)
cmpl   $0x2,-0x8(%rbp)
jge    11f1 <main+0x41>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <print_ids@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137f <main+0x1cf>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <print_ids@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    124a <main+0x9a>
lea    0xdd0(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x2c(%rbp),%esi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1285 <main+0xd5>
lea    0xdaf(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1090 <setreuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12c1 <main+0x111>
lea    0xd84(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <print_ids@plt>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <create_uucp@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1090 <setreuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1321 <main+0x171>
lea    0xd24(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <print_ids@plt>
mov    -0x2c(%rbp),%edi
mov    0x2c8a(%rip),%rax
mov    (%rax),%esi
call   10c0 <flock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1366 <main+0x1b6>
lea    0xd24(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jg     11ea <main+0x2a>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <print_ids@plt>
mov    $0xffffffff,%ebp
jmp    1334 <main+0x174>
mov    0x8(%rsi),%r15
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   1090 <print_ids@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10c0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    122d <main+0x6d>
lea    0xdd8(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdd1(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10d0 <flock@plt>
cmp    $0xffffffff,%eax
jne    125e <main+0x9e>
lea    0xdc1(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdf7(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    %r12d,%edi
mov    %r14d,%esi
call   10a0 <setreuid@plt>
cmp    $0xffffffff,%eax
jne    1290 <main+0xd0>
lea    0xd95(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xd92(%rip),%rdi
mov    %r12d,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <print_ids@plt>
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   10b0 <create_uucp@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    %r14d,%edi
mov    %r12d,%esi
call   10a0 <setreuid@plt>
cmp    $0xffffffff,%eax
jne    12e5 <main+0x125>
lea    0xd40(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xd55(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <print_ids@plt>
mov    0x2cc7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10d0 <flock@plt>
cmp    $0xffffffff,%eax
jne    1323 <main+0x163>
lea    0xd36(%rip),%rdi
call   1040 <err_sys@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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