#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 getgid();
int getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ setgid(int);
int /*<<< orphan*/ setuid(int);
int /*<<< orphan*/ system(char *);
int main(void) {
  int current_uid = getuid();
  int current_gid = getgid();
  printf("My UID is: %d. My GID is: %d\n", current_uid, getgid());
  system("/usr/bin/id");
  if (setuid(0)) {
    perror("setuid");
    return 1;
  }
  printf("My UID is: %d. My GID is: %d\n", getuid(), getgid());
  system("/usr/bin/id");
  setuid(current_uid);
  setgid(current_gid);
  printf("My UID is: %d. My GID is: %d\n", getuid(), getgid());
  system("/usr/bin/id");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1060 <getgid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <getgid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xe54(%rip),%rdi
call   1050 <printf@plt>
lea    0xe66(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1090 <setuid@plt>
cmp    $0x0,%eax
je     11e5 <main+0x75>
lea    0xe56(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1258 <main+0xe8>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getgid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
lea    0xe10(%rip),%rdi
call   1040 <system@plt>
mov    -0x8(%rbp),%edi
call   1090 <setuid@plt>
mov    -0xc(%rbp),%edi
call   1070 <setgid@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getgid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
lea    0xdd2(%rip),%rdi
call   1040 <system@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1060 <getgid@plt>
lea    0xe64(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <printf@plt>
lea    0xe72(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1090 <setuid@plt>
test   %eax,%eax
jne    122d <main+0xbd>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getgid@plt>
lea    0xe2d(%rip),%r12
mov    %r12,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <printf@plt>
lea    0xe38(%rip),%rbx
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %ebp,%edi
call   1090 <setuid@plt>
mov    %r15d,%edi
call   1070 <setgid@plt>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    1221 <main+0xb1>

LLVM IR (-O0):
@.str = [30 x i8] c"My UID is: %d. My GID is: %d\0A\00"
@.str.1 = [12 x i8] c"/usr/bin/id\00"
@.str.2 = [7 x i8] c"setuid\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (...) @getuid()
store i32 %4, i32* %2
%5 = call i32 (...) @getgid()
store i32 %5, i32* %3
%6 = load i32, i32* %2
%7 = call i32 (...) @getgid()
%8 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%10 = call i32 @setuid(i32 0)
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %27
14: ; preds = %0
%15 = call i32 (...) @getuid()
%16 = call i32 (...) @getgid()
%17 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
%18 = call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = call i32 @setuid(i32 %19)
%21 = load i32, i32* %3
%22 = call i32 @setgid(i32 %21)
%23 = call i32 (...) @getuid()
%24 = call i32 (...) @getgid()
%25 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %24)
%26 = call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %27
27: ; preds = %14, %12
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [30 x i8] c"My UID is: %d. My GID is: %d\0A\00"
@.str.1 = [12 x i8] c"/usr/bin/id\00"
@.str.2 = [7 x i8] c"setuid\00"
define i32 @main() {
%1 = tail call i32 (...) @getuid()
%2 = tail call i32 (...) @getgid()
%3 = tail call i32 (...) @getgid()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %3)
%5 = tail call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @setuid(i32 0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %21
10: ; preds = %0
%11 = tail call i32 (...) @getuid()
%12 = tail call i32 (...) @getgid()
%13 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = tail call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @setuid(i32 %1)
%16 = tail call i32 @setgid(i32 %2)
%17 = tail call i32 (...) @getuid()
%18 = tail call i32 (...) @getgid()
%19 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = tail call i32 @system(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %10, %8
%22 = phi i32 [ 1, %8 ], [ 0, %10 ]
ret i32 %22
}