#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 ssize_t;

/* Variables and functions */
int EINVAL;
int errno;
int readlink(char *, char *, size_t);
int s_get_own_path(char *buf, size_t bufsiz) {
  ssize_t l;
  if (bufsiz == 0)
    return -EINVAL;
  l = readlink("/proc/self/exe", buf, bufsiz - 1);
  if (l == -1)
    return -errno;
  buf[l] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<s_get_own_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    113e <s_get_own_path+0x2e>
xor    %eax,%eax
mov    0x2ea4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1189 <s_get_own_path+0x79>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
lea    0xeaf(%rip),%rdi
call   1030 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1176 <s_get_own_path+0x66>
xor    %eax,%eax
mov    0x2e74(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1189 <s_get_own_path+0x79>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_get_own_path>:
push   %rbx
test   %rsi,%rsi
je     113e <s_get_own_path+0x2e>
mov    %rsi,%rdx
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rdx
lea    0xed9(%rip),%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     1147 <s_get_own_path+0x37>
cltq
movb   $0x0,(%rbx,%rax,1)
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e93(%rip),%rcx
jmp    114e <s_get_own_path+0x3e>
mov    0x2e92(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@errno = global i32 0
define i32 @s_get_own_path(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @EINVAL
%11 = sub nsw i32 0, %10
store i32 %11, i32* %3
br label %27
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i64, i64* %5
%15 = sub i64 %14, 1
%16 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %13, i64 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %27
22: ; preds = %12
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
store i32 0, i32* %3
br label %27
27: ; preds = %22, %19, %9
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@EINVAL = global i32 0
@.str = [15 x i8] c"/proc/self/exe\00"
@errno = global i32 0
define i32 @s_get_own_path(i8* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @EINVAL
%6 = sub nsw i32 0, %5
br label %17
7: ; preds = %2
%8 = add i64 %1, -1
%9 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %17
14: ; preds = %7
%15 = sext i32 %9 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %14, %11, %4
%18 = phi i32 [ %6, %4 ], [ %13, %11 ], [ 0, %14 ]
ret i32 %18
}