#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 readlink(char *, char *, int);
char *safe_calloc_impl(int, int, char *, int);
char *get_path() {
  char *path = safe_calloc_impl(
      500, sizeof(char),
      "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
      "2022-01-22/02-19-57/repos/fg123/wendy/refs/heads/master/src/execpath.c",
      51);
  int end = readlink("/proc/self/exe", path, 500);
  while (path[end] != '/' && path[end] != '\\') {
    end--;
  }
  path[end + 1] = 0;
  return path;
}


x86 assembly (clang, -O0):
<get_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x1f4,%edi
mov    $0x1,%esi
lea    0xec7(%rip),%rdx
mov    $0x33,%ecx
call   1040 <safe_calloc_impl@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xf35(%rip),%rdi
mov    $0x1f4,%edx
call   1030 <readlink@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0xd(%rbp)
je     118e <get_path+0x6e>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x5c,%eax
setne  %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    119e <get_path+0x7e>
jmp    11ac <get_path+0x8c>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    115f <get_path+0x3f>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_path>:
push   %r14
push   %rbx
push   %rax
lea    0xed5(%rip),%rdx
mov    $0x1f4,%edi
mov    $0x1,%esi
mov    $0x33,%ecx
call   1040 <safe_calloc_impl@plt>
mov    %rax,%r14
lea    0xf3e(%rip),%rdi
mov    %rax,%rsi
mov    $0x1f4,%edx
call   1030 <readlink@plt>
movslq %eax,%rdx
mov    %r14,%rax
add    %rdx,%rax
shl    $0x20,%rdx
movabs $0x100000000,%rcx
add    %rdx,%rcx
movabs $0xffffffff00000000,%rdx
ff ff ff
nopw   0x0(%rax,%rax,1)
movzbl (%rax),%ebx
cmp    $0x2f,%bl
je     1196 <get_path+0x76>
cmp    $0x5c,%bl
je     1196 <get_path+0x76>
add    %rdx,%rcx
add    $0xffffffffffffffff,%rax
jmp    1180 <get_path+0x60>
sar    $0x20,%rcx
movb   $0x0,(%r14,%rcx,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/fg123/wendy/refs/heads/master/src/execpath.c\00"
@.str.1 = [15 x i8] c"/proc/self/exe\00"
define i8* @get_path() {
%1 = alloca i8*
%2 = alloca i32
%3 = call i8* @safe_calloc_impl(i32 500, i32 1, i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str, i64 0, i64 0), i32 51)
store i8* %3, i8** %1
%4 = load i8*, i8** %1
%5 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 500)
store i32 %5, i32* %2
br label %6
6: ; preds = %24, %0
%7 = load i8*, i8** %1
%8 = load i32, i32* %2
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %7, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 47
br i1 %13, label %14, label %22
14: ; preds = %6
%15 = load i8*, i8** %1
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 92
br label %22
22: ; preds = %14, %6
%23 = phi i1 [ false, %6 ], [ %21, %14 ]
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = load i32, i32* %2
%26 = add nsw i32 %25, -1
store i32 %26, i32* %2
br label %6
27: ; preds = %22
%28 = load i8*, i8** %1
%29 = load i32, i32* %2
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %1
ret i8* %33
}

LLVM IR (-O2):
@.str = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/fg123/wendy/refs/heads/master/src/execpath.c\00"
@.str.1 = [15 x i8] c"/proc/self/exe\00"
define i8* @get_path() {
%1 = tail call i8* @safe_calloc_impl(i32 500, i32 1, i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str, i64 0, i64 0), i32 51)
%2 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 500)
%3 = sext i32 %2 to i64
br label %4
4: ; preds = %8, %0
%5 = phi i64 [ %9, %8 ], [ %3, %0 ]
%6 = getelementptr inbounds i8, i8* %1, i64 %5
%7 = load i8, i8* %6
switch i8 %7, label %8 [
i8 47, label %10
i8 92, label %10
]
8: ; preds = %4
%9 = add i64 %5, -1
br label %4
10: ; preds = %4, %4
%11 = shl i64 %5, 32
%12 = add i64 %11, 4294967296
%13 = ashr exact i64 %12, 32
%14 = getelementptr inbounds i8, i8* %1, i64 %13
store i8 0, i8* %14
ret i8* %1
}