#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 */
struct ps_prochandle {
  int dummy;
};

/* Variables and functions */
struct ps_prochandle *Pgrab(int /*<<< orphan*/);
struct ps_prochandle *Pgrab_core(char *, char *);
int /*<<< orphan*/ Prelease(struct ps_prochandle *);
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ init_libproc(int);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  struct ps_prochandle *ph;
  init_libproc(1);
  switch (argc) {
  case 2: {
    ph = Pgrab(atoi(argv[1]));
    break;
  }
  case 3: {
    ph = Pgrab_core(argv[1], argv[2]);
    break;
  }
  default: {
    printf("usage %s <pid> or %s <exec file> <core file>\n");
    return 1;
  }
  }
  if (ph) {
    Prelease(ph);
    return 0;
  } else {
    printf("can't connect to debuggee\n");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1,%edi
call   1050 <init_libproc@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x2,%eax
je     11a5 <main+0x45>
jmp    1194 <main+0x34>
mov    -0x1c(%rbp),%eax
sub    $0x3,%eax
je     11c2 <main+0x62>
jmp    11e0 <main+0x80>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1070 <Pgrab@plt>
mov    %rax,-0x18(%rbp)
jmp    11f8 <main+0x98>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1040 <Pgrab_core@plt>
mov    %rax,-0x18(%rbp)
jmp    11f8 <main+0x98>
lea    0xe19(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    122b <main+0xcb>
cmpq   $0x0,-0x18(%rbp)
je     1218 <main+0xb8>
mov    -0x18(%rbp),%rdi
call   1030 <Prelease@plt>
movl   $0x0,-0x4(%rbp)
jmp    122b <main+0xcb>
lea    0xe0f(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1,%edi
call   1060 <init_libproc@plt>
cmp    $0x3,%ebp
je     11ad <main+0x3d>
cmp    $0x2,%ebp
jne    11cd <main+0x5d>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1080 <Pgrab@plt>
test   %rax,%rax
je     11bf <main+0x4f>
mov    %rax,%rdi
call   1030 <Prelease@plt>
xor    %eax,%eax
jmp    11de <main+0x6e>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
call   1050 <Pgrab_core@plt>
test   %rax,%rax
jne    11a1 <main+0x31>
lea    0xe68(%rip),%rdi
call   1040 <puts@plt>
jmp    11d9 <main+0x69>
lea    0xe2c(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.ps_prochandle = type { i32 }
@.str = [46 x i8] c"usage %s <pid> or %s <exec file> <core file>\0A\00"
@.str.1 = [27 x i8] c"can't connect to debuggee\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.ps_prochandle*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 @init_libproc(i32 1)
%8 = load i32, i32* %4
switch i32 %8, label %23 [
i32 2, label %9
i32 3, label %15
]
9: ; preds = %2
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @atoi(i8* %12)
%14 = call %struct.ps_prochandle* @Pgrab(i32 %13)
store %struct.ps_prochandle* %14, %struct.ps_prochandle** %6
br label %25
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call %struct.ps_prochandle* @Pgrab_core(i8* %18, i8* %21)
store %struct.ps_prochandle* %22, %struct.ps_prochandle** %6
br label %25
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %33
25: ; preds = %15, %9
%26 = load %struct.ps_prochandle*, %struct.ps_prochandle** %6
%27 = icmp ne %struct.ps_prochandle* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load %struct.ps_prochandle*, %struct.ps_prochandle** %6
%30 = call i32 @Prelease(%struct.ps_prochandle* %29)
store i32 0, i32* %3
br label %33
31: ; preds = %25
%32 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %33
33: ; preds = %31, %28, %23
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.ps_prochandle = type { i32 }
@.str = [46 x i8] c"usage %s <pid> or %s <exec file> <core file>\0A\00"
@str = [26 x i8] c"can't connect to debuggee\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @init_libproc(i32 1)
switch i32 %0, label %15 [
i32 2, label %4
i32 3, label %9
]
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = tail call %struct.ps_prochandle* @Pgrab(i32 %7)
br label %17
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call %struct.ps_prochandle* @Pgrab_core(i8* %11, i8* %13)
br label %17
15: ; preds = %2
%16 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %24
17: ; preds = %9, %4
%18 = phi %struct.ps_prochandle* [ %14, %9 ], [ %8, %4 ]
%19 = icmp eq %struct.ps_prochandle* %18, null
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @Prelease(%struct.ps_prochandle* %18)
br label %24
22: ; preds = %17
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %22, %20, %15
%25 = phi i32 [ 1, %15 ], [ 0, %20 ], [ 1, %22 ]
ret i32 %25
}