#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;
char *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int getInitPid(void) {
  int fd = 0, pid = -1, ret;
  char *buf = calloc(1, 10);
  fd = open("/var/run/init.pid", O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "Unable to find pid of init!!!\n");
    return -1;
  }
  ret = read(fd, buf, 9);
  close(fd);
  ret = sscanf(buf, "%d", &pid);
  return pid;
}


x86 assembly (clang, -O0):
<getInitPid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    $0x1,%edi
mov    $0xa,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11cc <getInitPid+0x6c>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <getInitPid+0xa5>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x9,%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rdi
lea    0xe3e(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1070 <sscanf@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getInitPid>:
push   %rbp
push   %rbx
push   %rax
movl   $0xffffffff,0x4(%rsp)
ff
mov    $0x1,%edi
mov    $0xa,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11c8 <getInitPid+0x68>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x9,%edx
call   1040 <read@plt>
mov    %ebp,%edi
call   1030 <close@plt>
lea    0xe7c(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <sscanf@plt>
mov    0x4(%rsp),%eax
jmp    11e2 <getInitPid+0x82>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/var/run/init.pid\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Unable to find pid of init!!!\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @getInitPid() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %2
store i32 -1, i32* %3
%6 = call i8* @calloc(i32 1, i32 10)
store i8* %6, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %23
14: ; preds = %0
%15 = load i32, i32* %2
%16 = load i8*, i8** %5
%17 = call i32 @read(i32 %15, i8* %16, i32 9)
store i32 %17, i32* %4
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
%20 = load i8*, i8** %5
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %3)
store i32 %21, i32* %4
%22 = load i32, i32* %3
store i32 %22, i32* %1
br label %23
23: ; preds = %14, %11
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [18 x i8] c"/var/run/init.pid\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [31 x i8] c"Unable to find pid of init!!!\0A\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @getInitPid() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 -1, i32* %1
%3 = tail call dereferenceable_or_null(10) i8* @calloc(i32 1, i32 10)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %15
10: ; preds = %0
%11 = tail call i32 @read(i32 %5, i8* %3, i32 9)
%12 = tail call i32 @close(i32 %5)
%13 = call i32 @sscanf(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%14 = load i32, i32* %1
br label %15
15: ; preds = %10, %7
%16 = phi i32 [ -1, %7 ], [ %14, %10 ]
ret i32 %16
}