#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 *, size_t);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strrchr(char *, char);
void locate_haproxy(char *buffer, size_t buffer_size) {
  char *end = NULL;
  int len;
  len = readlink("/proc/self/exe", buffer, buffer_size - 1);
  if (len == -1)
    goto fail;
  buffer[len] = 0;
  end = strrchr(buffer, '/');
  if (end == NULL)
    goto fail;
  if (strcmp(end + strlen(end) - 16, "-systemd-wrapper") == 0) {
    end[strlen(end) - 16] = '\0';
    return;
  }
  end[1] = '\0';
  strncpy(end + 1, "haproxy", buffer + buffer_size - (end + 1));
  buffer[buffer_size - 1] = '\0';
  return;
fail:
  strncpy(buffer, "/usr/sbin/haproxy", buffer_size);
  buffer[buffer_size - 1] = '\0';
  return;
}


x86 assembly (clang, -O0):
<locate_haproxy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    $0x1,%rdx
lea    0xe85(%rip),%rdi
call   1040 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1192 <locate_haproxy+0x42>
jmp    125e <locate_haproxy+0x10e>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <locate_haproxy+0x70>
jmp    125e <locate_haproxy+0x10e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffff0,%rdi
lea    0xe2a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    121a <locate_haproxy+0xca>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x10,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1282 <locate_haproxy+0x132>
mov    -0x18(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    -0x8(%rbp),%rdx
add    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rax
add    $0x1,%rax
sub    %rax,%rdx
lea    0xde6(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1282 <locate_haproxy+0x132>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdb3(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<locate_haproxy>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
lea    -0x1(%rsi),%r14
lea    0xe97(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11d3 <locate_haproxy+0x83>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11d3 <locate_haproxy+0x83>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <strlen@plt>
cltq
lea    (%r12,%rax,1),%rdi
add    $0xfffffffffffffff0,%rdi
lea    0xe62(%rip),%rsi
call   1070 <strcmp@plt>
mov    %r12,%rdi
test   %rax,%rax
je     11f8 <locate_haproxy+0xa8>
add    $0x1,%rdi
movb   $0x0,0x1(%r12)
add    %rbx,%r15
sub    %rdi,%r15
lea    0xe59(%rip),%rsi
jmp    11dd <locate_haproxy+0x8d>
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1030 <strncpy@plt>
add    %rbx,%r14
movb   $0x0,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
call   1050 <strlen@plt>
cltq
lea    (%rax,%r12,1),%r14
add    $0xfffffffffffffff0,%r14
jmp    11e8 <locate_haproxy+0x98>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [17 x i8] c"-systemd-wrapper\00"
@.str.2 = [8 x i8] c"haproxy\00"
@.str.3 = [18 x i8] c"/usr/sbin/haproxy\00"
define void @locate_haproxy(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i8* null, i8** %5
%7 = load i8*, i8** %3
%8 = load i64, i64* %4
%9 = sub i64 %8, 1
%10 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7, i64 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
br label %58
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %18
%19 = load i8*, i8** %3
%20 = call i8* @strrchr(i8* %19, i8 signext 47)
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %14
br label %58
24: ; preds = %14
%25 = load i8*, i8** %5
%26 = load i8*, i8** %5
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
%30 = getelementptr inbounds i8, i8* %29, i64 -16
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %24
%34 = load i8*, i8** %5
%35 = load i8*, i8** %5
%36 = call i32 @strlen(i8* %35)
%37 = sub nsw i32 %36, 16
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %34, i64 %38
store i8 0, i8* %39
br label %66
40: ; preds = %24
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 1
store i8 0, i8* %42
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 1
%45 = load i8*, i8** %3
%46 = load i64, i64* %4
%47 = getelementptr inbounds i8, i8* %45, i64 %46
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = ptrtoint i8* %47 to i64
%51 = ptrtoint i8* %49 to i64
%52 = sub i64 %50, %51
%53 = call i32 @strncpy(i8* %44, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %52)
%54 = load i8*, i8** %3
%55 = load i64, i64* %4
%56 = sub i64 %55, 1
%57 = getelementptr inbounds i8, i8* %54, i64 %56
store i8 0, i8* %57
br label %66
58: ; preds = %23, %13
%59 = load i8*, i8** %3
%60 = load i64, i64* %4
%61 = call i32 @strncpy(i8* %59, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %60)
%62 = load i8*, i8** %3
%63 = load i64, i64* %4
%64 = sub i64 %63, 1
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
br label %66
66: ; preds = %58, %40, %33
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [17 x i8] c"-systemd-wrapper\00"
@.str.2 = [8 x i8] c"haproxy\00"
@.str.3 = [18 x i8] c"/usr/sbin/haproxy\00"
define void @locate_haproxy(i8* %0, i64 %1) {
%3 = add i64 %1, -1
%4 = tail call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %31, label %6
6: ; preds = %2
%7 = sext i32 %4 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 0, i8* %8
%9 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %31, label %11
11: ; preds = %6
%12 = tail call i32 @strlen(i8* %9)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %9, i64 %13
%15 = getelementptr inbounds i8, i8* %14, i64 -16
%16 = tail call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %11
%19 = tail call i32 @strlen(i8* %9)
%20 = add nsw i32 %19, -16
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %9, i64 %21
br label %34
23: ; preds = %11
%24 = getelementptr inbounds i8, i8* %9, i64 1
store i8 0, i8* %24
%25 = getelementptr inbounds i8, i8* %0, i64 %1
%26 = ptrtoint i8* %25 to i64
%27 = ptrtoint i8* %24 to i64
%28 = sub i64 %26, %27
%29 = tail call i32 @strncpy(i8* %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%30 = getelementptr inbounds i8, i8* %0, i64 %3
br label %34
31: ; preds = %6, %2
%32 = tail call i32 @strncpy(i8* %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%33 = getelementptr inbounds i8, i8* %0, i64 %3
br label %34
34: ; preds = %31, %23, %18
%35 = phi i8* [ %33, %31 ], [ %30, %23 ], [ %22, %18 ]
store i8 0, i8* %35
ret void
}