#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 setuid(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 = setuid(uid);
  if (ret == -1)
    err_sys("setuid");
  printf("setuid back to %d\n", uid);
  print_ids();
  printf("do something...\n");
  create_uucp(lock);
  sleep(10);
  ret = setuid(euid);
  if (ret == -1)
    err_sys("setuid");
  printf("setuid to %d\n", 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    1373 <main+0x1c3>
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   10a0 <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   10b0 <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    -0x24(%rbp),%edi
call   10c0 <setuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12be <main+0x10e>
lea    0xd87(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x24(%rbp),%esi
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <print_ids@plt>
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <create_uucp@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    -0x28(%rbp),%edi
call   10c0 <setuid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1318 <main+0x168>
lea    0xd2d(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0x28(%rbp),%esi
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <print_ids@plt>
mov    -0x2c(%rbp),%edi
mov    0x2c96(%rip),%rax
mov    (%rax),%esi
call   10b0 <flock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    135a <main+0x1aa>
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   %rbx
push   %rax
cmp    $0x1,%edi
jg     11e9 <main+0x29>
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <print_ids@plt>
mov    $0xffffffff,%ebp
jmp    1324 <main+0x164>
mov    0x8(%rsi),%r15
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1090 <print_ids@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10b0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    122b <main+0x6b>
lea    0xdda(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdd3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
jne    125c <main+0x9c>
lea    0xdc3(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    %ebp,%edi
call   10d0 <setuid@plt>
cmp    $0xffffffff,%eax
jne    128a <main+0xca>
lea    0xd9b(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xd96(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <print_ids@plt>
lea    0xdb2(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   10a0 <create_uucp@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    %r14d,%edi
call   10d0 <setuid@plt>
cmp    $0xffffffff,%eax
jne    12d8 <main+0x118>
lea    0xd4d(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xd5b(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1090 <print_ids@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
jne    1313 <main+0x153>
lea    0xd3a(%rip),%rdi
call   1040 <err_sys@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
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 = [7 x i8] c"setuid\00"
@.str.6 = [19 x i8] c"setuid back to %d\0A\00"
@.str.7 = [17 x i8] c"do something...\0A\00"
@.str.8 = [14 x i8] c"setuid to %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 %78
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* %8
%44 = call i32 @setuid(i32 %43)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %40
%50 = load i32, i32* %8
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %50)
%52 = call i32 (...) @print_ids()
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%54 = load i8*, i8** %7
%55 = call i32 @create_uucp(i8* %54)
%56 = call i32 @sleep(i32 10)
%57 = load i32, i32* %9
%58 = call i32 @setuid(i32 %57)
store i32 %58, i32* %6
%59 = load i32, i32* %6
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %49
%62 = call i32 @err_sys(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %63
63: ; preds = %61, %49
%64 = load i32, i32* %9
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), i32 %64)
%66 = call i32 (...) @print_ids()
%67 = load i32, i32* %10
%68 = load i32, i32* @LOCK_UN
%69 = call i32 @flock(i32 %67, i32 %68)
store i32 %69, i32* %6
%70 = load i32, i32* %6
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %74
72: ; preds = %63
%73 = call i32 @err_sys(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %74
74: ; preds = %72, %63
%75 = load i32, i32* %10
%76 = call i32 @close(i32 %75)
%77 = call i32 @sleep(i32 10)
store i32 0, i32* %3
br label %78
78: ; preds = %74, %13
%79 = load i32, i32* %3
ret i32 %79
}

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 = [7 x i8] c"setuid\00"
@.str.6 = [19 x i8] c"setuid back to %d\0A\00"
@.str.8 = [14 x i8] c"setuid to %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 @setuid(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 ([7 x i8], [7 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 ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), 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 @setuid(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 ([7 x i8], [7 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 ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0), 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
}