#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
void get_proc_name(char *out, size_t size) {
  int fd = open("/proc/self/cmdline", O_RDONLY);
  if (fd != -1) {
    int n = read(fd, out, size);
    if (n >= 0) {
      out[n] = '\0';
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<get_proc_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
lea    0xeb0(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1193 <get_proc_name+0x63>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     118b <get_proc_name+0x5b>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_name>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1178 <get_proc_name+0x48>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
test   %eax,%eax
js     116d <get_proc_name+0x3d>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <close@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@O_RDONLY = global i32 0
define void @get_proc_name(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %26
11: ; preds = %2
%12 = load i32, i32* %5
%13 = load i8*, i8** %3
%14 = load i64, i64* %4
%15 = call i32 @read(i32 %12, i8* %13, i64 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %11
%19 = load i8*, i8** %3
%20 = load i32, i32* %6
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %18, %11
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
br label %26
26: ; preds = %23, %2
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@O_RDONLY = global i32 0
define void @get_proc_name(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = tail call i32 @read(i32 %4, i8* %0, i64 %1)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = zext i32 %7 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %9, %6
%13 = tail call i32 @close(i32 %4)
br label %14
14: ; preds = %12, %2
ret void
}