#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 servent {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ atoi(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
struct servent *getservbyname(char *, char *);
struct servent *getservbyport(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ printit(struct servent *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  struct servent *result;
  char proto[4];
  short inport;
  int k;
  if (argc < 3) {
    printf("\nUSAGE: %s Service PortNumber\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  inport = (short)atoi(argv[2]);
  printf("\nChecking things out for service = %s, port = %d\n\n", argv[1],
         inport);
  strcpy(proto, "tcp");
  for (k = 0; k < 2; k++) {
    if (k > 0)
      strcpy(proto, "udp");
    printf("\n     Doing getservbyname for proto = %s\n\n", proto);
    if (!(result = getservbyname(argv[1], proto))) {
      perror("getservbyname");
      exit(errno);
    };
    printit(result);
    printf("\n     Doing getservbyport for proto = %s\n\n", proto);
    if (!(result = getservbyport(htons(inport), proto))) {
      perror("getservbyport");
      exit(errno);
    };
    printit(result);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11d3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %ax,-0x1e(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
movswl -0x1e(%rbp),%edx
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x1c(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x24(%rbp)
jge    12f2 <main+0x162>
cmpl   $0x0,-0x24(%rbp)
jle    1239 <main+0xa9>
lea    -0x1c(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1c(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1060 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1284 <main+0xf4>
lea    0xe14(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1070 <printit@plt>
lea    -0x1c(%rbp),%rsi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movswl -0x1e(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,%edi
lea    -0x1c(%rbp),%rsi
call   10b0 <getservbyport@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12db <main+0x14b>
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1070 <printit@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1215 <main+0x85>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11bf <main+0x2f>
mov    (%rbx),%rsi
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %rax,%r14
mov    0x8(%rbx),%rsi
movswl %r14w,%edx
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6a(%rip),%rsi
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
lea    0xe5e(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
mov    %rbp,%rsi
call   1060 <getservbyname@plt>
test   %rax,%rax
je     128e <main+0xfe>
mov    %rax,%rdi
call   1070 <printit@plt>
lea    0xe6d(%rip),%rdi
lea    0xc(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movswl %r14w,%r14d
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   10b0 <getservbyport@plt>
test   %rax,%rax
je     12cc <main+0x13c>
mov    %rax,%rdi
call   1070 <printit@plt>
lea    0xdf7(%rip),%rsi
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
lea    0xde7(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rdi
mov    %rbp,%rsi
call   1060 <getservbyname@plt>
test   %rax,%rax
jne    1297 <main+0x107>
lea    0xdf0(%rip),%rdi
jmp    12d3 <main+0x143>
mov    %rax,%rdi
call   1070 <printit@plt>
lea    0xded(%rip),%rdi
lea    0xc(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   10b0 <getservbyport@plt>
test   %rax,%rax
jne    12e6 <main+0x156>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    %rax,%rdi
call   1070 <printit@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.servent = type { i32 }
@.str = [31 x i8] c"\0AUSAGE: %s Service PortNumber\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [51 x i8] c"\0AChecking things out for service = %s, port = %d\0A\0A\00"
@.str.2 = [4 x i8] c"tcp\00"
@.str.3 = [4 x i8] c"udp\00"
@.str.4 = [43 x i8] c"\0A Doing getservbyname for proto = %s\0A\0A\00"
@.str.5 = [14 x i8] c"getservbyname\00"
@errno = global i32 0
@.str.6 = [43 x i8] c"\0A Doing getservbyport for proto = %s\0A\0A\00"
@.str.7 = [14 x i8] c"getservbyport\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.servent*
%7 = alloca [4 x i8]
%8 = alloca i16
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i64 @atoi(i8* %22)
%24 = trunc i64 %23 to i16
store i16 %24, i16* %8
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = load i16, i16* %8
%29 = sext i16 %28 to i32
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %27, i32 %29)
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%32 = call i32 @strcpy(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %9
br label %33
33: ; preds = %72, %19
%34 = load i32, i32* %9
%35 = icmp slt i32 %34, 2
br i1 %35, label %36, label %75
36: ; preds = %33
%37 = load i32, i32* %9
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%41 = call i32 @strcpy(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %39, %36
%43 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%44 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%49 = call %struct.servent* @getservbyname(i8* %47, i8* %48)
store %struct.servent* %49, %struct.servent** %6
%50 = icmp ne %struct.servent* %49, null
br i1 %50, label %55, label %51
51: ; preds = %42
%52 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%53 = load i32, i32* @errno
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %42
%56 = load %struct.servent*, %struct.servent** %6
%57 = call i32 @printit(%struct.servent* %56)
%58 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%59 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i8* %58)
%60 = load i16, i16* %8
%61 = call i32 @htons(i16 signext %60)
%62 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%63 = call %struct.servent* @getservbyport(i32 %61, i8* %62)
store %struct.servent* %63, %struct.servent** %6
%64 = icmp ne %struct.servent* %63, null
br i1 %64, label %69, label %65
65: ; preds = %55
%66 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%67 = load i32, i32* @errno
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %55
%70 = load %struct.servent*, %struct.servent** %6
%71 = call i32 @printit(%struct.servent* %70)
br label %72
72: ; preds = %69
%73 = load i32, i32* %9
%74 = add nsw i32 %73, 1
store i32 %74, i32* %9
br label %33
75: ; preds = %33
%76 = load i32, i32* @EXIT_SUCCESS
%77 = call i32 @exit(i32 %76)
unreachable
}

LLVM IR (-O2):
%struct.servent = type { i32 }
@.str = [31 x i8] c"\0AUSAGE: %s Service PortNumber\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [51 x i8] c"\0AChecking things out for service = %s, port = %d\0A\0A\00"
@.str.2 = [4 x i8] c"tcp\00"
@.str.3 = [4 x i8] c"udp\00"
@.str.4 = [43 x i8] c"\0A Doing getservbyname for proto = %s\0A\0A\00"
@.str.5 = [14 x i8] c"getservbyname\00"
@errno = global i32 0
@.str.6 = [43 x i8] c"\0A Doing getservbyport for proto = %s\0A\0A\00"
@.str.7 = [14 x i8] c"getservbyport\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i64 @atoi(i8* %13)
%15 = trunc i64 %14 to i16
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = sext i16 %15 to i32
%19 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %17, i32 %18)
%20 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%22 = load i8*, i8** %16
%23 = call %struct.servent* @getservbyname(i8* %22, i8* %4)
%24 = icmp eq %struct.servent* %23, null
br i1 %24, label %25, label %29
25: ; preds = %39, %11
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%27 = load i32, i32* @errno
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %11
%30 = call i32 @printit(%struct.servent* %23)
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%32 = call i32 @htons(i16 signext %15)
%33 = call %struct.servent* @getservbyport(i32 %32, i8* %4)
%34 = icmp eq %struct.servent* %33, null
br i1 %34, label %35, label %39
35: ; preds = %46, %29
%36 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%37 = load i32, i32* @errno
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %29
%40 = call i32 @printit(%struct.servent* %33)
%41 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%43 = load i8*, i8** %16
%44 = call %struct.servent* @getservbyname(i8* %43, i8* %4)
%45 = icmp eq %struct.servent* %44, null
br i1 %45, label %25, label %46
46: ; preds = %39
%47 = call i32 @printit(%struct.servent* %44)
%48 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%49 = call i32 @htons(i16 signext %15)
%50 = call %struct.servent* @getservbyport(i32 %49, i8* %4)
%51 = icmp eq %struct.servent* %50, null
br i1 %51, label %35, label %52
52: ; preds = %46
%53 = call i32 @printit(%struct.servent* %50)
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
}