#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 dirent {
  char *d_name;
};
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ getpid();
int /*<<< orphan*/ *opendir(char *);
char *pid_proc_cmdline(scalar_t__);
char *pid_proc_comm(scalar_t__);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int strcmp(char *, char const *);
char *strstr(char *, char *);
scalar_t__ strtol(char *, char **, int);
int name2pid(const char *name, pid_t *pid) {
  pid_t parent = getpid();
  DIR *dir;
  if (!(dir = opendir("/proc"))) {
    sleep(2);
    if (!(dir = opendir("/proc"))) {
      fprintf(stderr, "Error: cannot open /proc directory\n");
      exit(1);
    }
  }
  struct dirent *entry;
  char *end;
  while ((entry = readdir(dir))) {
    pid_t newpid = strtol(entry->d_name, &end, 10);
    if (end == entry->d_name || *end)
      continue;
    if (newpid == parent)
      continue;
    char *comm = pid_proc_comm(newpid);
    if (comm) {
      char *ptr = strchr(comm, '\n');
      if (ptr)
        *ptr = '\0';
      if (strcmp(comm, "firejail")) {
        free(comm);
        continue;
      }
      free(comm);
    }
    char *cmd = pid_proc_cmdline(newpid);
    if (cmd) {
      char *ptr = strstr(cmd, "--name=");
      char *start = ptr;
      if (!ptr) {
        free(cmd);
        continue;
      }
      while (*ptr != ' ' && *ptr != '\t' && *ptr != '\0')
        ptr++;
      *ptr = '\0';
      int rv = strcmp(start + 7, name);
      if (rv == 0) {
        free(cmd);
        *pid = newpid;
        closedir(dir);
        return 0;
      }
      free(cmd);
    }
  }
  closedir(dir);
  return 1;
}


