#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct hostent {
  char *h_addr;
  int /*<<< orphan*/ h_length;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr_in *, int);
int /*<<< orphan*/ bcopy(void *, void *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr_in *, int);
int /*<<< orphan*/ bzero(void *, int);
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
main(int ac, char *av[]) {
  if (ac != 3) {
    {
      perror("Usage: myfingerd <username> <hostname>");
      exit(1);
    };
  }
  struct sockaddr_in saddr;
  struct hostent *hp;
  int sock_id, sock_fd, pid, saddr_len;
  sock_id = socket(PF_INET, SOCK_STREAM, 0);
  if (sock_id == -1) {
    {
      perror("socket");
      exit(1);
    };
  }
  bzero((void *)&saddr, sizeof(&saddr));
  hp = gethostbyname(av[2]);
  bcopy((void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);
  saddr.sin_port = htons(2345);
  saddr.sin_family = AF_INET;
  printf(hp->h_addr);
  if (bind(sock_id, (struct sockaddr_in *)&saddr, sizeof(saddr)) != 0) {
    {
      perror("bind");
      exit(1);
    };
  }
  while (1) {
    sock_fd = accept(sock_id, (struct sockaddr_in *)&saddr, sizeof(saddr));
    if (sock_fd < 0) {
      {
        perror("accept");
        exit(1);
      };
    }
    printf("connection from %s\n", inet_ntoa(saddr.sin_addr));
    pid = fork();
    if (pid != 0) {
      if (dup2(sock_id, 1) < 0 || dup2(sock_id, 2) < 0) {
        {
          perror("dup2");
          exit(1);
        };
      }
      close(sock_id);
      execl("usr/bin/finger", "finger", "--", av[1], NULL);
      {
        perror("usr/bin/finger");
        exit(1);
      };
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1226 <main+0x36>
lea    0xde9(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1262 <main+0x72>
lea    0xdd4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <bcopy@plt>
mov    $0x929,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     12f0 <main+0x100>
lea    0xd4d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12f5 <main+0x105>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <accept@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1329 <main+0x139>
lea    0xd19(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     13df <main+0x1ef>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jl     1384 <main+0x194>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    139a <main+0x1aa>
lea    0xcc3(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xca2(%rip),%rdi
lea    0xca3(%rip),%rsi
lea    0xca3(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   10f0 <execl@plt>
lea    0xc83(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    12f5 <main+0x105>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    122e <main+0x3e>
mov    %rsi,%r15
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
jne    123a <main+0x4a>
lea    0xdfe(%rip),%rdi
jmp    1352 <main+0x162>
lea    0xdcb(%rip),%rdi
jmp    1352 <main+0x162>
mov    %eax,%r12d
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    0x10(%r15),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,%rbx
mov    (%rax),%rsi
mov    0x8(%rax),%edx
mov    %r14,%rdi
call   10a0 <memmove@plt>
mov    $0x929,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rbx),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    1311 <main+0x121>
mov    %rsp,%rbx
lea    0xd8f(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <accept@plt>
test   %eax,%eax
js     131a <main+0x12a>
mov    (%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12b0 <main+0xc0>
mov    %r12d,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     1308 <main+0x118>
mov    %r12d,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
test   %rax,%rax
jns    1323 <main+0x133>
lea    0xd3f(%rip),%rdi
jmp    1352 <main+0x162>
lea    0xd16(%rip),%rdi
jmp    1352 <main+0x162>
lea    0xd12(%rip),%rdi
jmp    1352 <main+0x162>
mov    %r12d,%edi
call   1070 <close@plt>
mov    0x8(%r15),%rcx
lea    0xd1d(%rip),%rbx
lea    0xd1e(%rip),%rsi
lea    0xd1e(%rip),%rdx
mov    %rbx,%rdi
xor    %r8d,%r8d
call   10f0 <execl@plt>
mov    %rbx,%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i8*, i32 }
@.str = [39 x i8] c"Usage: myfingerd <username> <hostname>\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [20 x i8] c"connection from %s\0A\00"
@.str.5 = [5 x i8] c"dup2\00"
@.str.6 = [15 x i8] c"usr/bin/finger\00"
@.str.7 = [7 x i8] c"finger\00"
@.str.8 = [3 x i8] c"--\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %17
%27 = bitcast %struct.sockaddr_in* %6 to i8*
%28 = call i32 @bzero(i8* %27, i32 8)
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call %struct.hostent* @gethostbyname(i8* %31)
store %struct.hostent* %32, %struct.hostent** %7
%33 = load %struct.hostent*, %struct.hostent** %7
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%37 = bitcast i32* %36 to i8*
%38 = load %struct.hostent*, %struct.hostent** %7
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i32, i32* %39
%41 = call i32 @bcopy(i8* %35, i8* %37, i32 %40)
%42 = call i32 @htons(i32 2345)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %42, i32* %43
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %44, i32* %45
%46 = load %struct.hostent*, %struct.hostent** %7
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 0
%48 = load i8*, i8** %47
%49 = call i32 (i8*, ...) @printf(i8* %48)
%50 = load i32, i32* %8
%51 = call i64 @bind(i32 %50, %struct.sockaddr_in* %6, i32 12)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %26
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %26
br label %57
57: ; preds = %56, %93
%58 = load i32, i32* %8
%59 = call i32 @accept(i32 %58, %struct.sockaddr_in* %6, i32 12)
store i32 %59, i32* %9
%60 = load i32, i32* %9
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %57
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i8* @inet_ntoa(i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %68)
%70 = call i32 (...) @fork()
store i32 %70, i32* %10
%71 = load i32, i32* %10
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %93
73: ; preds = %65
%74 = load i32, i32* %8
%75 = call i64 @dup2(i32 %74, i32 1)
%76 = icmp slt i64 %75, 0
br i1 %76, label %81, label %77
77: ; preds = %73
%78 = load i32, i32* %8
%79 = call i64 @dup2(i32 %78, i32 2)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %77, %73
%82 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %77
%85 = load i32, i32* %8
%86 = call i32 @close(i32 %85)
%87 = load i8**, i8*** %5
%88 = getelementptr inbounds i8*, i8** %87, i64 1
%89 = load i8*, i8** %88
%90 = call i32 @execl(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %89, i32* null)
%91 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %65
br label %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i8*, i32 }
@.str = [39 x i8] c"Usage: myfingerd <username> <hostname>\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [20 x i8] c"connection from %s\0A\00"
@.str.5 = [5 x i8] c"dup2\00"
@.str.6 = [15 x i8] c"usr/bin/finger\00"
@.str.7 = [7 x i8] c"finger\00"
@.str.8 = [3 x i8] c"--\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = bitcast %struct.sockaddr_in* %3 to i8*
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = call i32 @bzero(i8* %9, i32 8)
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call %struct.hostent* @gethostbyname(i8* %20)
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 0, i32 1
%26 = load i32, i32* %25
%27 = tail call i32 @htons(i32 2345)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i8*, i8** %22
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %31)
%33 = call i64 @bind(i32 %12, %struct.sockaddr_in* %3, i32 12)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %17
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %17, %44
%39 = call i32 @accept(i32 %12, %struct.sockaddr_in* %3, i32 12)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %38
%45 = load i32, i32* %24
%46 = call i8* @inet_ntoa(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %46)
%48 = call i32 (...) @fork()
%49 = icmp eq i32 %48, 0
br i1 %49, label %38, label %50
50: ; preds = %44
%51 = call i64 @dup2(i32 %12, i32 1)
%52 = icmp slt i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %50
%54 = call i64 @dup2(i32 %12, i32 2)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53, %50
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %53
%60 = call i32 @close(i32 %12)
%61 = getelementptr inbounds i8*, i8** %1, i64 1
%62 = load i8*, i8** %61
%63 = call i32 @execl(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %62, i32* null)
%64 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
}