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

/* Variables and functions */
int /*<<< orphan*/ PR_SET_NAME;
int /*<<< orphan*/ get_proc_name(char *, int);
int /*<<< orphan*/ perror(char *);
int prctl(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ printf(char *, char const *);
scalar_t__ strcmp(char const *, char *);
int main(void) {
  const char *new_name = "Hello, world!";
  char buffer[4096];
  if (!get_proc_name(buffer, sizeof(buffer))) {
    return 1;
  }
  printf("Old name: %s\n", buffer);
  if (prctl(PR_SET_NAME, new_name) == -1) {
    perror("prctl");
    return 1;
  }
  if (!get_proc_name(buffer, sizeof(buffer))) {
    return 1;
  }
  printf("New name: %s\n", buffer);
  if (strcmp(new_name, buffer)) {
    printf("Error: expected name was %s\n", new_name);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    $0x1000,%esi
call   1030 <get_proc_name@plt>
cmp    $0x0,%eax
jne    1193 <main+0x43>
movl   $0x1,-0x4(%rbp)
jmp    124f <main+0xff>
lea    -0x1010(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
call   1060 <prctl@plt>
cmp    $0xffffffff,%eax
jne    11d9 <main+0x89>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124f <main+0xff>
lea    -0x1010(%rbp),%rdi
mov    $0x1000,%esi
call   1030 <get_proc_name@plt>
cmp    $0x0,%eax
jne    11ff <main+0xaf>
movl   $0x1,-0x4(%rbp)
jmp    124f <main+0xff>
lea    -0x1010(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1248 <main+0xf8>
mov    -0x10(%rbp),%rsi
lea    0xdf9(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    124f <main+0xff>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1030 <get_proc_name@plt>
mov    $0x1,%ebx
test   %eax,%eax
je     11f9 <main+0xa9>
lea    0xe93(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1060 <prctl@plt>
cmp    $0xffffffff,%eax
je     11ed <main+0x9d>
mov    %rsp,%rdi
mov    $0x1000,%esi
call   1030 <get_proc_name@plt>
test   %eax,%eax
je     11f9 <main+0xa9>
lea    0xe6d(%rip),%rdi
mov    %rsp,%r14
mov    %r14,%rsi
call   1040 <printf@plt>
lea    0xe39(%rip),%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11e9 <main+0x99>
lea    0xe55(%rip),%rdi
lea    0xe1e(%rip),%rsi
call   1040 <printf@plt>
jmp    11f9 <main+0xa9>
xor    %ebx,%ebx
jmp    11f9 <main+0xa9>
lea    0xe28(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Hello, world!\00"
@.str.1 = [14 x i8] c"Old name: %s\0A\00"
@PR_SET_NAME = global i32 0
@.str.2 = [6 x i8] c"prctl\00"
@.str.3 = [14 x i8] c"New name: %s\0A\00"
@.str.4 = [29 x i8] c"Error: expected name was %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [4096 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @get_proc_name(i8* %4, i32 4096)
%6 = icmp ne i32 %5, 0
br i1 %6, label %8, label %7
7: ; preds = %0
store i32 1, i32* %1
br label %33
8: ; preds = %0
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%10 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @PR_SET_NAME
%12 = load i8*, i8** %2
%13 = call i32 @prctl(i32 %11, i8* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %33
17: ; preds = %8
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%19 = call i32 @get_proc_name(i8* %18, i32 4096)
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %17
store i32 1, i32* %1
br label %33
22: ; preds = %17
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%24 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %2
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%27 = call i64 @strcmp(i8* %25, i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i8*, i8** %2
%31 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %30)
store i32 1, i32* %1
br label %33
32: ; preds = %22
store i32 0, i32* %1
br label %33
33: ; preds = %32, %29, %21, %15, %7
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [14 x i8] c"Hello, world!\00"
@.str.1 = [14 x i8] c"Old name: %s\0A\00"
@PR_SET_NAME = global i32 0
@.str.2 = [6 x i8] c"prctl\00"
@.str.3 = [14 x i8] c"New name: %s\0A\00"
@.str.4 = [29 x i8] c"Error: expected name was %s\0A\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = call i32 @get_proc_name(i8* %2, i32 4096)
%4 = icmp eq i32 %3, 0
br i1 %4, label %21, label %5
5: ; preds = %0
%6 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%7 = load i32, i32* @PR_SET_NAME
%8 = call i32 @prctl(i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %21
12: ; preds = %5
%13 = call i32 @get_proc_name(i8* %2, i32 4096)
%14 = icmp eq i32 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%17 = call i64 @strcmp(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %15, %12, %0, %19, %10
%22 = phi i32 [ 1, %10 ], [ 1, %19 ], [ 1, %0 ], [ 1, %12 ], [ 0, %15 ]
ret i32 %22
}