#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*/ path;
typedef int /*<<< orphan*/ cwd;

/* Variables and functions */
int /*<<< orphan*/ getcwd(char *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ test_cmdline(char *, char *, char *);
int /*<<< orphan*/ test_readlink(char *, char *, char *);
int /*<<< orphan*/ test_readlinkat(char *, char *, char *);
int main(int argc, char **argv) {
  char cwd[512];
  char path[512];
  cwd[0] = 0;
  if (!getcwd(cwd, sizeof(cwd)))
    perror("getcwd");
  strcat(cwd, "/");
  snprintf(path, sizeof(path), "/proc/%d/cmdline", getpid());
  test_cmdline(cwd, "/proc/self/cmdline", "/proc/self/cmdline");
  test_cmdline(cwd, "/proc/<pid>/cmdline", path);
  snprintf(path, sizeof(path), "/proc/%d/exe", getpid());
  test_readlink(cwd, "/proc/self/exe", "/proc/self/exe");
  test_readlink(cwd, "/proc/<pid>/exe", path);
  test_readlinkat(cwd, "/proc/self/exe", "/proc/self/exe");
  test_readlinkat(cwd, "/proc/<pid>/exe", path);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x210(%rbp)
lea    -0x210(%rbp),%rdi
mov    $0x200,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%eax
jne    11c6 <main+0x46>
lea    0xe3f(%rip),%rdi
call   1080 <perror@plt>
lea    -0x210(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x420(%rbp),%rdi
mov    %eax,%ecx
mov    $0x200,%esi
lea    0xe06(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x210(%rbp),%rdi
lea    0xe04(%rip),%rdx
mov    %rdx,%rsi
call   1040 <test_cmdline@plt>
lea    -0x210(%rbp),%rdi
lea    -0x410(%rbp),%rdx
lea    0xdfa(%rip),%rsi
call   1040 <test_cmdline@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x418(%rbp),%rdi
mov    %eax,%ecx
mov    $0x200,%esi
lea    0xddf(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x210(%rbp),%rdi
lea    0xdd9(%rip),%rdx
mov    %rdx,%rsi
call   10a0 <test_readlink@plt>
lea    -0x210(%rbp),%rdi
lea    -0x410(%rbp),%rdx
lea    0xdcb(%rip),%rsi
call   10a0 <test_readlink@plt>
lea    -0x210(%rbp),%rdi
lea    0xda9(%rip),%rdx
mov    %rdx,%rsi
call   1070 <test_readlinkat@plt>
lea    -0x210(%rbp),%rdi
lea    -0x410(%rbp),%rdx
lea    0xd9b(%rip),%rsi
call   1070 <test_readlinkat@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x408,%rsp
movb   $0x0,(%rsp)
mov    %rsp,%rdi
mov    $0x200,%esi
call   1060 <getcwd@plt>
test   %eax,%eax
je     1276 <main+0xf6>
lea    0xe59(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
call   1090 <strcat@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe42(%rip),%rdx
lea    0x200(%rsp),%r14
mov    %r14,%rdi
mov    $0x200,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
lea    0xe35(%rip),%rdx
mov    %r15,%rdi
mov    %rdx,%rsi
call   1040 <test_cmdline@plt>
lea    0xe36(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1040 <test_cmdline@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe31(%rip),%rdx
mov    %r14,%rdi
mov    $0x200,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
lea    0xe28(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
call   10a0 <test_readlink@plt>
lea    0xe22(%rip),%r12
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   10a0 <test_readlink@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbx,%rdx
call   1070 <test_readlinkat@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1070 <test_readlinkat@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd83(%rip),%rdi
call   1080 <perror@plt>
jmp    11a7 <main+0x27>

LLVM IR (-O0):
@.str = [7 x i8] c"getcwd\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [17 x i8] c"/proc/%d/cmdline\00"
@.str.3 = [19 x i8] c"/proc/self/cmdline\00"
@.str.4 = [20 x i8] c"/proc/<pid>/cmdline\00"
@.str.5 = [13 x i8] c"/proc/%d/exe\00"
@.str.6 = [15 x i8] c"/proc/self/exe\00"
@.str.7 = [16 x i8] c"/proc/<pid>/exe\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [512 x i8]
%7 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
store i8 0, i8* %8
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%10 = call i32 @getcwd(i8* %9, i32 512)
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = call i32 @strcat(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%18 = call i32 (...) @getpid()
%19 = call i32 @snprintf(i8* %17, i32 512, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%21 = call i32 @test_cmdline(i8* %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%24 = call i32 @test_cmdline(i8* %22, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%26 = call i32 (...) @getpid()
%27 = call i32 @snprintf(i8* %25, i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%29 = call i32 @test_readlink(i8* %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%32 = call i32 @test_readlink(i8* %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%34 = call i32 @test_readlinkat(i8* %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%35 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%37 = call i32 @test_readlinkat(i8* %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"getcwd\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [17 x i8] c"/proc/%d/cmdline\00"
@.str.3 = [19 x i8] c"/proc/self/cmdline\00"
@.str.4 = [20 x i8] c"/proc/<pid>/cmdline\00"
@.str.5 = [13 x i8] c"/proc/%d/exe\00"
@.str.6 = [15 x i8] c"/proc/self/exe\00"
@.str.7 = [16 x i8] c"/proc/<pid>/exe\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [512 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
store i8 0, i8* %5
%7 = call i32 @getcwd(i8* %5, i32 512)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 (...) @getpid()
%14 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = call i32 @test_cmdline(i8* %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%16 = call i32 @test_cmdline(i8* %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%17 = call i32 (...) @getpid()
%18 = call i32 @snprintf(i8* %6, i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = call i32 @test_readlink(i8* %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%20 = call i32 @test_readlink(i8* %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%21 = call i32 @test_readlinkat(i8* %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%22 = call i32 @test_readlinkat(i8* %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %6)
ret i32 0
}