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

/* Variables and functions */
scalar_t__ ENOMEM;
scalar_t__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
size_t readlink(char *, char *, size_t);
char *realloc(char *, size_t);
int strlen(char *);
char *sgsXPC_GetModuleFileName() {
  char stack_buf[4096 + 1];
  char *buf;
  size_t cur_size = 4096, max_size = 4096 * 10;
  errno = 0;
  buf = stack_buf;
  for (;;) {
    ssize_t res = readlink("/proc/self/exe", buf, cur_size);
    if (res >= 0 && res < cur_size) {
      buf[res] = 0;
      if (buf == stack_buf) {
        size_t len = strlen(buf) + 1;
        buf = (char *)malloc(len);
        memcpy(buf, stack_buf, len);
      }
      return buf;
    }
    if (errno != ERANGE || cur_size >= max_size) {
      if (buf != stack_buf)
        free(buf);
      return NULL;
    }
    cur_size *= 2;
    if (buf == stack_buf)
      buf = malloc(cur_size);
    else
      buf = realloc(buf, cur_size);
    if (!buf) {
      errno = ENOMEM;
      return NULL;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<sgsXPC_GetModuleFileName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movq   $0x1000,-0x1020(%rbp)
movq   $0xa000,-0x1028(%rbp)
mov    0x2e48(%rip),%rax
movq   $0x0,(%rax)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rsi
mov    -0x1020(%rbp),%rdx
lea    0xe4e(%rip),%rdi
call   1040 <readlink@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
jb     125e <sgsXPC_GetModuleFileName+0xfe>
mov    -0x1030(%rbp),%rax
cmp    -0x1020(%rbp),%rax
jae    125e <sgsXPC_GetModuleFileName+0xfe>
mov    -0x1018(%rbp),%rax
mov    -0x1030(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
cmp    %rcx,%rax
jne    124e <sgsXPC_GetModuleFileName+0xee>
mov    -0x1018(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x1038(%rbp)
mov    -0x1038(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    -0x1038(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x1018(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    134b <sgsXPC_GetModuleFileName+0x1eb>
mov    0x2d6b(%rip),%rax
mov    (%rax),%rax
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%rax
jne    128c <sgsXPC_GetModuleFileName+0x12c>
mov    -0x1020(%rbp),%rax
cmp    -0x1028(%rbp),%rax
jb     12bc <sgsXPC_GetModuleFileName+0x15c>
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
cmp    %rcx,%rax
je     12af <sgsXPC_GetModuleFileName+0x14f>
mov    -0x1018(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    134b <sgsXPC_GetModuleFileName+0x1eb>
mov    -0x1020(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
cmp    %rcx,%rax
jne    12fd <sgsXPC_GetModuleFileName+0x19d>
mov    -0x1020(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x1018(%rbp)
jmp    1317 <sgsXPC_GetModuleFileName+0x1b7>
mov    -0x1018(%rbp),%rdi
mov    -0x1020(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    1346 <sgsXPC_GetModuleFileName+0x1e6>
mov    0x2cac(%rip),%rax
mov    (%rax),%rcx
mov    0x2c9a(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    134b <sgsXPC_GetModuleFileName+0x1eb>
jmp    119d <sgsXPC_GetModuleFileName+0x3d>
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sgsXPC_GetModuleFileName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rsp,%r13
mov    0x2e55(%rip),%r12
movq   $0x0,(%r12)
mov    $0x1000,%ebx
lea    0xe71(%rip),%r15
mov    0x2e52(%rip),%rbp
mov    %r13,%r14
jmp    11b3 <sgsXPC_GetModuleFileName+0x53>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1224 <sgsXPC_GetModuleFileName+0xc4>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
cmp    %rbx,%rax
jb     11eb <sgsXPC_GetModuleFileName+0x8b>
cmp    $0x9fff,%rbx
ja     1234 <sgsXPC_GetModuleFileName+0xd4>
mov    0x0(%rbp),%rax
cmp    %rax,(%r12)
jne    1234 <sgsXPC_GetModuleFileName+0xd4>
add    %rbx,%rbx
cmp    %r13,%r14
jne    11a0 <sgsXPC_GetModuleFileName+0x40>
mov    %rbx,%rdi
call   1070 <malloc@plt>
jmp    11ab <sgsXPC_GetModuleFileName+0x4b>
movb   $0x0,(%r14,%rax,1)
mov    %rsp,%rax
cmp    %rax,%r14
jne    1247 <sgsXPC_GetModuleFileName+0xe7>
mov    %rsp,%r15
mov    %r15,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
jmp    1247 <sgsXPC_GetModuleFileName+0xe7>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
jmp    1244 <sgsXPC_GetModuleFileName+0xe4>
mov    %rsp,%rax
cmp    %rax,%r14
je     1244 <sgsXPC_GetModuleFileName+0xe4>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@.str = [15 x i8] c"/proc/self/exe\00"
@ERANGE = global i64 0
@ENOMEM = global i64 0
define i8* @sgsXPC_GetModuleFileName() {
%1 = alloca i8*
%2 = alloca [4097 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
store i64 4096, i64* %4
store i64 40960, i64* %5
store i64 0, i64* @errno
%8 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
store i8* %8, i8** %3
br label %9
9: ; preds = %73, %0
%10 = load i8*, i8** %3
%11 = load i64, i64* %4
%12 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %10, i64 %11)
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = icmp uge i64 %13, 0
br i1 %14, label %15, label %39
15: ; preds = %9
%16 = load i64, i64* %6
%17 = load i64, i64* %4
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %39
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = load i64, i64* %6
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8 0, i8* %22
%23 = load i8*, i8** %3
%24 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %37
26: ; preds = %19
%27 = load i8*, i8** %3
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %28, 1
%30 = sext i32 %29 to i64
store i64 %30, i64* %7
%31 = load i64, i64* %7
%32 = call i8* @malloc(i64 %31)
store i8* %32, i8** %3
%33 = load i8*, i8** %3
%34 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%35 = load i64, i64* %7
%36 = call i32 @memcpy(i8* %33, i8* %34, i64 %35)
br label %37
37: ; preds = %26, %19
%38 = load i8*, i8** %3
store i8* %38, i8** %1
br label %74
39: ; preds = %15, %9
%40 = load i64, i64* @errno
%41 = load i64, i64* @ERANGE
%42 = icmp ne i64 %40, %41
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = load i64, i64* %4
%45 = load i64, i64* %5
%46 = icmp uge i64 %44, %45
br i1 %46, label %47, label %55
47: ; preds = %43, %39
%48 = load i8*, i8** %3
%49 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%50 = icmp ne i8* %48, %49
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i8*, i8** %3
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %51, %47
store i8* null, i8** %1
br label %74
55: ; preds = %43
%56 = load i64, i64* %4
%57 = mul i64 %56, 2
store i64 %57, i64* %4
%58 = load i8*, i8** %3
%59 = getelementptr inbounds [4097 x i8], [4097 x i8]* %2, i64 0, i64 0
%60 = icmp eq i8* %58, %59
br i1 %60, label %61, label %64
61: ; preds = %55
%62 = load i64, i64* %4
%63 = call i8* @malloc(i64 %62)
store i8* %63, i8** %3
br label %68
64: ; preds = %55
%65 = load i8*, i8** %3
%66 = load i64, i64* %4
%67 = call i8* @realloc(i8* %65, i64 %66)
store i8* %67, i8** %3
br label %68
68: ; preds = %64, %61
%69 = load i8*, i8** %3
%70 = icmp ne i8* %69, null
br i1 %70, label %73, label %71
71: ; preds = %68
%72 = load i64, i64* @ENOMEM
store i64 %72, i64* @errno
store i8* null, i8** %1
br label %74
73: ; preds = %68
br label %9
74: ; preds = %71, %54, %37
%75 = load i8*, i8** %1
ret i8* %75
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [15 x i8] c"/proc/self/exe\00"
@ERANGE = global i64 0
@ENOMEM = global i64 0
define i8* @sgsXPC_GetModuleFileName() {
%1 = alloca [4097 x i8]
%2 = getelementptr inbounds [4097 x i8], [4097 x i8]* %1, i64 0, i64 0
store i64 0, i64* @errno
br label %3
3: ; preds = %34, %0
%4 = phi i64 [ 4096, %0 ], [ %28, %34 ]
%5 = phi i8* [ %2, %0 ], [ %35, %34 ]
%6 = call i64 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %5, i64 %4)
%7 = icmp ult i64 %6, %4
br i1 %7, label %8, label %17
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %5, i64 %6
store i8 0, i8* %9
%10 = icmp eq i8* %5, %2
br i1 %10, label %11, label %39
11: ; preds = %8
%12 = call i32 @strlen(i8* %2)
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = call i8* @malloc(i64 %14)
%16 = call i32 @memcpy(i8* %15, i8* %2, i64 %14)
br label %39
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @ERANGE
%20 = icmp ne i64 %18, %19
%21 = icmp ugt i64 %4, 40959
%22 = or i1 %21, %20
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = icmp eq i8* %5, %2
br i1 %24, label %39, label %25
25: ; preds = %23
%26 = call i32 @free(i8* %5)
br label %39
27: ; preds = %17
%28 = shl nuw nsw i64 %4, 1
%29 = icmp eq i8* %5, %2
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i8* @malloc(i64 %28)
br label %34
32: ; preds = %27
%33 = call i8* @realloc(i8* %5, i64 %28)
br label %34
34: ; preds = %32, %30
%35 = phi i8* [ %31, %30 ], [ %33, %32 ]
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %3
37: ; preds = %34
%38 = load i64, i64* @ENOMEM
store i64 %38, i64* @errno
br label %39
39: ; preds = %37, %11, %8, %25, %23
%40 = phi i8* [ null, %23 ], [ null, %25 ], [ %5, %8 ], [ %15, %11 ], [ null, %37 ]
ret i8* %40
}