#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
size_t strlen(char *);
bool get_proc_name(char *buffer, size_t size) {
  FILE *fcomm;
  size_t len;
  fcomm = fopen("/proc/self/comm", "r");
  if (!fcomm) {
    perror("fopen(/proc/self/comm)");
    return false;
  }
  if (!fgets(buffer, (int)size, fcomm)) {
    fprintf(stderr, "Unable to read /proc/self/comm\n");
    fclose(fcomm);
    return false;
  }
  fclose(fcomm);
  len = strlen(buffer);
  assert(len > 0 && len < size);
  if (buffer[len - 1] == '\n') {
    buffer[len - 1] = '\0';
  }
  return true;
}


x86 assembly (clang, -O0):
<get_proc_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe79(%rip),%rdi
lea    0xe82(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ba <get_proc_name+0x4a>
lea    0xe69(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1271 <get_proc_name+0x101>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    1200 <get_proc_name+0x90>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1271 <get_proc_name+0x101>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jbe    1234 <get_proc_name+0xc4>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setb   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    126a <get_proc_name+0xfa>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_proc_name>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe7e(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     120a <get_proc_name+0x9a>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     11e5 <get_proc_name+0x75>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
add    $0xffffffffffffffff,%rbx
setb   %cl
cmp    %r14,%rax
setb   %al
and    %cl,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x1,%eax
cmpb   $0xa,(%r15,%rbx,1)
jne    1204 <get_proc_name+0x94>
movb   $0x0,(%r15,%rbx,1)
jmp    1204 <get_proc_name+0x94>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
jmp    1202 <get_proc_name+0x92>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/comm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"fopen(/proc/self/comm)\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Unable to read /proc/self/comm\0A\00"
define i32 @get_proc_name(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %53
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i64, i64* %5
%16 = trunc i64 %15 to i32
%17 = load i32*, i32** %6
%18 = call i32 @fgets(i8* %14, i32 %16, i32* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
store i32 0, i32* %3
br label %53
25: ; preds = %13
%26 = load i32*, i32** %6
%27 = call i32 @fclose(i32* %26)
%28 = load i8*, i8** %4
%29 = call i64 @strlen(i8* %28)
store i64 %29, i64* %7
%30 = load i64, i64* %7
%31 = icmp ugt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i64, i64* %7
%34 = load i64, i64* %5
%35 = icmp ult i64 %33, %34
br label %36
36: ; preds = %32, %25
%37 = phi i1 [ false, %25 ], [ %35, %32 ]
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load i8*, i8** %4
%41 = load i64, i64* %7
%42 = sub i64 %41, 1
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 10
br i1 %46, label %47, label %52
47: ; preds = %36
%48 = load i8*, i8** %4
%49 = load i64, i64* %7
%50 = sub i64 %49, 1
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %47, %36
store i32 1, i32* %3
br label %53
53: ; preds = %52, %20, %11
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/comm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"fopen(/proc/self/comm)\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Unable to read /proc/self/comm\0A\00"
define i32 @get_proc_name(i8* %0, i64 %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %28
7: ; preds = %2
%8 = trunc i64 %1 to i32
%9 = tail call i32 @fgets(i8* %0, i32 %8, i32* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 @fclose(i32* %3)
br label %28
15: ; preds = %7
%16 = tail call i32 @fclose(i32* %3)
%17 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%18 = icmp ne i64 %17, 0
%19 = icmp ult i64 %17, %1
%20 = and i1 %18, %19
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = add i64 %17, -1
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 10
br i1 %26, label %27, label %28
27: ; preds = %15
store i8 0, i8* %24
br label %28
28: ; preds = %15, %27, %11, %5
%29 = phi i32 [ 0, %11 ], [ 0, %5 ], [ 1, %27 ], [ 1, %15 ]
ret i32 %29
}