#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 */
typedef struct TYPE_6__ TYPE_3__;
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_5__ {
  char *pointer;
  int length;
};
struct TYPE_6__ {
  TYPE_2__ essid;
};
struct TYPE_4__ {
  int /*<<< orphan*/ ifrn_name;
};
struct iwreq {
  TYPE_3__ u;
  TYPE_1__ ifr_ifrn;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIWESSID;
int /*<<< orphan*/ SOCK_DGRAM;
char *calloc(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int ioctl(int, int /*<<< orphan*/, struct iwreq *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
char *strerror(int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Bad usage\n");
    fprintf(stderr, "Usage: %s ifname\n", argv[0]);
    return -1;
  }
  struct iwreq req;
  strcpy(req.ifr_ifrn.ifrn_name, argv[1]);
  int fd, status;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  char *buffer;
  buffer = calloc(32, sizeof(char));
  req.u.essid.pointer = buffer;
  req.u.essid.length = 32;
  if (ioctl(fd, SIOCGIWESSID, &req) == -1) {
    fprintf(stderr, "Failed ESSID get on interface %s: %s\n", argv[1],
            strerror(errno));
  } else {
    printf("%s\n", (char *)req.u.essid.pointer);
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e1 <main+0x61>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <main+0x129>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1040 <strcpy@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x20,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x20,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1290 <main+0x110>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    12a0 <main+0x120>
mov    -0x28(%rbp),%rsi
lea    0xda4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1200 <main+0x80>
mov    0x8(%rbx),%rsi
call   1040 <strcpy@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    $0x20,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r14
mov    %rax,0x8(%rsp)
movl   $0x20,0x10(%rsp)
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1233 <main+0xb3>
mov    0x8(%rsp),%rsi
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
jmp    1264 <main+0xe4>
mov    0x2dc1(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xdef(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    (%rbx),%rdx
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    126e <main+0xee>
mov    0x2d8e(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rbx),%rbx
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iwreq = type { %struct.TYPE_6__, %struct.TYPE_4__ }
%struct.TYPE_6__ = type { %struct.TYPE_5__ }
%struct.TYPE_5__ = type { i8*, i32 }
%struct.TYPE_4__ = type { i32 }
@stderr = global i32 0
@.str = [11 x i8] c"Bad usage\0A\00"
@.str.1 = [18 x i8] c"Usage: %s ifname\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIWESSID = global i32 0
@.str.2 = [38 x i8] c"Failed ESSID get on interface %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.iwreq
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %60
20: ; preds = %2
%21 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %6, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strcpy(i32 %23, i8* %26)
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_DGRAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
store i32 %30, i32* %7
%31 = call i8* @calloc(i32 32, i32 1)
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %6, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %33, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %34, i32 0, i32 0
store i8* %32, i8** %35
%36 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %6, i32 0, i32 0
%37 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %36, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %37, i32 0, i32 1
store i32 32, i32* %38
%39 = load i32, i32* %7
%40 = load i32, i32* @SIOCGIWESSID
%41 = call i32 @ioctl(i32 %39, i32 %40, %struct.iwreq* %6)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %51
43: ; preds = %20
%44 = load i32, i32* @stderr
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %47, i8* %49)
br label %57
51: ; preds = %20
%52 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %6, i32 0, i32 0
%53 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %52, i32 0, i32 0
%54 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %51, %43
%58 = load i8*, i8** %9
%59 = call i32 @free(i8* %58)
br label %60
60: ; preds = %57, %12
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
%struct.iwreq = type { %struct.TYPE_6__, %struct.TYPE_4__ }
%struct.TYPE_6__ = type { %struct.TYPE_5__ }
%struct.TYPE_5__ = type { i8*, i32 }
%struct.TYPE_4__ = type { i32 }
@stderr = global i32 0
@.str = [11 x i8] c"Bad usage\0A\00"
@.str.1 = [18 x i8] c"Usage: %s ifname\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIWESSID = global i32 0
@.str.2 = [38 x i8] c"Failed ESSID get on interface %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.iwreq
%4 = icmp eq i32 %0, 2
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %36
11: ; preds = %2
%12 = bitcast %struct.iwreq* %3 to i8*
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @strcpy(i32 undef, i8* %14)
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = tail call dereferenceable_or_null(32) i8* @calloc(i32 32, i32 1)
%20 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 0, i32 0, i32 0
store i8* %19, i8** %20
%21 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %3, i64 0, i32 0, i32 0, i32 1
store i32 32, i32* %21
%22 = load i32, i32* @SIOCGIWESSID
%23 = call i32 @ioctl(i32 %18, i32 %22, %struct.iwreq* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %11
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %13
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %27, i8* %29)
br label %34
31: ; preds = %11
%32 = load i8*, i8** %20
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %25
%35 = call i32 @free(i8* %19)
br label %36
36: ; preds = %34, %5
%37 = phi i32 [ -1, %5 ], [ 0, %34 ]
ret i32 %37
}