#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 pid_t;
typedef int /*<<< orphan*/ file;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *get_proc_stat_field(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, unsigned long long *);
unsigned long long get_starttime(pid_t pid) {
  char file[20];
  snprintf(file, sizeof(file), "/proc/%d/stat", pid);
  char *tbuf = get_proc_stat_field(file, 21);
  if (tbuf == NULL)
    return -1;
  unsigned long long starttime = 0;
  sscanf(tbuf, "%llu", &starttime);
  free(tbuf);
  return starttime;
}


x86 assembly (clang, -O0):
<get_starttime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x14,%esi
lea    0xea2(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x20(%rbp),%rdi
mov    $0x15,%esi
call   1050 <get_proc_stat_field@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    118d <get_starttime+0x4d>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11ba <get_starttime+0x7a>
movq   $0x0,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe6e(%rip),%rsi
lea    -0x30(%rbp),%rdx
call   1060 <sscanf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_starttime>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ecx
lea    0xeb2(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    $0x15,%esi
call   1050 <get_proc_stat_field@plt>
test   %rax,%rax
je     11a1 <get_starttime+0x61>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
lea    0xe89(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1060 <sscanf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rax
jmp    11a8 <get_starttime+0x68>
mov    $0xffffffffffffffff,%rax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [5 x i8] c"%llu\00"
define i64 @get_starttime(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca i8*
%6 = alloca i64
store i32 %0, i32* %3
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = call i8* @get_proc_stat_field(i8* %10, i32 21)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %1
store i64 -1, i64* %2
br label %21
15: ; preds = %1
store i64 0, i64* %6
%16 = load i8*, i8** %5
%17 = call i32 @sscanf(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %6)
%18 = load i8*, i8** %5
%19 = call i32 @free(i8* %18)
%20 = load i64, i64* %6
store i64 %20, i64* %2
br label %21
21: ; preds = %15, %14
%22 = load i64, i64* %2
ret i64 %22
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@.str.1 = [5 x i8] c"%llu\00"
define i64 @get_starttime(i32 %0) {
%2 = alloca [20 x i8]
%3 = alloca i64
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i8* @get_proc_stat_field(i8* %4, i32 21)
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %1
%9 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%10 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64* %3)
%11 = call i32 @free(i8* %6)
%12 = load i64, i64* %3
br label %13
13: ; preds = %1, %8
%14 = phi i64 [ %12, %8 ], [ -1, %1 ]
ret i64 %14
}