#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 */
struct stat {
  scalar_t__ st_uid;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int errno;
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int stat(char *, struct stat *);
int get_uid(int pid) {
  char path[256] = {0};
  snprintf(path, sizeof(path), "/proc/%d", pid);
  struct stat st = {0};
  int res = stat(path, &st);
  if (res < 0) {
    return -errno;
  }
  return (int)st.st_uid;
}


x86 assembly (clang, -O0):
<get_uid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x100,%esi
lea    0xe99(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x110(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    11b8 <get_uid+0x88>
xor    %eax,%eax
mov    0x2e32(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11c2 <get_uid+0x92>
mov    -0x120(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_uid>:
push   %rbx
sub    $0x110,%rsp
mov    %edi,%ecx
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xe71(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1030 <snprintf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
js     11cc <get_uid+0xac>
mov    (%rsp),%eax
jmp    11d7 <get_uid+0xb7>
mov    0x2e0d(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x110,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@.str = [9 x i8] c"/proc/%d\00"
@errno = global i32 0
define i32 @get_uid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.stat
%6 = alloca i32
store i32 %0, i32* %3
%7 = bitcast [256 x i8]* %4 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @snprintf(i8* %8, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = bitcast %struct.stat* %5 to i8*
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = call i32 @stat(i8* %12, %struct.stat* %5)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
store i32 %18, i32* %2
br label %23
19: ; preds = %1
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %2
br label %23
23: ; preds = %19, %16
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@.str = [9 x i8] c"/proc/%d\00"
@errno = global i32 0
define i32 @get_uid(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 256, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = bitcast %struct.stat* %3 to i8*
%7 = call i32 @stat(i8* %4, %struct.stat* %3)
%8 = icmp slt i32 %7, 0
%9 = load i32, i32* @errno
%10 = sub nsw i32 0, %9
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%12 = load i64, i64* %11
%13 = trunc i64 %12 to i32
%14 = select i1 %8, i32 %10, i32 %13
ret i32 %14
}