x86 assembly (clang, -O0):
<name2pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %rax,-0x20(%rbp)
lea    0xdfe(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    125d <name2pid+0x7d>
mov    $0x2,%edi
call   10f0 <sleep@plt>
lea    0xdda(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1258 <name2pid+0x78>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    125d <name2pid+0x7d>
jmp    1262 <name2pid+0x82>
mov    -0x28(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1440 <name2pid+0x260>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    -0x38(%rbp),%rsi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
cmp    (%rcx),%rax
je     12b3 <name2pid+0xd3>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12b8 <name2pid+0xd8>
jmp    1262 <name2pid+0x82>
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    12cb <name2pid+0xeb>
jmp    1262 <name2pid+0x82>
mov    -0x40(%rbp),%rdi
call   1040 <pid_proc_comm@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1337 <name2pid+0x157>
mov    -0x48(%rbp),%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1307 <name2pid+0x127>
mov    -0x50(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rdi
lea    0xd18(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
je     132e <name2pid+0x14e>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    1262 <name2pid+0x82>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <pid_proc_cmdline@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     143b <name2pid+0x25b>
mov    -0x58(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   1100 <strstr@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    1384 <name2pid+0x1a4>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    1262 <name2pid+0x82>
jmp    1389 <name2pid+0x1a9>
mov    -0x60(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x20,%ecx
mov    %al,-0x6d(%rbp)
je     13c3 <name2pid+0x1e3>
mov    -0x60(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x9,%ecx
mov    %al,-0x6d(%rbp)
je     13c3 <name2pid+0x1e3>
mov    -0x60(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x6d(%rbp)
mov    -0x6d(%rbp),%al
test   $0x1,%al
jne    13d3 <name2pid+0x1f3>
jmp    13e4 <name2pid+0x204>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
jmp    1389 <name2pid+0x1a9>
mov    -0x60(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x68(%rbp),%rdi
add    $0x7,%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcmp@plt>
mov    %eax,-0x6c(%rbp)
cmpl   $0x0,-0x6c(%rbp)
jne    1432 <name2pid+0x252>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    1450 <name2pid+0x270>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
jmp    1262 <name2pid+0x82>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name2pid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,0x18(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %rax,0x8(%rsp)
lea    0xdf7(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1238 <name2pid+0x58>
mov    $0x2,%edi
call   10f0 <sleep@plt>
lea    0xdd9(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,%r12
test   %rax,%rax
je     13a1 <name2pid+0x1c1>
mov    %r12,%rdi
call   10d0 <readdir@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     1388 <name2pid+0x1a8>
mov    %rax,%rbp
mov    %r14,0x10(%rsp)
lea    0x20(%rsp),%r14
mov    0x8(%rsp),%rbx
jmp    1284 <name2pid+0xa4>
mov    %r15,%rdi
call   1030 <free@plt>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   10d0 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1383 <name2pid+0x1a3>
mov    0x0(%rbp),%rdi
mov    %r14,%rsi
mov    $0xa,%edx
call   10c0 <strtol@plt>
mov    %rax,%r13
mov    0x20(%rsp),%rax
cmp    0x0(%rbp),%rax
je     1270 <name2pid+0x90>
cmpb   $0x0,(%rax)
jne    1270 <name2pid+0x90>
cmp    %rbx,%r13
je     1270 <name2pid+0x90>
mov    %r13,%rdi
call   1040 <pid_proc_comm@plt>
test   %rax,%rax
je     12f8 <name2pid+0x118>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0xa,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     12d2 <name2pid+0xf2>
movb   $0x0,(%rax)
mov    %rbp,%rdi
lea    0xd4e(%rip),%rsi
call   1090 <strcmp@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
test   %ebx,%ebx
mov    0x8(%rsp),%rbx
jne    1270 <name2pid+0x90>
mov    %r13,%rdi
call   10a0 <pid_proc_cmdline@plt>
test   %rax,%rax
je     1270 <name2pid+0x90>
mov    %rax,%r15
mov    %rax,%rdi
lea    0xd1d(%rip),%rsi
call   1100 <strstr@plt>
test   %rax,%rax
je     1262 <name2pid+0x82>
mov    %rax,%rcx
movabs $0x100000201,%rsi
jmp    1344 <name2pid+0x164>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rcx
movzbl (%rcx),%edx
cmp    $0x20,%rdx
ja     1340 <name2pid+0x160>
bt     %rdx,%rsi
jae    1340 <name2pid+0x160>
movb   $0x0,(%rcx)
add    $0x7,%rax
mov    %rax,%rdi
mov    0x18(%rsp),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
jne    1262 <name2pid+0x82>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rax
mov    %r13,(%rax)
xor    %ebx,%ebx
jmp    1388 <name2pid+0x1a8>
mov    $0x1,%ebx
mov    %r12,%rdi
call   1080 <closedir@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c38(%rip),%rax
mov    (%rax),%edi
lea    0xc55(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Error: cannot open /proc directory\0A\00"
@.str.2 = [9 x i8] c"firejail\00"
@.str.3 = [8 x i8] c"--name=\00"
define i32 @name2pid(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca %struct.dirent*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%17 = call i64 (...) @getpid()
store i64 %17, i64* %6
%18 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %7
%19 = icmp ne i32* %18, null
br i1 %19, label %29, label %20
20: ; preds = %2
%21 = call i32 @sleep(i32 2)
%22 = call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %7
%23 = icmp ne i32* %22, null
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
br label %29
29: ; preds = %28, %2
br label %30
30: ; preds = %130, %88, %71, %54, %49, %29
%31 = load i32*, i32** %7
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %8
%33 = icmp ne %struct.dirent* %32, null
br i1 %33, label %34, label %131
34: ; preds = %30
%35 = load %struct.dirent*, %struct.dirent** %8
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i64 @strtol(i8* %37, i8** %9, i32 10)
store i64 %38, i64* %10
%39 = load i8*, i8** %9
%40 = load %struct.dirent*, %struct.dirent** %8
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = icmp eq i8* %39, %42
br i1 %43, label %49, label %44
44: ; preds = %34
%45 = load i8*, i8** %9
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44, %34
br label %30
50: ; preds = %44
%51 = load i64, i64* %10
%52 = load i64, i64* %6
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %50
br label %30
55: ; preds = %50
%56 = load i64, i64* %10
%57 = call i8* @pid_proc_comm(i64 %56)
store i8* %57, i8** %11
%58 = load i8*, i8** %11
%59 = icmp ne i8* %58, null
br i1 %59, label %60, label %77
60: ; preds = %55
%61 = load i8*, i8** %11
%62 = call i8* @strchr(i8* %61, i8 signext 10)
store i8* %62, i8** %12
%63 = load i8*, i8** %12
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %67
65: ; preds = %60
%66 = load i8*, i8** %12
store i8 0, i8* %66
br label %67
67: ; preds = %65, %60
%68 = load i8*, i8** %11
%69 = call i32 @strcmp(i8* %68, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %67
%72 = load i8*, i8** %11
%73 = call i32 @free(i8* %72)
br label %30
74: ; preds = %67
%75 = load i8*, i8** %11
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %74, %55
%78 = load i64, i64* %10
%79 = call i8* @pid_proc_cmdline(i64 %78)
store i8* %79, i8** %13
%80 = load i8*, i8** %13
%81 = icmp ne i8* %80, null
br i1 %81, label %82, label %130
82: ; preds = %77
%83 = load i8*, i8** %13
%84 = call i8* @strstr(i8* %83, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i8* %84, i8** %14
%85 = load i8*, i8** %14
store i8* %85, i8** %15
%86 = load i8*, i8** %14
%87 = icmp ne i8* %86, null
br i1 %87, label %91, label %88
88: ; preds = %82
%89 = load i8*, i8** %13
%90 = call i32 @free(i8* %89)
br label %30
91: ; preds = %82
br label %92
92: ; preds = %109, %91
%93 = load i8*, i8** %14
%94 = load i8, i8* %93
%95 = sext i8 %94 to i32
%96 = icmp ne i32 %95, 32
br i1 %96, label %97, label %107
97: ; preds = %92
%98 = load i8*, i8** %14
%99 = load i8, i8* %98
%100 = sext i8 %99 to i32
%101 = icmp ne i32 %100, 9
br i1 %101, label %102, label %107
102: ; preds = %97
%103 = load i8*, i8** %14
%104 = load i8, i8* %103
%105 = sext i8 %104 to i32
%106 = icmp ne i32 %105, 0
br label %107
107: ; preds = %102, %97, %92
%108 = phi i1 [ false, %97 ], [ false, %92 ], [ %106, %102 ]
br i1 %108, label %109, label %112
109: ; preds = %107
%110 = load i8*, i8** %14
%111 = getelementptr inbounds i8, i8* %110, i32 1
store i8* %111, i8** %14
br label %92
112: ; preds = %107
%113 = load i8*, i8** %14
store i8 0, i8* %113
%114 = load i8*, i8** %15
%115 = getelementptr inbounds i8, i8* %114, i64 7
%116 = load i8*, i8** %4
%117 = call i32 @strcmp(i8* %115, i8* %116)
store i32 %117, i32* %16
%118 = load i32, i32* %16
%119 = icmp eq i32 %118, 0
br i1 %119, label %120, label %127
120: ; preds = %112
%121 = load i8*, i8** %13
%122 = call i32 @free(i8* %121)
%123 = load i64, i64* %10
%124 = load i64*, i64** %5
store i64 %123, i64* %124
%125 = load i32*, i32** %7
%126 = call i32 @closedir(i32* %125)
store i32 0, i32* %3
br label %134
127: ; preds = %112
%128 = load i8*, i8** %13
%129 = call i32 @free(i8* %128)
br label %130
130: ; preds = %127, %77
br label %30
131: ; preds = %30
%132 = load i32*, i32** %7
%133 = call i32 @closedir(i32* %132)
store i32 1, i32* %3
br label %134
134: ; preds = %131, %120
%135 = load i32, i32* %3
ret i32 %135
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [6 x i8] c"/proc\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"Error: cannot open /proc directory\0A\00"
@.str.2 = [9 x i8] c"firejail\00"
@.str.3 = [8 x i8] c"--name=\00"
define i32 @name2pid(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = alloca i8*
%4 = tail call i64 (...) @getpid()
%5 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %15
7: ; preds = %2
%8 = tail call i32 @sleep(i32 2)
%9 = tail call i32* @opendir(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
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 ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %7, %2
%16 = phi i32* [ %5, %2 ], [ %9, %7 ]
%17 = bitcast i8** %3 to i8*
%18 = tail call %struct.dirent* @readdir(i32* %16)
%19 = icmp eq %struct.dirent* %18, null
br i1 %19, label %66, label %20
20: ; preds = %15, %63
%21 = phi %struct.dirent* [ %64, %63 ], [ %18, %15 ]
%22 = getelementptr inbounds %struct.dirent, %struct.dirent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = call i64 @strtol(i8* %23, i8** %3, i32 10)
%25 = load i8*, i8** %3
%26 = load i8*, i8** %22
%27 = icmp eq i8* %25, %26
br i1 %27, label %63, label %28
28: ; preds = %20
%29 = load i8, i8* %25
%30 = icmp ne i8 %29, 0
%31 = icmp eq i64 %24, %4
%32 = select i1 %30, i1 true, i1 %31
br i1 %32, label %63, label %33
33: ; preds = %28
%34 = tail call i8* @pid_proc_comm(i64 %24)
%35 = icmp eq i8* %34, null
br i1 %35, label %44, label %36
36: ; preds = %33
%37 = tail call i8* @strchr(i8* %34, i8 signext 10)
%38 = icmp eq i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %36
store i8 0, i8* %37
br label %40
40: ; preds = %39, %36
%41 = tail call i32 @strcmp(i8* %34, i8* dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%42 = icmp eq i32 %41, 0
%43 = tail call i32 @free(i8* %34)
br i1 %42, label %44, label %63
44: ; preds = %40, %33
%45 = tail call i8* @pid_proc_cmdline(i64 %24)
%46 = icmp eq i8* %45, null
br i1 %46, label %63, label %47
47: ; preds = %44
%48 = tail call i8* @strstr(i8* %45, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%49 = icmp eq i8* %48, null
br i1 %49, label %61, label %50
50: ; preds = %47, %53
%51 = phi i8* [ %54, %53 ], [ %48, %47 ]
%52 = load i8, i8* %51
switch i8 %52, label %53 [
i8 32, label %55
i8 9, label %55
i8 0, label %55
]
53: ; preds = %50
%54 = getelementptr inbounds i8, i8* %51, i64 1
br label %50
55: ; preds = %50, %50, %50
store i8 0, i8* %51
%56 = getelementptr inbounds i8, i8* %48, i64 7
%57 = tail call i32 @strcmp(i8* %56, i8* dereferenceable(1) %0)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = tail call i32 @free(i8* %45)
store i64 %24, i64* %1
br label %66
61: ; preds = %55, %47
%62 = tail call i32 @free(i8* %45)
br label %63
63: ; preds = %61, %40, %44, %20, %28
%64 = tail call %struct.dirent* @readdir(i32* %16)
%65 = icmp eq %struct.dirent* %64, null
br i1 %65, label %66, label %20
66: ; preds = %63, %15, %59
%67 = phi i32 [ 0, %59 ], [ 1, %15 ], [ 1, %63 ]
%68 = tail call i32 @closedir(i32* %16)
ret i32 %67
}