#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 */
typedef int uid_t;
typedef int gid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getgid();
int getuid();
int /*<<< orphan*/ perror(char *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
int main(int argc, char *argv[]) {
  uid_t my_uid;
  gid_t my_gid;
  FILE *fp;
  my_uid = getuid();
  my_gid = getgid();
  if (NULL == (fp = fopen("/tmp/ostiary/ostcmds.log", "a+"))) {
    fprintf(stderr, "Error opening output file!\n");
    return -1;
  }
  stderr = fp;
  fprintf(fp, "I'm running as %d:%d.\n", my_uid, my_gid);
  if (!my_uid) {
    if (setuid(0)) {
      fprintf(fp, "Could not set uid to '0': ", errno);
      perror("");
    } else {
      fprintf(fp, "Uh oh, got root!\n", errno);
    }
  }
  fclose(fp);
  return 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)
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1060 <getgid@plt>
mov    %eax,-0x18(%rbp)
lea    0xe5f(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11e3 <main+0x73>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rdi
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <main+0x110>
mov    -0x20(%rbp),%rcx
mov    0x2df2(%rip),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1270 <main+0x100>
xor    %edi,%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
je     1250 <main+0xe0>
mov    -0x20(%rbp),%rdi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
lea    0xdf1(%rip),%rdi
call   1080 <perror@plt>
jmp    126b <main+0xfb>
mov    -0x20(%rbp),%rdi
mov    0x2d7d(%rip),%rax
mov    (%rax),%edx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1270 <main+0x100>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@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   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getgid@plt>
mov    %eax,%r15d
lea    0xe5f(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     120c <main+0x8c>
mov    %rax,%rbx
mov    0x2e24(%rip),%rax
mov    %rbx,(%rax)
lea    0xe72(%rip),%rsi
xor    %r14d,%r14d
mov    %rbx,%rdi
mov    %ebp,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %ebp,%ebp
jne    1202 <main+0x82>
xor    %edi,%edi
call   10a0 <setuid@plt>
mov    0x2dee(%rip),%rcx
mov    (%rcx),%edx
test   %rax,%rax
jne    1240 <main+0xc0>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1232 <main+0xb2>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rcx
lea    0xdff(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
jmp    1202 <main+0x82>

LLVM IR (-O0):
@.str = [25 x i8] c"/tmp/ostiary/ostcmds.log\00"
@.str.1 = [3 x i8] c"a+\00"
@stderr = global i32* null
@.str.2 = [28 x i8] c"Error opening output file!\0A\00"
@.str.3 = [23 x i8] c"I'm running as %d:%d.\0A\00"
@.str.4 = [27 x i8] c"Could not set uid to '0': \00"
@errno = global i32 0
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [18 x i8] c"Uh oh, got root!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @getuid()
store i32 %9, i32* %6
%10 = call i32 (...) @getgid()
store i32 %10, i32* %7
%11 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %8
%12 = icmp eq i32* null, %11
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32*, i32** @stderr
%15 = call i32 (i32*, i8*, ...) @fprintf(i32* %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
16: ; preds = %2
%17 = load i32*, i32** %8
store i32* %17, i32** @stderr
%18 = load i32*, i32** %8
%19 = load i32, i32* %6
%20 = load i32, i32* %7
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %37, label %24
24: ; preds = %16
%25 = call i64 @setuid(i32 0)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32*, i32** %8
%29 = load i32, i32* @errno
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
br label %36
32: ; preds = %24
%33 = load i32*, i32** %8
%34 = load i32, i32* @errno
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %27
br label %37
37: ; preds = %36, %16
%38 = load i32*, i32** %8
%39 = call i32 @fclose(i32* %38)
store i32 0, i32* %3
br label %40
40: ; preds = %37, %13
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [25 x i8] c"/tmp/ostiary/ostcmds.log\00"
@.str.1 = [3 x i8] c"a+\00"
@stderr = global i32* null
@.str.2 = [28 x i8] c"Error opening output file!\0A\00"
@.str.3 = [23 x i8] c"I'm running as %d:%d.\0A\00"
@.str.4 = [27 x i8] c"Could not set uid to '0': \00"
@errno = global i32 0
@.str.5 = [1 x i8] zeroinitializer
@.str.6 = [18 x i8] c"Uh oh, got root!\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getuid()
%4 = tail call i32 (...) @getgid()
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32*, i32** @stderr
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i64 27, i64 1, i32* %8)
br label %24
10: ; preds = %2
store i32* %5, i32** @stderr
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %3, i32 %4)
%12 = icmp eq i32 %3, 0
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = tail call i64 @setuid(i32 0)
%15 = icmp eq i64 %14, 0
%16 = load i32, i32* @errno
br i1 %15, label %20, label %17
17: ; preds = %13
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%19 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.5, i64 0, i64 0))
br label %22
20: ; preds = %13
%21 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 %16)
br label %22
22: ; preds = %17, %20, %10
%23 = tail call i32 @fclose(i32* %5)
br label %24
24: ; preds = %22, %7
%25 = phi i32 [ -1, %7 ], [ 0, %22 ]
ret i32 %25
}