#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int tv_sec;
  int tv_usec;
};
struct rusage {
  TYPE_1__ ru_utime;
};
typedef int clock_t;

/* Variables and functions */
int CLOCKS_PER_SEC;
int /*<<< orphan*/ EFAULT;
int /*<<< orphan*/ EINVAL;
int RUSAGE_CHILDREN;
int RUSAGE_SELF;
int clock();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct rusage *, int /*<<< orphan*/, int);
int getrusage(int _who, struct rusage *_rusage) {
  clock_t q;
  if (_rusage == 0) {
    errno = EFAULT;
    return -1;
  }
  if (_who != RUSAGE_SELF && _who != RUSAGE_CHILDREN) {
    errno = EINVAL;
    return -1;
  }
  memset(_rusage, 0, sizeof(struct rusage));
  q = clock();
  _rusage->ru_utime.tv_sec = q / CLOCKS_PER_SEC;
  _rusage->ru_utime.tv_usec = q % CLOCKS_PER_SEC * 1000000 / CLOCKS_PER_SEC;
  return 0;
}


x86 assembly (clang, -O0):
<getrusage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <getrusage+0x38>
mov    0x2eb7(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <getrusage+0xd6>
mov    -0x8(%rbp),%eax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%eax
je     119a <getrusage+0x7a>
mov    -0x8(%rbp),%eax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%eax
je     119a <getrusage+0x7a>
mov    0x2e35(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <getrusage+0xd6>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e22(%rip),%rcx
cltd
idivl  (%rcx)
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
mov    0x2e0d(%rip),%rcx
cltd
idivl  (%rcx)
imul   $0xf4240,%edx,%eax
mov    0x2dfd(%rip),%rcx
cltd
idivl  (%rcx)
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrusage>:
push   %rbp
push   %rbx
push   %rax
test   %rsi,%rsi
je     114a <getrusage+0x2a>
mov    %rsi,%rbx
mov    0x2e8e(%rip),%rax
cmp    %edi,(%rax)
je     1163 <getrusage+0x43>
mov    0x2e93(%rip),%rax
cmp    %edi,(%rax)
je     1163 <getrusage+0x43>
mov    0x2e70(%rip),%rax
jmp    1151 <getrusage+0x31>
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    0x2e6e(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    119c <getrusage+0x7c>
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
xor    %eax,%eax
call   1030 <clock@plt>
mov    %eax,%ecx
mov    0x2e5c(%rip),%rsi
cltd
idivl  (%rsi)
mov    %eax,(%rbx)
mov    (%rsi),%esi
mov    %ecx,%eax
cltd
idiv   %esi
imul   $0xf4240,%edx,%eax
cltd
idiv   %esi
mov    %eax,0x4(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.rusage = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32 }
@EFAULT = global i32 0
@errno = global i32 0
@RUSAGE_SELF = global i32 0
@RUSAGE_CHILDREN = global i32 0
@EINVAL = global i32 0
@CLOCKS_PER_SEC = global i32 0
define i32 @getrusage(i32 %0, %struct.rusage* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.rusage*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.rusage* %1, %struct.rusage** %5
%7 = load %struct.rusage*, %struct.rusage** %5
%8 = icmp eq %struct.rusage* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @EFAULT
store i32 %10, i32* @errno
store i32 -1, i32* %3
br label %40
11: ; preds = %2
%12 = load i32, i32* %4
%13 = load i32, i32* @RUSAGE_SELF
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = load i32, i32* %4
%17 = load i32, i32* @RUSAGE_CHILDREN
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i32, i32* @EINVAL
store i32 %20, i32* @errno
store i32 -1, i32* %3
br label %40
21: ; preds = %15, %11
%22 = load %struct.rusage*, %struct.rusage** %5
%23 = call i32 @memset(%struct.rusage* %22, i32 0, i32 8)
%24 = call i32 (...) @clock()
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = load i32, i32* @CLOCKS_PER_SEC
%27 = sdiv i32 %25, %26
%28 = load %struct.rusage*, %struct.rusage** %5
%29 = getelementptr inbounds %struct.rusage, %struct.rusage* %28, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %27, i32* %30
%31 = load i32, i32* %6
%32 = load i32, i32* @CLOCKS_PER_SEC
%33 = srem i32 %31, %32
%34 = mul nsw i32 %33, 1000000
%35 = load i32, i32* @CLOCKS_PER_SEC
%36 = sdiv i32 %34, %35
%37 = load %struct.rusage*, %struct.rusage** %5
%38 = getelementptr inbounds %struct.rusage, %struct.rusage* %37, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 1
store i32 %36, i32* %39
store i32 0, i32* %3
br label %40
40: ; preds = %21, %19, %9
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.rusage = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32 }
@EFAULT = global i32 0
@errno = global i32 0
@RUSAGE_SELF = global i32 0
@RUSAGE_CHILDREN = global i32 0
@EINVAL = global i32 0
@CLOCKS_PER_SEC = global i32 0
define i32 @getrusage(i32 %0, %struct.rusage* %1) {
%3 = icmp eq %struct.rusage* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32, i32* @EFAULT
store i32 %5, i32* @errno
br label %25
6: ; preds = %2
%7 = load i32, i32* @RUSAGE_SELF
%8 = icmp eq i32 %7, %0
%9 = load i32, i32* @RUSAGE_CHILDREN
%10 = icmp eq i32 %9, %0
%11 = select i1 %8, i1 true, i1 %10
br i1 %11, label %14, label %12
12: ; preds = %6
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
br label %25
14: ; preds = %6
%15 = tail call i32 @memset(%struct.rusage* %1, i32 0, i32 8)
%16 = tail call i32 (...) @clock()
%17 = load i32, i32* @CLOCKS_PER_SEC
%18 = sdiv i32 %16, %17
%19 = getelementptr inbounds %struct.rusage, %struct.rusage* %1, i64 0, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @CLOCKS_PER_SEC
%21 = srem i32 %16, %20
%22 = mul nsw i32 %21, 1000000
%23 = sdiv i32 %22, %20
%24 = getelementptr inbounds %struct.rusage, %struct.rusage* %1, i64 0, i32 0, i32 1
store i32 %23, i32* %24
br label %25
25: ; preds = %14, %12, %4
%26 = phi i32 [ -1, %4 ], [ -1, %12 ], [ 0, %14 ]
ret i32 %26
}