#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int lstat(char *, struct stat *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int readlink(char *, char *, int);
char *get_dwm_path() {
  struct stat s;
  int r, length, rate = 42;
  char *path = NULL;
  if (lstat("/proc/self/exe", &s) == -1) {
    perror("lstat:");
    return NULL;
  }
  length = s.st_size + 1 - rate;
  do {
    length += rate;
    free(path);
    path = malloc(sizeof(char) * length);
    if (path == NULL) {
      perror("malloc:");
      return NULL;
    }
    r = readlink("/proc/self/exe", path, length);
    if (r == -1) {
      perror("readlink:");
      return NULL;
    }
  } while (r >= length);
  path[r] = '\0';
  return path;
}


x86 assembly (clang, -O0):
<get_dwm_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x2a,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe92(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0xffffffff,%eax
jne    1199 <get_dwm_path+0x49>
lea    0xe88(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    124c <get_dwm_path+0xfc>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ee <get_dwm_path+0x9e>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    124c <get_dwm_path+0xfc>
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xe04(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1227 <get_dwm_path+0xd7>
lea    0xe09(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    124c <get_dwm_path+0xfc>
jmp    122c <get_dwm_path+0xdc>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11a5 <get_dwm_path+0x55>
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dwm_path>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xea0(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <lstat@plt>
cmp    $0xffffffff,%eax
je     11c6 <get_dwm_path+0x76>
mov    0x8(%rsp),%ebx
add    $0x1,%ebx
xor    %r14d,%r14d
lea    0xe80(%rip),%r15
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11cf <get_dwm_path+0x7f>
mov    %rax,%r14
mov    %r15,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11d8 <get_dwm_path+0x88>
lea    0x2a(%rbx),%ecx
cmp    %ebx,%eax
mov    %ecx,%ebx
jge    1180 <get_dwm_path+0x30>
cltq
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe42(%rip),%rdi
jmp    11df <get_dwm_path+0x8f>
lea    0xe40(%rip),%rdi
jmp    11df <get_dwm_path+0x8f>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
xor    %r14d,%r14d
jmp    11b9 <get_dwm_path+0x69>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [7 x i8] c"lstat:\00"
@.str.2 = [8 x i8] c"malloc:\00"
@.str.3 = [10 x i8] c"readlink:\00"
define i8* @get_dwm_path() {
%1 = alloca i8*
%2 = alloca %struct.stat
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i32 42, i32* %5
store i8* null, i8** %6
%7 = call i32 @lstat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
br label %51
11: ; preds = %0
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%13 = load i32, i32* %12
%14 = add nsw i32 %13, 1
%15 = load i32, i32* %5
%16 = sub nsw i32 %14, %15
store i32 %16, i32* %4
br label %17
17: ; preds = %41, %11
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = add nsw i32 %19, %18
store i32 %20, i32* %4
%21 = load i8*, i8** %6
%22 = call i32 @free(i8* %21)
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = mul i64 1, %24
%26 = trunc i64 %25 to i32
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %51
32: ; preds = %17
%33 = load i8*, i8** %6
%34 = load i32, i32* %4
%35 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %33, i32 %34)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %32
%39 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %1
br label %51
40: ; preds = %32
br label %41
41: ; preds = %40
%42 = load i32, i32* %3
%43 = load i32, i32* %4
%44 = icmp sge i32 %42, %43
br i1 %44, label %17, label %45
45: ; preds = %41
%46 = load i8*, i8** %6
%47 = load i32, i32* %3
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %6
store i8* %50, i8** %1
br label %51
51: ; preds = %45, %38, %30, %9
%52 = load i8*, i8** %1
ret i8* %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [7 x i8] c"lstat:\00"
@.str.2 = [8 x i8] c"malloc:\00"
@.str.3 = [10 x i8] c"readlink:\00"
define noalias i8* @get_dwm_path() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @lstat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %30
7: ; preds = %0
%8 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = add nsw i32 %9, -41
br label %11
11: ; preds = %25, %7
%12 = phi i32 [ %10, %7 ], [ %14, %25 ]
%13 = phi i8* [ null, %7 ], [ %16, %25 ]
%14 = add nsw i32 %12, 42
%15 = tail call i32 @free(i8* %13)
%16 = tail call i8* @malloc(i32 %14)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %30
20: ; preds = %11
%21 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %14)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %30
25: ; preds = %20
%26 = icmp slt i32 %21, %14
br i1 %26, label %27, label %11
27: ; preds = %25
%28 = sext i32 %21 to i64
%29 = getelementptr inbounds i8, i8* %16, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %27, %23, %18, %5
%31 = phi i8* [ null, %5 ], [ null, %18 ], [ null, %23 ], [ %16, %27 ]
ret i8* %31
}