#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*/ SIGUSR1;
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int sscanf(char *, char *, int *, int *, int *, int *, int *, int *, int *,
           int *, int *, int *);
int signal_tcp_proxy(char *proc_name) {
  FILE *fp;
  char *cptr;
  char buf[2048];
  int pids[10];
  int status = 0;
  int num_procs;
  int i;
  sprintf(buf, "pidof %s", proc_name);
  if ((fp = popen(buf, "r")) == NULL) {
    printf("xdemo: popen() failed\n");
    return -1;
  }
  cptr = fgets(buf, 2047, fp);
  if (cptr == NULL) {
    pclose(fp);
    return -1;
  }
  num_procs = sscanf(buf, "%d %d %d %d %d %d %d %d %d %d", &pids[0], &pids[1],
                     &pids[2], &pids[3], &pids[4], &pids[5], &pids[6], &pids[7],
                     &pids[8], &pids[9]);
  if (num_procs > 0) {
    for (i = 0; i < num_procs; i++) {
      kill(pids[i], SIGUSR1);
      printf("sent SIGUSR1 to process %d\n", pids[i]);
    }
  }
  pclose(fp);
  return status;
}


x86 assembly (clang, -O0):
<signal_tcp_proxy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x898,%rsp
mov    %rdi,-0x28(%rbp)
movl   $0x0,-0x874(%rbp)
lea    -0x840(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe60(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x840(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11e0 <signal_tcp_proxy+0x70>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1345 <signal_tcp_proxy+0x1d5>
lea    -0x840(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x7ff,%esi
call   1050 <fgets@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1219 <signal_tcp_proxy+0xa9>
mov    -0x30(%rbp),%rdi
call   1040 <pclose@plt>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1345 <signal_tcp_proxy+0x1d5>
lea    -0x840(%rbp),%rdi
lea    -0x870(%rbp),%rdx
lea    -0x870(%rbp),%rcx
add    $0x4,%rcx
lea    -0x870(%rbp),%r8
add    $0x8,%r8
lea    -0x870(%rbp),%r9
add    $0xc,%r9
lea    -0x870(%rbp),%r15
add    $0x10,%r15
lea    -0x870(%rbp),%r14
add    $0x14,%r14
lea    -0x870(%rbp),%rbx
add    $0x18,%rbx
lea    -0x870(%rbp),%r11
add    $0x1c,%r11
lea    -0x870(%rbp),%r10
add    $0x20,%r10
lea    -0x870(%rbp),%rax
add    $0x24,%rax
lea    0xd91(%rip),%rsi
mov    %r15,(%rsp)
mov    %r14,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    %r11,0x18(%rsp)
mov    %r10,0x20(%rsp)
mov    %rax,0x28(%rsp)
call   1070 <sscanf@plt>
mov    %eax,-0x878(%rbp)
cmpl   $0x0,-0x878(%rbp)
jle    1333 <signal_tcp_proxy+0x1c3>
movl   $0x0,-0x87c(%rbp)
mov    -0x87c(%rbp),%eax
cmp    -0x878(%rbp),%eax
jge    132e <signal_tcp_proxy+0x1be>
movslq -0x87c(%rbp),%rax
mov    -0x870(%rbp,%rax,4),%edi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
call   1060 <kill@plt>
movslq -0x87c(%rbp),%rax
mov    -0x870(%rbp,%rax,4),%esi
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x87c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x87c(%rbp)
jmp    12d0 <signal_tcp_proxy+0x160>
jmp    1333 <signal_tcp_proxy+0x1c3>
mov    -0x30(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x874(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x898,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_tcp_proxy>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x830,%rsp
mov    %rdi,%rdx
lea    0xe66(%rip),%rsi
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
lea    0xe5b(%rip),%rsi
mov    %rbx,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     127e <signal_tcp_proxy+0xfe>
mov    %rax,%r14
lea    0x30(%rsp),%rdi
mov    $0x7ff,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     128c <signal_tcp_proxy+0x10c>
lea    0x4(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
lea    0x10(%rsp),%r10
lea    0x14(%rsp),%r11
lea    0x18(%rsp),%r15
lea    0x1c(%rsp),%r12
lea    0x20(%rsp),%rax
lea    0x24(%rsp),%rbx
lea    0xdfa(%rip),%rsi
lea    0x30(%rsp),%rdi
mov    %rsp,%rdx
push   %rbx
push   %rax
push   %r12
push   %r15
push   %r11
push   %r10
call   1080 <sscanf@plt>
add    $0x30,%rsp
test   %eax,%eax
jle    1272 <signal_tcp_proxy+0xf2>
mov    %eax,%r13d
mov    0x2dae(%rip),%r12
lea    0xde8(%rip),%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rsp,%rbx,4),%edi
mov    (%r12),%esi
call   1070 <kill@plt>
mov    (%rsp,%rbx,4),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1250 <signal_tcp_proxy+0xd0>
mov    %r14,%rdi
call   1050 <pclose@plt>
xor    %eax,%eax
jmp    1299 <signal_tcp_proxy+0x119>
lea    0xdc0(%rip),%rdi
call   1030 <puts@plt>
jmp    1294 <signal_tcp_proxy+0x114>
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    $0xffffffff,%eax
add    $0x830,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c"pidof %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"xdemo: popen() failed\0A\00"
@.str.3 = [30 x i8] c"%d %d %d %d %d %d %d %d %d %d\00"
@SIGUSR1 = global i32 0
@.str.4 = [28 x i8] c"sent SIGUSR1 to process %d\0A\00"
define i32 @signal_tcp_proxy(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [2048 x i8]
%7 = alloca [10 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %8
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%15 = call i32* @popen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %4
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %68
19: ; preds = %1
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%21 = load i32*, i32** %4
%22 = call i8* @fgets(i8* %20, i32 2047, i32* %21)
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32*, i32** %4
%27 = call i32 @pclose(i32* %26)
store i32 -1, i32* %2
br label %68
28: ; preds = %19
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 0
%31 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 1
%32 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 2
%33 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 3
%34 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 4
%35 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 5
%36 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 6
%37 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 7
%38 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 8
%39 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 9
%40 = call i32 @sscanf(i8* %29, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32* %30, i32* %31, i32* %32, i32* %33, i32* %34, i32* %35, i32* %36, i32* %37, i32* %38, i32* %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %64
43: ; preds = %28
store i32 0, i32* %10
br label %44
44: ; preds = %60, %43
%45 = load i32, i32* %10
%46 = load i32, i32* %9
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %63
48: ; preds = %44
%49 = load i32, i32* %10
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %50
%52 = load i32, i32* %51
%53 = load i32, i32* @SIGUSR1
%54 = call i32 @kill(i32 %52, i32 %53)
%55 = load i32, i32* %10
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 %56
%58 = load i32, i32* %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %48
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %44
63: ; preds = %44
br label %64
64: ; preds = %63, %28
%65 = load i32*, i32** %4
%66 = call i32 @pclose(i32* %65)
%67 = load i32, i32* %8
store i32 %67, i32* %2
br label %68
68: ; preds = %64, %25, %17
%69 = load i32, i32* %2
ret i32 %69
}

LLVM IR (-O2):
@.str = [9 x i8] c"pidof %s\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [30 x i8] c"%d %d %d %d %d %d %d %d %d %d\00"
@SIGUSR1 = global i32 0
@.str.4 = [28 x i8] c"sent SIGUSR1 to process %d\0A\00"
@str = [22 x i8] c"xdemo: popen() failed\00"
define i32 @signal_tcp_proxy(i8* %0) {
%2 = alloca [2048 x i8]
%3 = alloca [10 x i32]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = bitcast [10 x i32]* %3 to i8*
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %43
11: ; preds = %1
%12 = call i8* @fgets(i8* %4, i32 2047, i32* %7)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @pclose(i32* %7)
br label %43
16: ; preds = %11
%17 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 0
%18 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 1
%19 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 2
%20 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 3
%21 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 4
%22 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 5
%23 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 6
%24 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 7
%25 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 8
%26 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 9
%27 = call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32* %17, i32* %18, i32* %19, i32* %20, i32* %21, i32* %22, i32* %23, i32* %24, i32* %25, i32* %26)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %41
29: ; preds = %16
%30 = zext i32 %27 to i64
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ 0, %29 ], [ %39, %31 ]
%33 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i64 0, i64 %32
%34 = load i32, i32* %33
%35 = load i32, i32* @SIGUSR1
%36 = call i32 @kill(i32 %34, i32 %35)
%37 = load i32, i32* %33
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = add nuw nsw i64 %32, 1
%40 = icmp eq i64 %39, %30
br i1 %40, label %41, label %31
41: ; preds = %31, %16
%42 = call i32 @pclose(i32* %7)
br label %43
43: ; preds = %41, %14, %9
%44 = phi i32 [ -1, %9 ], [ -1, %14 ], [ 0, %41 ]
ret i32 %44
}