#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 /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ readlink(char *, char *, int);
int main(int argc, char *argv[]) {
  char buf[10] = {0};
  ssize_t ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  printf("ret:%ld, readlink return: ", ret);
  printf("ret:%ld, readlink: buf:%s", ret, buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1a(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
lea    -0x1a(%rbp),%rsi
lea    0xe9f(%rip),%rdi
mov    $0x9,%edx
call   1030 <readlink@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%esi
lea    -0x1a(%rbp),%rdx
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movw   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xec3(%rip),%rdi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x9,%edx
call   1030 <readlink@plt>
mov    %eax,%ebp
lea    0xeb7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec2(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [27 x i8] c"ret:%ld, readlink return: \00"
@.str.2 = [26 x i8] c"ret:%ld, readlink: buf:%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10 x i8]
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast [10 x i8]* %6 to i8*
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%10 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %9, i32 9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* %7
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %13, i8* %14)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/self/exe\00"
@.str.1 = [27 x i8] c"ret:%ld, readlink return: \00"
@.str.2 = [26 x i8] c"ret:%ld, readlink: buf:%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %4, i32 9)
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %5, i8* %4)
ret i32 0
}