#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 atoi(char *);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setregid(int const, int const);
scalar_t__ setreuid(int const, int const);
scalar_t__ setuid(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 3)
    exit(-1);
  const int id = atoi(argv[1]);
  if (!getuid())
    if (setregid(id, id) || setregid(id, id))
      do {
        perror("setgid");
        exit(-1);
      } while (0);
    else if (setreuid(id, id) || setreuid(id, id))
      do {
        perror("setuid");
        exit(-1);
      } while (0);
  if (setuid(0) >= 0)
    do {
      perror("setuid(0)");
      exit(-1);
    } while (0);
  execvp(argv[2], argv + 2);
  do {
    perror("execvp");
    exit(-1);
  } while (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11aa <main+0x2a>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%eax
jne    125e <main+0xde>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <setregid@plt>
cmp    $0x0,%rax
jne    11f4 <main+0x74>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <setregid@plt>
cmp    $0x0,%rax
je     120f <main+0x8f>
jmp    11f9 <main+0x79>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <setreuid@plt>
cmp    $0x0,%rax
jne    1239 <main+0xb9>
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <setreuid@plt>
cmp    $0x0,%rax
je     1254 <main+0xd4>
jmp    123e <main+0xbe>
lea    0xdc2(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    1259 <main+0xd9>
jmp    125e <main+0xde>
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
jl     128a <main+0x10a>
jmp    1274 <main+0xf4>
lea    0xd93(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1070 <execvp@plt>
lea    0xd72(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     1192 <main+0x12>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <getuid@plt>
test   %eax,%eax
jne    11e3 <main+0x63>
mov    %ebp,%edi
mov    %ebp,%esi
call   1050 <setregid@plt>
test   %rax,%rax
jne    11f8 <main+0x78>
mov    %ebp,%edi
mov    %ebp,%esi
call   1050 <setregid@plt>
test   %rax,%rax
jne    11f8 <main+0x78>
mov    %ebp,%edi
mov    %ebp,%esi
call   1040 <setreuid@plt>
test   %rax,%rax
jne    121a <main+0x9a>
mov    %ebp,%edi
mov    %ebp,%esi
call   1040 <setreuid@plt>
test   %rax,%rax
jne    121a <main+0x9a>
xor    %edi,%edi
call   10a0 <setuid@plt>
test   %rax,%rax
js     1201 <main+0x81>
lea    0xe18(%rip),%rdi
jmp    1221 <main+0xa1>
lea    0xe01(%rip),%rdi
jmp    1221 <main+0xa1>
mov    0x10(%rbx),%rdi
add    $0x10,%rbx
mov    %rbx,%rsi
call   1070 <execvp@plt>
lea    0xe00(%rip),%rdi
jmp    1221 <main+0xa1>
lea    0xde6(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"setgid\00"
@.str.1 = [7 x i8] c"setuid\00"
@.str.2 = [10 x i8] c"setuid(0)\00"
@.str.3 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 3
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %6
%16 = call i32 (...) @getuid()
%17 = icmp ne i32 %16, 0
br i1 %17, label %50, label %18
18: ; preds = %11
%19 = load i32, i32* %6
%20 = load i32, i32* %6
%21 = call i64 @setregid(i32 %19, i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = load i32, i32* %6
%25 = load i32, i32* %6
%26 = call i64 @setregid(i32 %24, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23, %18
br label %29
29: ; preds = %28
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; No predecessors!
br label %49
33: ; preds = %23
%34 = load i32, i32* %6
%35 = load i32, i32* %6
%36 = call i64 @setreuid(i32 %34, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = load i32, i32* %6
%40 = load i32, i32* %6
%41 = call i64 @setreuid(i32 %39, i32 %40)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %38, %33
br label %44
44: ; preds = %43
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; No predecessors!
br label %48
48: ; preds = %47, %38
br label %49
49: ; preds = %48, %32
br label %50
50: ; preds = %49, %11
%51 = call i64 @setuid(i32 0)
%52 = icmp sge i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
br label %54
54: ; preds = %53
%55 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; No predecessors!
br label %58
58: ; preds = %57, %50
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 2
%61 = load i8*, i8** %60
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = call i32 @execvp(i8* %61, i8** %63)
br label %65
65: ; preds = %58
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%67 = call i32 @exit(i32 -1)
unreachable
68: ; No predecessors!
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [7 x i8] c"setgid\00"
@.str.1 = [7 x i8] c"setuid\00"
@.str.2 = [10 x i8] c"setuid(0)\00"
@.str.3 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i32 (...) @getuid()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %6
%13 = tail call i64 @setregid(i32 %9, i32 %9)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i64 @setregid(i32 %9, i32 %9)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %12, %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 -1)
unreachable
21: ; preds = %15
%22 = tail call i64 @setreuid(i32 %9, i32 %9)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = tail call i64 @setreuid(i32 %9, i32 %9)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %21, %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %24, %6
%31 = tail call i64 @setuid(i32 0)
%32 = icmp sgt i64 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i32 @exit(i32 -1)
unreachable
36: ; preds = %30
%37 = getelementptr inbounds i8*, i8** %1, i64 2
%38 = load i8*, i8** %37
%39 = tail call i32 @execvp(i8* %38, i8** %37)
%40 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 @exit(i32 -1)
unreachable
}