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

/* Variables and functions */
int asprintf(char **, char *, int /*<<< orphan*/);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ strncmp(char *, char *, int);
uid_t pid_get_uid(pid_t pid) {
  uid_t rv = 0;
  char *file;
  if (asprintf(&file, "/proc/%u/status", pid) == -1) {
    perror("asprintf");
    exit(1);
  }
  FILE *fp = fopen(file, "r");
  if (!fp) {
    free(file);
    return 0;
  }
  char buf[4096];
  while (fgets(buf, 4096 - 1, fp)) {
    if (strncmp(buf, "Uid:", 4) == 0) {
      char *ptr = buf + 5;
      while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) {
        ptr++;
      }
      if (*ptr == '\0')
        goto doexit;
      rv = atoi(ptr);
      break;
    }
  }
doexit:
  fclose(fp);
  free(file);
  return rv;
}


x86 assembly (clang, -O0):
<pid_get_uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11d7 <pid_get_uid+0x47>
lea    0xe48(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120b <pid_get_uid+0x7b>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1333 <pid_get_uid+0x1a3>
jmp    1210 <pid_get_uid+0x80>
lea    -0x1020(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0xfff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1316 <pid_get_uid+0x186>
lea    -0x1020(%rbp),%rdi
lea    0xdde(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1311 <pid_get_uid+0x181>
lea    -0x1020(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x1028(%rbp)
mov    -0x1028(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x1029(%rbp)
je     12bb <pid_get_uid+0x12b>
mov    -0x1028(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x1,%al
cmp    $0x20,%ecx
mov    %al,-0x102a(%rbp)
je     12af <pid_get_uid+0x11f>
mov    -0x1028(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x9,%eax
sete   %al
mov    %al,-0x102a(%rbp)
mov    -0x102a(%rbp),%al
mov    %al,-0x1029(%rbp)
mov    -0x1029(%rbp),%al
test   $0x1,%al
jne    12ce <pid_get_uid+0x13e>
jmp    12e5 <pid_get_uid+0x155>
mov    -0x1028(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1028(%rbp)
jmp    1263 <pid_get_uid+0xd3>
mov    -0x1028(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12fd <pid_get_uid+0x16d>
jmp    131b <pid_get_uid+0x18b>
mov    -0x1028(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0xc(%rbp)
jmp    1316 <pid_get_uid+0x186>
jmp    1210 <pid_get_uid+0x80>
jmp    131b <pid_get_uid+0x18b>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pid_get_uid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%edx
lea    0xe5a(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
je     127a <pid_get_uid+0xea>
mov    0x8(%rsp),%rdi
lea    0xe54(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     125e <pid_get_uid+0xce>
mov    %rax,%r14
lea    0x10(%rsp),%rbx
lea    0xe39(%rip),%r15
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0xfff,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     124a <pid_get_uid+0xba>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11f0 <pid_get_uid+0x60>
lea    0x15(%rsp),%rdi
jmp    1234 <pid_get_uid+0xa4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rdi
movzbl (%rdi),%eax
cmp    $0x9,%al
je     1230 <pid_get_uid+0xa0>
cmp    $0x20,%al
je     1230 <pid_get_uid+0xa0>
test   %al,%al
je     124a <pid_get_uid+0xba>
call   10a0 <atoi@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    126a <pid_get_uid+0xda>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%u/status\00"
@.str.1 = [9 x i8] c"asprintf\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [5 x i8] c"Uid:\00"
define i32 @pid_get_uid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [4096 x i8]
%8 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %4
%9 = load i32, i32* %3
%10 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i8*, i8** %5
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = load i8*, i8** %5
%22 = call i32 @free(i8* %21)
store i32 0, i32* %2
br label %75
23: ; preds = %15
br label %24
24: ; preds = %67, %23
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%26 = load i32*, i32** %6
%27 = call i64 @fgets(i8* %25, i32 4095, i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %68
29: ; preds = %24
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = call i64 @strncmp(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %67
33: ; preds = %29
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%35 = getelementptr inbounds i8, i8* %34, i64 5
store i8* %35, i8** %8
br label %36
36: ; preds = %55, %33
%37 = load i8*, i8** %8
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %36
%42 = load i8*, i8** %8
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 32
br i1 %45, label %51, label %46
46: ; preds = %41
%47 = load i8*, i8** %8
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 9
br label %51
51: ; preds = %46, %41
%52 = phi i1 [ true, %41 ], [ %50, %46 ]
br label %53
53: ; preds = %51, %36
%54 = phi i1 [ false, %36 ], [ %52, %51 ]
br i1 %54, label %55, label %58
55: ; preds = %53
%56 = load i8*, i8** %8
%57 = getelementptr inbounds i8, i8* %56, i32 1
store i8* %57, i8** %8
br label %36
58: ; preds = %53
%59 = load i8*, i8** %8
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %58
br label %69
64: ; preds = %58
%65 = load i8*, i8** %8
%66 = call i32 @atoi(i8* %65)
store i32 %66, i32* %4
br label %68
67: ; preds = %29
br label %24
68: ; preds = %64, %24
br label %69
69: ; preds = %68, %63
%70 = load i32*, i32** %6
%71 = call i32 @fclose(i32* %70)
%72 = load i8*, i8** %5
%73 = call i32 @free(i8* %72)
%74 = load i32, i32* %4
store i32 %74, i32* %2
br label %75
75: ; preds = %69, %20
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%u/status\00"
@.str.1 = [9 x i8] c"asprintf\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [5 x i8] c"Uid:\00"
define i32 @pid_get_uid(i32 %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = bitcast i8** %2 to i8*
%5 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i8*, i8** %2
%16 = call i32 @free(i8* %15)
br label %39
17: ; preds = %10
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
br label %19
19: ; preds = %22, %17
%20 = call i64 @fgets(i8* %18, i32 4095, i32* %12)
%21 = icmp eq i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %19
%23 = call i64 @strncmp(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %19
25: ; preds = %22
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 5
br label %27
27: ; preds = %30, %25
%28 = phi i8* [ %26, %25 ], [ %31, %30 ]
%29 = load i8, i8* %28
switch i8 %29, label %32 [
i8 9, label %30
i8 32, label %30
i8 0, label %34
]
30: ; preds = %27, %27
%31 = getelementptr inbounds i8, i8* %28, i64 1
br label %27
32: ; preds = %27
%33 = call i32 @atoi(i8* %28)
br label %34
34: ; preds = %19, %27, %32
%35 = phi i32 [ %33, %32 ], [ 0, %27 ], [ 0, %19 ]
%36 = call i32 @fclose(i32* %12)
%37 = load i8*, i8** %2
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %34, %14
%40 = phi i32 [ %35, %34 ], [ 0, %14 ]
ret i32 %40
}