#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 */
typedef scalar_t__ uid_t;
struct passwd {
  char *pw_name;
  scalar_t__ pw_uid;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
char *strtok(char *, char *);
scalar_t__ strtoll(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  if (argc != 2) {
    printf("need to supply one username as argument\n");
    exit(1);
  }
  FILE *pw = fopen("/etc/passwd", "r");
  if (pw == NULL) {
    perror("fopen /etc/passwd");
    exit(2);
  }
  char *pwstring = malloc(4096);
  if (pwstring == NULL) {
    perror("malloc buffer");
    exit(3);
  }
  struct passwd mypasswd;
  while (fgets(pwstring, 4096, pw)) {
    char *p = strtok(pwstring, ":");
    if (strcmp(argv[1], p) == 0) {
      mypasswd.pw_name = strdup(p);
      if (mypasswd.pw_name == NULL) {
        perror("strdup");
        exit(5);
      }
      p = strtok(NULL, ":");
      p = strtok(NULL, ":");
      mypasswd.pw_uid = (uid_t)strtoll(p, NULL, 10);
      printf("Username %s, UID %lld\n", mypasswd.pw_name,
             (long long)mypasswd.pw_uid);
    }
  }
  return 0;
}


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   $0x2,-0x8(%rbp)
je     11d8 <main+0x38>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe50(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1210 <main+0x70>
lea    0xe28(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    $0x1000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123f <main+0x9f>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
jmp    1244 <main+0xa4>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1316 <main+0x176>
mov    -0x20(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1311 <main+0x171>
mov    -0x38(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12bd <main+0x11d>
lea    0xd9d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x5,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd81(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd6d(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoll@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1244 <main+0xa4>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12d0 <main+0x120>
mov    %rsi,%r14
lea    0xe38(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12e6 <main+0x136>
mov    %rax,%r15
mov    $0x1000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12fc <main+0x14c>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12a9 <main+0xf9>
lea    0xe04(%rip),%r12
lea    0xe06(%rip),%r13
jmp    1245 <main+0x95>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12a9 <main+0xf9>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10b0 <strtok@plt>
mov    %rax,%rbp
mov    0x8(%r14),%rdi
mov    %rax,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1230 <main+0x80>
mov    %rbp,%rdi
call   10d0 <strdup@plt>
test   %rax,%rax
je     12ba <main+0x10a>
mov    %rax,%rbp
xor    %edi,%edi
mov    %r12,%rsi
call   10b0 <strtok@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   10b0 <strtok@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1070 <strtoll@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1230 <main+0x80>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x5,%edi
call   10c0 <exit@plt>
lea    0xd69(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd13(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%edi
call   10c0 <exit@plt>
lea    0xd0f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x3,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8*, i64 }
@.str = [41 x i8] c"need to supply one username as argument\0A\00"
@.str.1 = [12 x i8] c"/etc/passwd\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [18 x i8] c"fopen /etc/passwd\00"
@.str.4 = [14 x i8] c"malloc buffer\00"
@.str.5 = [2 x i8] c":\00"
@.str.6 = [7 x i8] c"strdup\00"
@.str.7 = [23 x i8] c"Username %s, UID %lld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca %struct.passwd
%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 %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %15
%23 = call i8* @malloc(i32 4096)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 3)
unreachable
29: ; preds = %22
br label %30
30: ; preds = %65, %29
%31 = load i8*, i8** %7
%32 = load i32*, i32** %6
%33 = call i64 @fgets(i8* %31, i32 4096, i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %66
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = call i8* @strtok(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %37, i8** %9
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = load i8*, i8** %9
%42 = call i64 @strcmp(i8* %40, i8* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %65
44: ; preds = %35
%45 = load i8*, i8** %9
%46 = call i8* @strdup(i8* %45)
%47 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 0
store i8* %46, i8** %47
%48 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 0
%49 = load i8*, i8** %48
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%53 = call i32 @exit(i32 5)
unreachable
54: ; preds = %44
%55 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %55, i8** %9
%56 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %56, i8** %9
%57 = load i8*, i8** %9
%58 = call i64 @strtoll(i8* %57, i32* null, i32 10)
%59 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 1
store i64 %58, i64* %59
%60 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 0
%61 = load i8*, i8** %60
%62 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i32 0, i32 1
%63 = load i64, i64* %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %61, i64 %63)
br label %65
65: ; preds = %54, %35
br label %30
66: ; preds = %30
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"/etc/passwd\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [18 x i8] c"fopen /etc/passwd\00"
@.str.4 = [14 x i8] c"malloc buffer\00"
@.str.5 = [2 x i8] c":\00"
@.str.6 = [7 x i8] c"strdup\00"
@.str.7 = [23 x i8] c"Username %s, UID %lld\0A\00"
@str = [40 x i8] c"need to supply one username as argument\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 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %7
%14 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%15 = icmp eq i8* %14, null
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = tail call i64 @fgets(i8* %14, i32 4096, i32* %8)
%18 = icmp eq i64 %17, 0
br i1 %18, label %43, label %19
19: ; preds = %16
%20 = getelementptr inbounds i8*, i8** %1, i64 1
br label %24
21: ; preds = %13
%22 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%23 = tail call i32 @exit(i32 3)
unreachable
24: ; preds = %19, %40
%25 = tail call i8* @strtok(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%26 = load i8*, i8** %20
%27 = tail call i64 @strcmp(i8* %26, i8* %25)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = tail call i8* @strdup(i8* %25)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%34 = tail call i32 @exit(i32 5)
unreachable
35: ; preds = %29
%36 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%37 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%38 = tail call i64 @strtoll(i8* nocapture %37, i32* null, i32 10)
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i8* %30, i64 %38)
br label %40
40: ; preds = %35, %24
%41 = tail call i64 @fgets(i8* %14, i32 4096, i32* %8)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %24
43: ; preds = %40, %16
ret i32 0
}