#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*/ free(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
char *strtok(char *, char *);
int main(void) {
  int fd = open("/proc/self/stat", O_RDONLY);
  char *buf = calloc(100, sizeof(char));
  read(fd, buf, 100);
  int ntoken = 1;
  char *token = strtok(buf, " ");
  while (token) {
    ntoken++;
    token = strtok(NULL, " ");
    if (ntoken == 4) {
      printf("%s\n", token);
      break;
    }
  }
  free(buf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x64,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1222 <main+0xb2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe1b(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x4,-0x14(%rbp)
jne    121d <main+0xad>
mov    -0x20(%rbp),%rsi
lea    0xdff(%rip),%rdi
call   1040 <printf@plt>
jmp    1222 <main+0xb2>
jmp    11d6 <main+0x66>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    $0x64,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
lea    0xe5e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     1202 <main+0x92>
lea    0xe4a(%rip),%rsi
xor    %edi,%edi
call   1090 <strtok@plt>
test   %rax,%rax
je     1202 <main+0x92>
lea    0xe37(%rip),%rsi
xor    %edi,%edi
call   1090 <strtok@plt>
test   %rax,%rax
je     1202 <main+0x92>
lea    0xe24(%rip),%rsi
xor    %edi,%edi
call   1090 <strtok@plt>
lea    0xe18(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c" \00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i32 0, i32* %1
%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)
store i32 %7, i32* %2
%8 = call i8* @calloc(i32 100, i32 1)
store i8* %8, i8** %3
%9 = load i32, i32* %2
%10 = load i8*, i8** %3
%11 = call i32 @read(i32 %9, i8* %10, i32 100)
store i32 1, i32* %4
%12 = load i8*, i8** %3
%13 = call i8* @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %13, i8** %5
br label %14
14: ; preds = %26, %0
%15 = load i8*, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %27
17: ; preds = %14
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
%20 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %5
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 4
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i8*, i8** %5
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %27
26: ; preds = %17
br label %14
27: ; preds = %23, %14
%28 = load i8*, i8** %3
%29 = call i32 @free(i8* %28)
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/stat\00"
@O_RDONLY = global i32 0
@.str.1 = [2 x i8] c" \00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call dereferenceable_or_null(100) i8* @calloc(i32 100, i32 1)
%4 = tail call i32 @read(i32 %2, i8* %3, i32 100)
%5 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %16, label %7
7: ; preds = %0
%8 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %0, %7, %10, %13
%17 = tail call i32 @free(i8* %3)
%18 = tail call i32 @close(i32 %2)
ret i32 0
}