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

/* Variables and functions */
int /*<<< orphan*/ PTRACE_ATTACH;
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_PEEKDATA;
int /*<<< orphan*/ PTRACE_POKEDATA;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long *malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int ptrace(int /*<<< orphan*/, unsigned int, unsigned long, unsigned long);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int sscanf(char *, char *, unsigned long *, unsigned long *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int setproctitle(unsigned int pid, char *title) {
  char buf[2048], *tmp;
  FILE *f;
  int i, ret = 0;
  size_t len, peek_poke_num_words = 0;
  unsigned long arg_start, arg_end, peek_poke_start, peek_poke_end,
      *peek_poke_buf;
  sprintf(buf, "/proc/%d/stat", pid);
  errno = 0;
  f = fopen(buf, "r");
  if (!f) {
    perror("Invalid PID specified?");
    return -1;
  }
  errno = 0;
  tmp = fgets(buf, sizeof(buf), f);
  fclose(f);
  if (!tmp) {
    perror("Error reading /proc/PID/stat.");
    return -1;
  }
  tmp = strchr(buf, ' ');
  for (i = 0; i < 46; i++) {
    if (!tmp) {
      printf("/proc/PID/strace format changed?\n");
      return -1;
    }
    tmp = strchr(tmp + 1, ' ');
  }
  if (!tmp)
    return -1;
  i = sscanf(tmp, "%lu %lu", &arg_start, &arg_end);
  if (i != 2) {
    printf("/proc/PID/strace format changed?\n");
    return -1;
  }
  len = strlen(title) + 1;
  if (len > arg_end - arg_start) {
    printf("Can't set a title that is larger than the current one :(");
    return -1;
  } else if (len < arg_end - arg_start) {
    for (i = len; i < arg_end - arg_start; i++) {
      strcat(title, " ");
    }
  }
  errno = 0;
  if (ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
    perror("Unable to attach.");
    return -1;
  }
  wait(NULL);
  peek_poke_start = arg_start & ~(sizeof(long) - 1);
  peek_poke_end = (arg_end & ~(sizeof(long) - 1)) + sizeof(long);
  peek_poke_num_words = (peek_poke_end - peek_poke_start) / sizeof(long);
  peek_poke_buf = malloc(peek_poke_num_words * sizeof(long));
  errno = 0;
  for (i = 0; i < peek_poke_num_words; i++) {
    if ((ret = ptrace(PTRACE_PEEKDATA, pid, peek_poke_start + i * sizeof(long),
                      NULL)) == -1) {
      perror("Error setting the new title.");
      return -1;
    }
    peek_poke_buf[i] = ret;
  }
  strcpy((char *)peek_poke_buf + arg_start - peek_poke_start, title);
  errno = 0;
  for (i = 0; i < peek_poke_num_words; i++) {
    if (ptrace(PTRACE_POKEDATA, pid, peek_poke_start + i * sizeof(long),
               peek_poke_buf[i]) == -1) {
      perror("Error setting the new title. Target potentially left in "
             "incosistent state.");
      return -1;
    }
  }
  errno = 0;
  if (ptrace(PTRACE_CONT, pid, 0, 0) == -1) {
    perror("Error setting the new title while resuming the process.");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setproctitle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x828(%rbp)
movq   $0x0,-0x838(%rbp)
lea    -0x810(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xde8(%rip),%rsi
call   10f0 <sprintf@plt>
mov    0x2dac(%rip),%rax
movq   $0x0,(%rax)
lea    -0x810(%rbp),%rdi
lea    0xdd5(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x820(%rbp)
cmpq   $0x0,-0x820(%rbp)
jne    126b <setproctitle+0x8b>
lea    0xdb6(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
mov    0x2d5e(%rip),%rax
movq   $0x0,(%rax)
lea    -0x810(%rbp),%rdi
mov    -0x820(%rbp),%rdx
mov    $0x800,%esi
call   1080 <fgets@plt>
mov    %rax,-0x818(%rbp)
mov    -0x820(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x818(%rbp)
jne    12ca <setproctitle+0xea>
lea    0xd6e(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
lea    -0x810(%rbp),%rdi
mov    $0x20,%esi
call   1060 <strchr@plt>
mov    %rax,-0x818(%rbp)
movl   $0x0,-0x824(%rbp)
cmpl   $0x2e,-0x824(%rbp)
jge    134f <setproctitle+0x16f>
cmpq   $0x0,-0x818(%rbp)
jne    131f <setproctitle+0x13f>
lea    0xd37(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
mov    -0x818(%rbp),%rdi
add    $0x1,%rdi
mov    $0x20,%esi
call   1060 <strchr@plt>
mov    %rax,-0x818(%rbp)
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
jmp    12ec <setproctitle+0x10c>
cmpq   $0x0,-0x818(%rbp)
jne    1369 <setproctitle+0x189>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
mov    -0x818(%rbp),%rdi
lea    0xcf0(%rip),%rsi
lea    -0x840(%rbp),%rdx
lea    -0x848(%rbp),%rcx
call   10a0 <sscanf@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x2,-0x824(%rbp)
je     13b5 <setproctitle+0x1d5>
lea    0xca1(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rax
mov    -0x848(%rbp),%rcx
sub    -0x840(%rbp),%rcx
cmp    %rcx,%rax
jbe    1400 <setproctitle+0x220>
lea    0xc80(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
mov    -0x830(%rbp),%rax
mov    -0x848(%rbp),%rcx
sub    -0x840(%rbp),%rcx
cmp    %rcx,%rax
jae    1472 <setproctitle+0x292>
mov    -0x830(%rbp),%rax
mov    %eax,-0x824(%rbp)
movslq -0x824(%rbp),%rax
mov    -0x848(%rbp),%rcx
sub    -0x840(%rbp),%rcx
cmp    %rcx,%rax
jae    146d <setproctitle+0x28d>
mov    -0x10(%rbp),%rdi
lea    0xc54(%rip),%rsi
call   10e0 <strcat@plt>
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
jmp    142b <setproctitle+0x24b>
jmp    1472 <setproctitle+0x292>
jmp    1477 <setproctitle+0x297>
mov    0x2b52(%rip),%rax
movq   $0x0,(%rax)
mov    0x2b5c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10b0 <ptrace@plt>
cmp    $0xffffffff,%eax
sete   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x828(%rbp)
test   $0x1,%al
jne    14be <setproctitle+0x2de>
jmp    14d6 <setproctitle+0x2f6>
lea    0xbe5(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
xor    %eax,%eax
mov    %eax,%edi
call   1100 <wait@plt>
mov    -0x840(%rbp),%rax
and    $0xfffffffffffffff8,%rax
mov    %rax,-0x850(%rbp)
mov    -0x848(%rbp),%rax
and    $0xfffffffffffffff8,%rax
add    $0x8,%rax
mov    %rax,-0x858(%rbp)
mov    -0x858(%rbp),%rax
sub    -0x850(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rdi
shl    $0x3,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x860(%rbp)
mov    0x2a92(%rip),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x824(%rbp)
movslq -0x824(%rbp),%rax
cmp    -0x838(%rbp),%rax
jae    15e1 <setproctitle+0x401>
mov    0x2a5e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x850(%rbp),%rdx
movslq -0x824(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   10b0 <ptrace@plt>
mov    %eax,-0x828(%rbp)
cmp    $0xffffffff,%eax
jne    15b4 <setproctitle+0x3d4>
lea    0xb19(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
movslq -0x828(%rbp),%rdx
mov    -0x860(%rbp),%rax
movslq -0x824(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
jmp    154f <setproctitle+0x36f>
mov    -0x860(%rbp),%rdi
add    -0x840(%rbp),%rdi
xor    %eax,%eax
sub    -0x850(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x29c5(%rip),%rax
movq   $0x0,(%rax)
movl   $0x0,-0x824(%rbp)
movslq -0x824(%rbp),%rax
cmp    -0x838(%rbp),%rax
jae    16a2 <setproctitle+0x4c2>
mov    0x29a1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
mov    -0x850(%rbp),%rdx
movslq -0x824(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdx
mov    -0x860(%rbp),%rax
movslq -0x824(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
call   10b0 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1689 <setproctitle+0x4a9>
lea    0xa61(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
jmp    168e <setproctitle+0x4ae>
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
jmp    161c <setproctitle+0x43c>
mov    0x2927(%rip),%rax
movq   $0x0,(%rax)
mov    0x2909(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10b0 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    16e9 <setproctitle+0x509>
lea    0xa4c(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    16f0 <setproctitle+0x510>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setproctitle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    %rsi,%r12
mov    %edi,%r15d
lea    0xdf2(%rip),%rsi
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15d,%edx
call   1100 <sprintf@plt>
mov    0x2dab(%rip),%r14
movq   $0x0,(%r14)
lea    0xddb(%rip),%rsi
mov    %rbx,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     148c <setproctitle+0x29c>
mov    %rax,%rbx
movq   $0x0,(%r14)
lea    0x30(%rsp),%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1090 <fgets@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rbp,%rbp
je     1495 <setproctitle+0x2a5>
lea    0x30(%rsp),%rdi
mov    $0x20,%esi
call   1070 <strchr@plt>
mov    $0x2e,%ebx
nopl   0x0(%rax,%rax,1)
test   %rax,%rax
je     12fe <setproctitle+0x10e>
add    $0x1,%rax
mov    %rax,%rdi
mov    $0x20,%esi
call   1070 <strchr@plt>
add    $0xffffffff,%ebx
jne    1290 <setproctitle+0xa0>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     130f <setproctitle+0x11f>
lea    0xd89(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
mov    %rax,%rdi
call   10b0 <sscanf@plt>
cmp    $0x2,%eax
jne    12fe <setproctitle+0x10e>
mov    %r12,%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbp
mov    0x18(%rsp),%rax
sub    0x8(%rsp),%rax
cmp    %rbp,%rax
jae    1323 <setproctitle+0x133>
lea    0xd56(%rip),%rdi
call   1080 <printf@plt>
jmp    130a <setproctitle+0x11a>
lea    0xe35(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x838,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
jbe    134e <setproctitle+0x15e>
lea    0xd5a(%rip),%rbx
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
call   10f0 <strcat@plt>
add    $0x1,%rbp
mov    0x18(%rsp),%rax
sub    0x8(%rsp),%rax
cmp    %rbp,%rax
ja     1330 <setproctitle+0x140>
movq   $0x0,(%r14)
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10c0 <ptrace@plt>
cmp    $0xffffffff,%eax
je     14f0 <setproctitle+0x300>
xor    %edi,%edi
call   1110 <wait@plt>
mov    0x8(%rsp),%rbp
mov    %rbp,0x10(%rsp)
and    $0xfffffffffffffff8,%rbp
mov    0x18(%rsp),%rbx
add    $0x8,%rbx
and    $0xfffffffffffffff8,%rbx
sub    %rbp,%rbx
mov    %rbx,%rdi
call   10a0 <malloc@plt>
mov    %rax,%r13
movq   $0x0,(%r14)
test   %rbx,%rbx
je     14a6 <setproctitle+0x2b6>
mov    %r12,0x10(%rsp)
mov    %rbx,0x28(%rsp)
shr    $0x3,%rbx
cmp    $0x2,%rbx
mov    $0x1,%r12d
mov    %rbx,0x20(%rsp)
cmovae %rbx,%r12
xor    %r14d,%r14d
mov    %rbp,%rbx
nopw   0x0(%rax,%rax,1)
mov    0x2be1(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   10c0 <ptrace@plt>
cmp    $0xffffffff,%eax
je     14f9 <setproctitle+0x309>
cltq
mov    %rax,0x0(%r13,%r14,8)
add    $0x1,%r14
add    $0x8,%rbx
cmp    %r14,%r12
jne    13e0 <setproctitle+0x1f0>
mov    0x8(%rsp),%rdi
add    %r13,%rdi
sub    %rbp,%rdi
mov    0x10(%rsp),%rsi
call   1030 <strcpy@plt>
mov    0x2ba1(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,0x28(%rsp)
je     14b9 <setproctitle+0x2c9>
mov    0x20(%rsp),%rax
cmp    $0x2,%rax
mov    $0x1,%r14d
cmovae %rax,%r14
xor    %ebx,%ebx
mov    0x2b7e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    0x0(%r13,%rbx,8),%rcx
mov    %r15d,%esi
mov    %rbp,%rdx
call   10c0 <ptrace@plt>
cmp    $0xffffffff,%eax
je     1502 <setproctitle+0x312>
add    $0x1,%rbx
add    $0x8,%rbp
cmp    %rbx,%r14
jne    1460 <setproctitle+0x270>
jmp    14b9 <setproctitle+0x2c9>
lea    0xb7d(%rip),%rdi
jmp    149c <setproctitle+0x2ac>
lea    0xb8b(%rip),%rdi
call   10e0 <perror@plt>
jmp    130a <setproctitle+0x11a>
add    0x10(%rsp),%r13
sub    %rbp,%r13
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    0x2b10(%rip),%rax
movq   $0x0,(%rax)
mov    0x2af2(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    %r15d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   10c0 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    130f <setproctitle+0x11f>
lea    0xc14(%rip),%rdi
jmp    149c <setproctitle+0x2ac>
lea    0xb91(%rip),%rdi
jmp    149c <setproctitle+0x2ac>
lea    0xb9a(%rip),%rdi
jmp    149c <setproctitle+0x2ac>
lea    0xbae(%rip),%rdi
jmp    149c <setproctitle+0x2ac>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/%d/stat\00"
@errno = global i64 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Invalid PID specified?\00"
@.str.3 = [30 x i8] c"Error reading /proc/PID/stat.\00"
@.str.4 = [34 x i8] c"/proc/PID/strace format changed?\0A\00"
@.str.5 = [8 x i8] c"%lu %lu\00"
@.str.6 = [57 x i8] c"Can't set a title that is larger than the current one :(\00"
@.str.7 = [2 x i8] c" \00"
@PTRACE_ATTACH = global i32 0
@.str.8 = [18 x i8] c"Unable to attach.\00"
@PTRACE_PEEKDATA = global i32 0
@.str.9 = [29 x i8] c"Error setting the new title.\00"
@PTRACE_POKEDATA = global i32 0
@.str.10 = [75 x i8] c"Error setting the new title. Target potentially left in incosistent state.\00"
@PTRACE_CONT = global i32 0
@.str.11 = [56 x i8] c"Error setting the new title while resuming the process.\00"
define i32 @setproctitle(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [2048 x i8]
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %10
store i64 0, i64* %12
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%19 = load i32, i32* %4
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %19)
store i64 0, i64* @errno
%21 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %8
%23 = load i32*, i32** %8
%24 = icmp ne i32* %23, null
br i1 %24, label %27, label %25
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
27: ; preds = %2
store i64 0, i64* @errno
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%29 = load i32*, i32** %8
%30 = call i8* @fgets(i8* %28, i32 2048, i32* %29)
store i8* %30, i8** %7
%31 = load i32*, i32** %8
%32 = call i32 @fclose(i32* %31)
%33 = load i8*, i8** %7
%34 = icmp ne i8* %33, null
br i1 %34, label %37, label %35
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
37: ; preds = %27
%38 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%39 = call i8* @strchr(i8* %38, i8 signext 32)
store i8* %39, i8** %7
store i32 0, i32* %9
br label %40
40: ; preds = %52, %37
%41 = load i32, i32* %9
%42 = icmp slt i32 %41, 46
br i1 %42, label %43, label %55
43: ; preds = %40
%44 = load i8*, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %48, label %46
46: ; preds = %43
%47 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
48: ; preds = %43
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 1
%51 = call i8* @strchr(i8* %50, i8 signext 32)
store i8* %51, i8** %7
br label %52
52: ; preds = %48
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
store i32 %54, i32* %9
br label %40
55: ; preds = %40
%56 = load i8*, i8** %7
%57 = icmp ne i8* %56, null
br i1 %57, label %59, label %58
58: ; preds = %55
store i32 -1, i32* %3
br label %195
59: ; preds = %55
%60 = load i8*, i8** %7
%61 = call i32 @sscanf(i8* %60, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i64* %13, i64* %14)
store i32 %61, i32* %9
%62 = load i32, i32* %9
%63 = icmp ne i32 %62, 2
br i1 %63, label %64, label %66
64: ; preds = %59
%65 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
66: ; preds = %59
%67 = load i8*, i8** %5
%68 = call i32 @strlen(i8* %67)
%69 = add nsw i32 %68, 1
%70 = sext i32 %69 to i64
store i64 %70, i64* %11
%71 = load i64, i64* %11
%72 = load i64, i64* %14
%73 = load i64, i64* %13
%74 = sub i64 %72, %73
%75 = icmp ugt i64 %71, %74
br i1 %75, label %76, label %78
76: ; preds = %66
%77 = call i32 @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
78: ; preds = %66
%79 = load i64, i64* %11
%80 = load i64, i64* %14
%81 = load i64, i64* %13
%82 = sub i64 %80, %81
%83 = icmp ult i64 %79, %82
br i1 %83, label %84, label %101
84: ; preds = %78
%85 = load i64, i64* %11
%86 = trunc i64 %85 to i32
store i32 %86, i32* %9
br label %87
87: ; preds = %97, %84
%88 = load i32, i32* %9
%89 = sext i32 %88 to i64
%90 = load i64, i64* %14
%91 = load i64, i64* %13
%92 = sub i64 %90, %91
%93 = icmp ult i64 %89, %92
br i1 %93, label %94, label %100
94: ; preds = %87
%95 = load i8*, i8** %5
%96 = call i32 @strcat(i8* %95, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %97
97: ; preds = %94
%98 = load i32, i32* %9
%99 = add nsw i32 %98, 1
store i32 %99, i32* %9
br label %87
100: ; preds = %87
br label %101
101: ; preds = %100, %78
br label %102
102: ; preds = %101
store i64 0, i64* @errno
%103 = load i32, i32* @PTRACE_ATTACH
%104 = load i32, i32* %4
%105 = call i32 @ptrace(i32 %103, i32 %104, i64 0, i64 0)
%106 = icmp eq i32 %105, -1
%107 = zext i1 %106 to i32
store i32 %107, i32* %10
br i1 %106, label %108, label %110
108: ; preds = %102
%109 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
110: ; preds = %102
%111 = call i32 @wait(i32* null)
%112 = load i64, i64* %13
%113 = and i64 %112, -8
store i64 %113, i64* %15
%114 = load i64, i64* %14
%115 = and i64 %114, -8
%116 = add i64 %115, 8
store i64 %116, i64* %16
%117 = load i64, i64* %16
%118 = load i64, i64* %15
%119 = sub i64 %117, %118
%120 = udiv i64 %119, 8
store i64 %120, i64* %12
%121 = load i64, i64* %12
%122 = mul i64 %121, 8
%123 = call i64* @malloc(i64 %122)
store i64* %123, i64** %17
store i64 0, i64* @errno
store i32 0, i32* %9
br label %124
124: ; preds = %148, %110
%125 = load i32, i32* %9
%126 = sext i32 %125 to i64
%127 = load i64, i64* %12
%128 = icmp ult i64 %126, %127
br i1 %128, label %129, label %151
129: ; preds = %124
%130 = load i32, i32* @PTRACE_PEEKDATA
%131 = load i32, i32* %4
%132 = load i64, i64* %15
%133 = load i32, i32* %9
%134 = sext i32 %133 to i64
%135 = mul i64 %134, 8
%136 = add i64 %132, %135
%137 = call i32 @ptrace(i32 %130, i32 %131, i64 %136, i64 0)
store i32 %137, i32* %10
%138 = icmp eq i32 %137, -1
br i1 %138, label %139, label %141
139: ; preds = %129
%140 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
141: ; preds = %129
%142 = load i32, i32* %10
%143 = sext i32 %142 to i64
%144 = load i64*, i64** %17
%145 = load i32, i32* %9
%146 = sext i32 %145 to i64
%147 = getelementptr inbounds i64, i64* %144, i64 %146
store i64 %143, i64* %147
br label %148
148: ; preds = %141
%149 = load i32, i32* %9
%150 = add nsw i32 %149, 1
store i32 %150, i32* %9
br label %124
151: ; preds = %124
%152 = load i64*, i64** %17
%153 = bitcast i64* %152 to i8*
%154 = load i64, i64* %13
%155 = getelementptr inbounds i8, i8* %153, i64 %154
%156 = load i64, i64* %15
%157 = sub i64 0, %156
%158 = getelementptr inbounds i8, i8* %155, i64 %157
%159 = load i8*, i8** %5
%160 = call i32 @strcpy(i8* %158, i8* %159)
store i64 0, i64* @errno
store i32 0, i32* %9
br label %161
161: ; preds = %184, %151
%162 = load i32, i32* %9
%163 = sext i32 %162 to i64
%164 = load i64, i64* %12
%165 = icmp ult i64 %163, %164
br i1 %165, label %166, label %187
166: ; preds = %161
%167 = load i32, i32* @PTRACE_POKEDATA
%168 = load i32, i32* %4
%169 = load i64, i64* %15
%170 = load i32, i32* %9
%171 = sext i32 %170 to i64
%172 = mul i64 %171, 8
%173 = add i64 %169, %172
%174 = load i64*, i64** %17
%175 = load i32, i32* %9
%176 = sext i32 %175 to i64
%177 = getelementptr inbounds i64, i64* %174, i64 %176
%178 = load i64, i64* %177
%179 = call i32 @ptrace(i32 %167, i32 %168, i64 %173, i64 %178)
%180 = icmp eq i32 %179, -1
br i1 %180, label %181, label %183
181: ; preds = %166
%182 = call i32 @perror(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.10, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
183: ; preds = %166
br label %184
184: ; preds = %183
%185 = load i32, i32* %9
%186 = add nsw i32 %185, 1
store i32 %186, i32* %9
br label %161
187: ; preds = %161
store i64 0, i64* @errno
%188 = load i32, i32* @PTRACE_CONT
%189 = load i32, i32* %4
%190 = call i32 @ptrace(i32 %188, i32 %189, i64 0, i64 0)
%191 = icmp eq i32 %190, -1
br i1 %191, label %192, label %194
192: ; preds = %187
%193 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.11, i64 0, i64 0))
store i32 -1, i32* %3
br label %195
194: ; preds = %187
store i32 0, i32* %3
br label %195
195: ; preds = %194, %192, %181, %139, %108, %76, %64, %58, %46, %35, %25
%196 = load i32, i32* %3
ret i32 %196
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/%d/stat\00"
@errno = global i64 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Invalid PID specified?\00"
@.str.3 = [30 x i8] c"Error reading /proc/PID/stat.\00"
@.str.5 = [8 x i8] c"%lu %lu\00"
@.str.6 = [57 x i8] c"Can't set a title that is larger than the current one :(\00"
@.str.7 = [2 x i8] c" \00"
@PTRACE_ATTACH = global i32 0
@.str.8 = [18 x i8] c"Unable to attach.\00"
@PTRACE_PEEKDATA = global i32 0
@.str.9 = [29 x i8] c"Error setting the new title.\00"
@PTRACE_POKEDATA = global i32 0
@.str.10 = [75 x i8] c"Error setting the new title. Target potentially left in incosistent state.\00"
@PTRACE_CONT = global i32 0
@.str.11 = [56 x i8] c"Error setting the new title while resuming the process.\00"
@str.12 = [33 x i8] c"/proc/PID/strace format changed?\00"
define i32 @setproctitle(i32 %0, i8* %1) {
%3 = alloca [2048 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
store i64 0, i64* @errno
%10 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %128
14: ; preds = %2
store i64 0, i64* @errno
%15 = call i8* @fgets(i8* %6, i32 2048, i32* %10)
%16 = call i32 @fclose(i32* %10)
%17 = icmp eq i8* %15, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %128
20: ; preds = %14
%21 = call i8* @strchr(i8* %6, i8 signext 32)
br label %22
22: ; preds = %20, %28
%23 = phi i8* [ %21, %20 ], [ %30, %28 ]
%24 = phi i32 [ 0, %20 ], [ %31, %28 ]
%25 = icmp eq i8* %23, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.12, i64 0, i64 0))
br label %128
28: ; preds = %22
%29 = getelementptr inbounds i8, i8* %23, i64 1
%30 = call i8* @strchr(i8* %29, i8 signext 32)
%31 = add nuw nsw i32 %24, 1
%32 = icmp eq i32 %31, 46
br i1 %32, label %33, label %22
33: ; preds = %28
%34 = icmp eq i8* %30, null
br i1 %34, label %128, label %35
35: ; preds = %33
%36 = call i32 @sscanf(i8* %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i64* %4, i64* %5)
%37 = icmp eq i32 %36, 2
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.12, i64 0, i64 0))
br label %128
40: ; preds = %35
%41 = call i32 @strlen(i8* %1)
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = load i64, i64* %5
%45 = load i64, i64* %4
%46 = sub i64 %44, %45
%47 = icmp ult i64 %46, %43
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.6, i64 0, i64 0))
br label %128
50: ; preds = %40
%51 = icmp ugt i64 %46, %43
br i1 %51, label %52, label %60
52: ; preds = %50, %52
%53 = phi i64 [ %55, %52 ], [ %43, %50 ]
%54 = call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%55 = add i64 %53, 1
%56 = load i64, i64* %5
%57 = load i64, i64* %4
%58 = sub i64 %56, %57
%59 = icmp ugt i64 %58, %55
br i1 %59, label %52, label %60
60: ; preds = %52, %50
store i64 0, i64* @errno
%61 = load i32, i32* @PTRACE_ATTACH
%62 = call i32 @ptrace(i32 %61, i32 %0, i64 0, i64 0)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
br label %128
66: ; preds = %60
%67 = call i32 @wait(i32* null)
%68 = load i64, i64* %4
%69 = and i64 %68, -8
%70 = load i64, i64* %5
%71 = add i64 %70, 8
%72 = and i64 %71, -8
%73 = sub i64 %72, %69
%74 = lshr exact i64 %73, 3
%75 = call i64* @malloc(i64 %73)
store i64 0, i64* @errno
%76 = icmp eq i64 %73, 0
br i1 %76, label %77, label %83
77: ; preds = %66
%78 = bitcast i64* %75 to i8*
%79 = getelementptr inbounds i8, i8* %78, i64 %68
%80 = sub i64 0, %69
%81 = getelementptr inbounds i8, i8* %79, i64 %80
%82 = call i32 @strcpy(i8* %81, i8* %1)
br label %122
83: ; preds = %66
%84 = call i64 @llvm.umax.i64(i64 %74, i64 1)
br label %85
85: ; preds = %83, %94
%86 = phi i64 [ 0, %83 ], [ %97, %94 ]
%87 = load i32, i32* @PTRACE_PEEKDATA
%88 = shl nuw nsw i64 %86, 3
%89 = add i64 %88, %69
%90 = call i32 @ptrace(i32 %87, i32 %0, i64 %89, i64 0)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %94
92: ; preds = %85
%93 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
br label %128
94: ; preds = %85
%95 = sext i32 %90 to i64
%96 = getelementptr inbounds i64, i64* %75, i64 %86
store i64 %95, i64* %96
%97 = add nuw nsw i64 %86, 1
%98 = icmp eq i64 %97, %84
br i1 %98, label %99, label %85
99: ; preds = %94
%100 = load i64, i64* %4
%101 = bitcast i64* %75 to i8*
%102 = getelementptr inbounds i8, i8* %101, i64 %100
%103 = sub i64 0, %69
%104 = getelementptr inbounds i8, i8* %102, i64 %103
%105 = call i32 @strcpy(i8* %104, i8* %1)
store i64 0, i64* @errno
br i1 %76, label %122, label %106
106: ; preds = %99
%107 = call i64 @llvm.umax.i64(i64 %74, i64 1)
br label %110
108: ; preds = %110
%109 = icmp eq i64 %119, %107
br i1 %109, label %122, label %110
110: ; preds = %106, %108
%111 = phi i64 [ 0, %106 ], [ %119, %108 ]
%112 = load i32, i32* @PTRACE_POKEDATA
%113 = shl nuw nsw i64 %111, 3
%114 = add i64 %113, %69
%115 = getelementptr inbounds i64, i64* %75, i64 %111
%116 = load i64, i64* %115
%117 = call i32 @ptrace(i32 %112, i32 %0, i64 %114, i64 %116)
%118 = icmp eq i32 %117, -1
%119 = add nuw nsw i64 %111, 1
br i1 %118, label %120, label %108
120: ; preds = %110
%121 = call i32 @perror(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.10, i64 0, i64 0))
br label %128
122: ; preds = %108, %77, %99
store i64 0, i64* @errno
%123 = load i32, i32* @PTRACE_CONT
%124 = call i32 @ptrace(i32 %123, i32 %0, i64 0, i64 0)
%125 = icmp eq i32 %124, -1
br i1 %125, label %126, label %128
126: ; preds = %122
%127 = call i32 @perror(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.11, i64 0, i64 0))
br label %128
128: ; preds = %122, %33, %126, %120, %92, %64, %48, %38, %26, %18, %12
%129 = phi i32 [ -1, %26 ], [ -1, %38 ], [ -1, %48 ], [ -1, %64 ], [ -1, %92 ], [ -1, %120 ], [ -1, %126 ], [ -1, %18 ], [ -1, %12 ], [ -1, %33 ], [ 0, %122 ]
ret i32 %129
}