#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 */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int stat(char *, struct stat *);
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)
    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    0xe93(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x60(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     1198 <get_uid_gid+0x58>
movl   $0x1,-0x4(%rbp)
jmp    11c0 <get_uid_gid+0x80>
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    0xea7(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1040 <snprintf@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    11a0 <get_uid_gid+0x60>
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

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@.str = [9 x i8] c"/proc/%d\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
store i64 %0, i64* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%12 = load i64, i64* %5
%13 = trunc i64 %12 to i32
%14 = call i32 @snprintf(i8* %11, i32 64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%16 = call i32 @stat(i8* %15, %struct.stat* %9)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 1, i32* %4
br label %31
20: ; preds = %3
%21 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 2
%22 = load i32, i32* %21
%23 = call i32 @S_ISDIR(i32 %22)
%24 = call i32 @assert(i32 %23)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%26 = load i32, i32* %25
%27 = load i32*, i32** %6
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32*, i32** %7
store i32 %29, i32* %30
store i32 0, i32* %4
br label %31
31: ; preds = %20, %19
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@.str = [9 x i8] c"/proc/%d\00"
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 %12, label %21
12: ; preds = %3
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 2
%14 = load i32, i32* %13
%15 = call i32 @S_ISDIR(i32 %14)
%16 = call i32 @assert(i32 %15)
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%18 = load i32, i32* %17
store i32 %18, i32* %1
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%20 = load i32, i32* %19
store i32 %20, i32* %2
br label %21
21: ; preds = %3, %12
%22 = phi i32 [ 0, %12 ], [ 1, %3 ]
ret i32 %22
}