#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 {
  char *s_name;
  char **s_aliases;
  int /*<<< orphan*/ s_port;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct servent *getservbyname(char *, char *);
int /*<<< orphan*/ herror(char *);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  struct servent *service;
  char **ptr;
  if (argc != 2) {
    fprintf(stderr, "only a single argument is supported\n");
    return 1;
  }
  service = getservbyname(argv[1], "tcp");
  if (!service) {
    herror("getservbyname failed");
    return 1;
  }
  printf("service: %s\n", service->s_name);
  printf("tcp port: %d\n", ntohs(service->s_port));
  if (*service->s_aliases) {
    printf("aliases:");
    for (ptr = service->s_aliases; *ptr; ptr++)
      printf(" %s", *ptr);
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1191 <main+0x41>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1272 <main+0x122>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe85(%rip),%rsi
call   1040 <getservbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cc <main+0x7c>
lea    0xe6e(%rip),%rdi
call   1060 <herror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1272 <main+0x122>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xe64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%edi
call   1070 <ntohs@plt>
mov    %eax,%esi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
cmpq   $0x0,(%rax)
je     126b <main+0x11b>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     125d <main+0x10d>
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    1229 <main+0xd9>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    1213 <main+0xb3>
mov    0x8(%rsi),%rdi
lea    0xead(%rip),%rsi
call   1050 <getservbyname@plt>
test   %rax,%rax
je     122a <main+0xca>
mov    %rax,%rbx
mov    (%rax),%rsi
lea    0xeab(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rbx),%edi
call   1080 <ntohs@plt>
lea    0xe9f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%rax
cmpq   $0x0,(%rax)
je     123c <main+0xdc>
lea    0xe93(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rbx),%rbp
mov    0x0(%rbp),%rsi
test   %rsi,%rsi
je     1207 <main+0xa7>
add    $0x8,%rbp
lea    0xe7a(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x0(%rbp),%rsi
add    $0x8,%rbp
test   %rsi,%rsi
jne    11f0 <main+0x90>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    123c <main+0xdc>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1236 <main+0xd6>
lea    0xdf8(%rip),%rdi
call   1070 <herror@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i8*, i8**, i32 }
@stderr = global i32 0
@.str = [37 x i8] c"only a single argument is supported\0A\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [21 x i8] c"getservbyname failed\00"
@.str.3 = [13 x i8] c"service: %s\0A\00"
@.str.4 = [14 x i8] c"tcp port: %d\0A\00"
@.str.5 = [9 x i8] c"aliases:\00"
@.str.6 = [4 x i8] c" %s\00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.servent*
%7 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %56
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call %struct.servent* @getservbyname(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store %struct.servent* %17, %struct.servent** %6
%18 = load %struct.servent*, %struct.servent** %6
%19 = icmp ne %struct.servent* %18, null
br i1 %19, label %22, label %20
20: ; preds = %13
%21 = call i32 @herror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %56
22: ; preds = %13
%23 = load %struct.servent*, %struct.servent** %6
%24 = getelementptr inbounds %struct.servent, %struct.servent* %23, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load %struct.servent*, %struct.servent** %6
%28 = getelementptr inbounds %struct.servent, %struct.servent* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load %struct.servent*, %struct.servent** %6
%33 = getelementptr inbounds %struct.servent, %struct.servent* %32, i32 0, i32 1
%34 = load i8**, i8*** %33
%35 = load i8*, i8** %34
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %55
37: ; preds = %22
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%39 = load %struct.servent*, %struct.servent** %6
%40 = getelementptr inbounds %struct.servent, %struct.servent* %39, i32 0, i32 1
%41 = load i8**, i8*** %40
store i8** %41, i8*** %7
br label %42
42: ; preds = %50, %37
%43 = load i8**, i8*** %7
%44 = load i8*, i8** %43
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %53
46: ; preds = %42
%47 = load i8**, i8*** %7
%48 = load i8*, i8** %47
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %46
%51 = load i8**, i8*** %7
%52 = getelementptr inbounds i8*, i8** %51, i32 1
store i8** %52, i8*** %7
br label %42
53: ; preds = %42
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %55
55: ; preds = %53, %22
store i32 0, i32* %3
br label %56
56: ; preds = %55, %20, %10
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
%struct.servent = type { i8*, i8**, i32 }
@stderr = global i32 0
@.str = [37 x i8] c"only a single argument is supported\0A\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [21 x i8] c"getservbyname failed\00"
@.str.3 = [13 x i8] c"service: %s\0A\00"
@.str.4 = [14 x i8] c"tcp port: %d\0A\00"
@.str.5 = [9 x i8] c"aliases:\00"
@.str.6 = [4 x i8] c" %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %40
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call %struct.servent* @getservbyname(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq %struct.servent* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @herror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %40
14: ; preds = %7
%15 = getelementptr inbounds %struct.servent, %struct.servent* %10, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds %struct.servent, %struct.servent* %10, i64 0, i32 2
%19 = load i32, i32* %18
%20 = tail call i32 @ntohs(i32 %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds %struct.servent, %struct.servent* %10, i64 0, i32 1
%23 = load i8**, i8*** %22
%24 = load i8*, i8** %23
%25 = icmp eq i8* %24, null
br i1 %25, label %40, label %26
26: ; preds = %14
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%28 = load i8**, i8*** %22
%29 = load i8*, i8** %28
%30 = icmp eq i8* %29, null
br i1 %30, label %38, label %31
31: ; preds = %26, %31
%32 = phi i8* [ %36, %31 ], [ %29, %26 ]
%33 = phi i8** [ %35, %31 ], [ %28, %26 ]
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %32)
%35 = getelementptr inbounds i8*, i8** %33, i64 1
%36 = load i8*, i8** %35
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %31
38: ; preds = %31, %26
%39 = tail call i32 @putchar(i32 10)
br label %40
40: ; preds = %14, %38, %12, %4
%41 = phi i32 [ 1, %4 ], [ 1, %12 ], [ 0, %38 ], [ 0, %14 ]
ret i32 %41
}