#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;

/* Variables and functions */
int /*<<< orphan*/ getpwuid(int);
scalar_t__ getuid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setuid(int);
int reset_uid(void) {
  uid_t uid;
  if (getuid())
    return 0;
  for (uid = 0; uid < 10000; uid++) {
    if (!getpwuid(uid) && !setuid(uid))
      return 0;
  }
  printf("Failed: No unused uid's in %d attempts\n", 10000);
  return -1;
}


x86 assembly (clang, -O0):
<reset_uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
je     1165 <reset_uid+0x25>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <reset_uid+0x92>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2710,-0x8(%rbp)
jge    11ba <reset_uid+0x7a>
mov    -0x8(%rbp),%edi
call   1030 <getpwuid@plt>
cmp    $0x0,%eax
jne    11a7 <reset_uid+0x67>
mov    -0x8(%rbp),%edi
call   1060 <setuid@plt>
cmp    $0x0,%eax
jne    11a7 <reset_uid+0x67>
movl   $0x0,-0x4(%rbp)
jmp    11d2 <reset_uid+0x92>
jmp    11ac <reset_uid+0x6c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    116c <reset_uid+0x2c>
lea    0xe3f(%rip),%rdi
mov    $0x2710,%esi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reset_uid>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
jne    119b <reset_uid+0x5b>
xor    %ebx,%ebx
jmp    116b <reset_uid+0x2b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebx
cmp    $0x2710,%ebx
je     1185 <reset_uid+0x45>
mov    %ebx,%edi
call   1030 <getpwuid@plt>
test   %eax,%eax
jne    1160 <reset_uid+0x20>
mov    %ebx,%edi
call   1060 <setuid@plt>
test   %eax,%eax
jne    1160 <reset_uid+0x20>
xor    %ebx,%ebx
jmp    119b <reset_uid+0x5b>
lea    0xe74(%rip),%rdi
mov    $0x2710,%esi
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Failed: No unused uid's in %d attempts\0A\00"
define i32 @reset_uid() {
%1 = alloca i32
%2 = alloca i32
%3 = call i64 (...) @getuid()
%4 = icmp ne i64 %3, 0
br i1 %4, label %5, label %6
5: ; preds = %0
store i32 0, i32* %1
br label %25
6: ; preds = %0
store i32 0, i32* %2
br label %7
7: ; preds = %20, %6
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 10000
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = load i32, i32* %2
%12 = call i32 @getpwuid(i32 %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = load i32, i32* %2
%16 = call i32 @setuid(i32 %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %14
store i32 0, i32* %1
br label %25
19: ; preds = %14, %10
br label %20
20: ; preds = %19
%21 = load i32, i32* %2
%22 = add nsw i32 %21, 1
store i32 %22, i32* %2
br label %7
23: ; preds = %7
%24 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 10000)
store i32 -1, i32* %1
br label %25
25: ; preds = %23, %18, %5
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [40 x i8] c"Failed: No unused uid's in %d attempts\0A\00"
define i32 @reset_uid() {
%1 = tail call i64 (...) @getuid()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %15
3: ; preds = %0, %10
%4 = phi i32 [ %11, %10 ], [ 0, %0 ]
%5 = tail call i32 @getpwuid(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @setuid(i32 %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %3, %7
%11 = add nuw nsw i32 %4, 1
%12 = icmp eq i32 %11, 10000
br i1 %12, label %13, label %3
13: ; preds = %10
%14 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 10000)
br label %15
15: ; preds = %7, %0, %13
%16 = phi i32 [ -1, %13 ], [ 0, %0 ], [ 0, %7 ]
ret i32 %16
}