#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int look_up_our_self() {
  int fd, pid, num_read;
  char buf[5];
  if ((fd = open("/proc/self/stat", O_RDONLY, 0)) < 0) {
    fprintf(stderr, "open: /proc/self/stat: %s\n", strerror(errno));
    return -1;
  }
  if ((num_read = read(fd, buf, 4)) < 4) {
    fprintf(stderr, "read: /proc/self/stat: %s\n", strerror(errno));
    return -1;
  }
  buf[4] = 0;
  sscanf(buf, "%d", &pid);
  close(fd);
  return pid;
}


x86 assembly (clang, -O0):
<look_up_our_self>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c3 <look_up_our_self+0x63>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <look_up_our_self+0xde>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x4,%eax
jge    1218 <look_up_our_self+0xb8>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe24(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123e <look_up_our_self+0xde>
movb   $0x0,-0x11(%rbp)
lea    -0x15(%rbp),%rdi
lea    0xe1f(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<look_up_our_self>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
xor    %edx,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11b8 <look_up_our_self+0x58>
mov    %eax,%ebx
lea    0x7(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x3,%eax
jg     11e7 <look_up_our_self+0x87>
mov    0x2e39(%rip),%rax
mov    (%rax),%ebx
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe75(%rip),%rsi
jmp    11d6 <look_up_our_self+0x76>
mov    0x2e19(%rip),%rax
mov    (%rax),%ebx
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3a(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    120d <look_up_our_self+0xad>
movb   $0x0,0xb(%rsp)
lea    0xe53(%rip),%rsi
lea    0x7(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1060 <sscanf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/stat\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"open: /proc/self/stat: %s\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"read: /proc/self/stat: %s\0A\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @look_up_our_self() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [5 x i8]
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 -1, i32* %1
br label %31
14: ; preds = %0
%15 = load i32, i32* %2
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%17 = call i32 @read(i32 %15, i8* %16, i32 4)
store i32 %17, i32* %4
%18 = icmp slt i32 %17, 4
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %22)
store i32 -1, i32* %1
br label %31
24: ; preds = %14
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 4
store i8 0, i8* %25
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %3
store i32 %30, i32* %1
br label %31
31: ; preds = %24, %19, %9
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/stat\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"open: /proc/self/stat: %s\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"read: /proc/self/stat: %s\0A\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @look_up_our_self() {
%1 = alloca i32
%2 = alloca [5 x i8]
%3 = bitcast i32* %1 to i8*
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %26
13: ; preds = %0
%14 = call i32 @read(i32 %6, i8* %4, i32 4)
%15 = icmp slt i32 %14, 4
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %26
21: ; preds = %13
%22 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 4
store i8 0, i8* %22
%23 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%24 = call i32 @close(i32 %6)
%25 = load i32, i32* %1
br label %26
26: ; preds = %21, %16, %8
%27 = phi i32 [ -1, %8 ], [ -1, %16 ], [ %25, %21 ]
ret i32 %27
}