#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;
struct stat {
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mode;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ gid_t;
typedef int /*<<< orphan*/ dir_name;

/* Variables and functions */
#define EACCES 136
#define EBADF 135
#define EFAULT 134
#define ELOOP 133
#define ENAMETOOLONG 132
#define ENOENT 131
#define ENOMEM 130
#define ENOTDIR 129
#define EOVERFLOW 128
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int get_uid_gid(pid_t pid, uid_t *uid, gid_t *gid) {
  char dir_name[64];
  struct stat dir_info;
  int rval;
  snprintf(dir_name, sizeof(dir_name), "/proc/%d", (int)pid);
  rval = stat(dir_name, &dir_info);
  if (rval != 0) {
    int saveerror = errno;
    switch (saveerror) {
    case EACCES:
      fprintf(stderr, "EACCES\n");
      break;
    case EBADF:
      fprintf(stderr, "EBADF\n");
      break;
    case EFAULT:
      fprintf(stderr, "EFAULT\n");
      break;
    case ELOOP:
      fprintf(stderr, "ELOOP\n");
      break;
    case ENAMETOOLONG:
      fprintf(stderr, "ENAMETOOLONG\n");
      break;
    case ENOENT:
      fprintf(stderr, "ENOENT\n");
      break;
    case ENOMEM:
      fprintf(stderr, "ENOMEM\n");
      break;
    case ENOTDIR:
      fprintf(stderr, "ENOTDIR\n");
      break;
    case EOVERFLOW:
      fprintf(stderr, "EOVERFLOW\n");
      break;
    }
    perror("stat:");
    fprintf(stderr, "stat error\n");
    return 1;
  }
  assert(S_ISDIR(dir_info.st_mode));
  *uid = dir_info.st_uid;
  *gid = dir_info.st_gid;
  return 0;
}


x86 assembly (clang, -O0):
<get_uid_gid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xe97(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     12f3 <get_uid_gid+0x193>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    -0x78(%rbp),%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x80(%rbp)
sub    $0x8,%eax
ja     12c6 <get_uid_gid+0x166>
mov    -0x80(%rbp),%rax
lea    0xe28(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12c6 <get_uid_gid+0x166>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1060 <fprintf@plt>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131b <get_uid_gid+0x1bb>
mov    -0x68(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x6c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x70(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_uid_gid>:
push   %r15
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rcx
lea    0xe87(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %eax,%eax
jne    11c3 <get_uid_gid+0x63>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    0x4(%rsp),%eax
mov    %eax,(%r15)
mov    (%rsp),%eax
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x8,%eax
ja     11f2 <get_uid_gid+0x92>
cltq
lea    0xe8f(%rip),%rcx
movslq (%rcx,%rax,4),%rsi
add    %rcx,%rsi
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1060 <fprintf@plt>
lea    0xe60(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    11b9 <get_uid_gid+0x59>

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@.str = [9 x i8] c"/proc/%d\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [8 x i8] c"EACCES\0A\00"
@.str.2 = [7 x i8] c"EBADF\0A\00"
@.str.3 = [8 x i8] c"EFAULT\0A\00"
@.str.4 = [7 x i8] c"ELOOP\0A\00"
@.str.5 = [14 x i8] c"ENAMETOOLONG\0A\00"
@.str.6 = [8 x i8] c"ENOENT\0A\00"
@.str.7 = [8 x i8] c"ENOMEM\0A\00"
@.str.8 = [9 x i8] c"ENOTDIR\0A\00"
@.str.9 = [11 x i8] c"EOVERFLOW\0A\00"
@.str.10 = [6 x i8] c"stat:\00"
@.str.11 = [12 x i8] c"stat error\0A\00"
define i32 @get_uid_gid(i64 %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [64 x i8]
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
store i64 %0, i64* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%12 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%13 = load i64, i64* %5
%14 = trunc i64 %13 to i32
%15 = call i32 @snprintf(i8* %12, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%17 = call i32 @stat(i8* %16, %struct.stat* %9)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %54
20: ; preds = %3
%21 = load i32, i32* @errno
store i32 %21, i32* %11
%22 = load i32, i32* %11
switch i32 %22, label %50 [
i32 136, label %23
i32 135, label %26
i32 134, label %29
i32 133, label %32
i32 132, label %35
i32 131, label %38
i32 130, label %41
i32 129, label %44
i32 128, label %47
]
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %50
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %50
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %50
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %50
35: ; preds = %20
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %50
38: ; preds = %20
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %50
41: ; preds = %20
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %50
44: ; preds = %20
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
br label %50
47: ; preds = %20
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
br label %50
50: ; preds = %20, %47, %44, %41, %38, %35, %32, %29, %26, %23
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0))
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
store i32 1, i32* %4
br label %65
54: ; preds = %3
%55 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%56 = load i32, i32* %55
%57 = call i32 @S_ISDIR(i32 %56)
%58 = call i32 @assert(i32 %57)
%59 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%60 = load i32, i32* %59
%61 = load i32*, i32** %6
store i32 %60, i32* %61
%62 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%63 = load i32, i32* %62
%64 = load i32*, i32** %7
store i32 %63, i32* %64
store i32 0, i32* %4
br label %65
65: ; preds = %54, %50
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@.str = [9 x i8] c"/proc/%d\00"
@errno = global i32 0
@stderr = global i32 0
@.str.1 = [8 x i8] c"EACCES\0A\00"
@.str.2 = [7 x i8] c"EBADF\0A\00"
@.str.3 = [8 x i8] c"EFAULT\0A\00"
@.str.4 = [7 x i8] c"ELOOP\0A\00"
@.str.5 = [14 x i8] c"ENAMETOOLONG\0A\00"
@.str.6 = [8 x i8] c"ENOENT\0A\00"
@.str.7 = [8 x i8] c"ENOMEM\0A\00"
@.str.8 = [9 x i8] c"ENOTDIR\0A\00"
@.str.9 = [11 x i8] c"EOVERFLOW\0A\00"
@.str.10 = [6 x i8] c"stat:\00"
@.str.11 = [12 x i8] c"stat error\0A\00"
@reltable.get_uid_gid = [9 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([11 x i8]* @.str.9 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.8 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str.7 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str.6 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([14 x i8]* @.str.5 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.4 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str.3 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([7 x i8]* @.str.2 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([8 x i8]* @.str.1 to i64), i64 ptrtoint ([9 x i32]* @reltable.get_uid_gid to i64)) to i32)]
define i32 @get_uid_gid(i64 %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca [64 x i8]
%5 = alloca %struct.stat
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.stat* %5 to i8*
%8 = trunc i64 %0 to i32
%9 = call i32 @snprintf(i8* %6, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 @stat(i8* %6, %struct.stat* %5)
%11 = icmp eq i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = add i32 %13, -128
%15 = icmp ult i32 %14, 9
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = sext i32 %14 to i64
%18 = shl i64 %17, 2
%19 = call i8* @llvm.load.relative.i64(i8* bitcast ([9 x i32]* @reltable.get_uid_gid to i8*), i64 %18)
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* %19)
br label %22
22: ; preds = %12, %16
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.11, i64 0, i64 0))
br label %35
26: ; preds = %3
%27 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @S_ISDIR(i32 %28)
%30 = call i32 @assert(i32 %29)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%32 = load i32, i32* %31
store i32 %32, i32* %1
%33 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%34 = load i32, i32* %33
store i32 %34, i32* %2
br label %35
35: ; preds = %26, %22
%36 = phi i32 [ 1, %22 ], [ 0, %26 ]
ret i32 %36
}